Skip to content

Commit

Permalink
fix pm2 env var evaluation, stop timer
Browse files Browse the repository at this point in the history
  • Loading branch information
megastef committed Jan 16, 2020
1 parent 2099b6a commit d1f42d4
Showing 1 changed file with 14 additions and 13 deletions.
27 changes: 14 additions & 13 deletions lib/processAgent.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
'use strict'
const { Agent, Config } = require('spm-agent')
const pidUsageTree = require('pidusage-tree')
const pm2Enabled = /^true/i.test(process.env.PM2)

const addMetrics = function (agent, pidToCheck) {
const timestamp = new Date()
pidUsageTree(pidToCheck, function (err, results) {
Expand All @@ -36,7 +38,6 @@ const addMetrics = function (agent, pidToCheck) {
}
}
agent.addMetrics(masterProcessMetric)

const childProcesses = processes.filter(proc => proc.pid !== pidToCheck)
if (childProcesses && childProcesses.length) {
childProcesses.forEach((proc, counter) => {
Expand Down Expand Up @@ -86,37 +87,37 @@ function getDefaultProcessMetrics (agent) {
if (process.send !== undefined) {
return // child process, not collecting stats
}

addMetrics(agent, process.pid)
}

function getPm2Metrics (agent) {
if (process.send === undefined) {
return // master pm2 process, not collecting stats
}

addMetrics(agent, process.ppid)
}

function getProcessStats () {
return {
start: function (agent) {
const timerId = setInterval(function () {
if (
Boolean(process.env.PM2) === true
) {
this.timerId = setInterval(function () {
if (pm2Enabled) {
getPm2Metrics(agent)
return
} else {
getDefaultProcessMetrics(agent)
}
getDefaultProcessMetrics(agent)
}, Config.collectionInterval)
if (timerId.unref) {
timerId.unref()
if (this.timerId.unref) {
this.timerId.unref()
}
},
stop: function () { }
stop: function () {
clearInterval(this.timerId)
}
}
}

module.exports = function () {
return new Agent(getProcessStats())
const a = getProcessStats()
return new Agent(a)
}

0 comments on commit d1f42d4

Please sign in to comment.