pm2-perfmonitor 1.2.1 → 1.2.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/app.js +11 -38
- package/lib/defaults.js +35 -0
- package/lib/message.js +22 -0
- package/lib/pm2-extra.js +4 -4
- package/package.json +9 -4
package/lib/app.js
CHANGED
|
@@ -1,39 +1,9 @@
|
|
|
1
1
|
const pmx = require('pmx')
|
|
2
2
|
const pm2 = require('pm2')
|
|
3
|
-
const {
|
|
3
|
+
const { listAppsAsync } = require('./pm2-extra')
|
|
4
4
|
const { parseParamToArray, parseParamToNumber, parseBool } = require('./utils')
|
|
5
|
-
|
|
6
|
-
const
|
|
7
|
-
enabled: true,
|
|
8
|
-
/**
|
|
9
|
-
* 排除的 app 名
|
|
10
|
-
*/
|
|
11
|
-
excludeApps: [],
|
|
12
|
-
/**
|
|
13
|
-
* 包含的 app 名
|
|
14
|
-
*/
|
|
15
|
-
includeApps: [],
|
|
16
|
-
/**
|
|
17
|
-
* 定时检测间隔(ms)
|
|
18
|
-
*/
|
|
19
|
-
workerInterval: 60000,
|
|
20
|
-
/**
|
|
21
|
-
* 是否开启僵尸进程守护
|
|
22
|
-
*/
|
|
23
|
-
zombieDetection: true,
|
|
24
|
-
/**
|
|
25
|
-
* 僵尸状态最大出现次数
|
|
26
|
-
*/
|
|
27
|
-
zombieMaxHits: 10,
|
|
28
|
-
/**
|
|
29
|
-
* 僵尸状态达到最大容忍度时,是否自动重启僵尸进程
|
|
30
|
-
*/
|
|
31
|
-
autoRestartWhenZombieDetected: true,
|
|
32
|
-
/**
|
|
33
|
-
* 僵尸进程最大重启次数,设置为0表示不限制
|
|
34
|
-
*/
|
|
35
|
-
zombieMaxRestarts: 0,
|
|
36
|
-
}
|
|
5
|
+
const { defaultOptions } = require('./defaults')
|
|
6
|
+
// const { sendMessage } = require('./message')
|
|
37
7
|
|
|
38
8
|
const conf = pmx.initModule({}, (err, incomingConf) => {
|
|
39
9
|
if (err) {
|
|
@@ -88,7 +58,7 @@ const zombieProcessChecker = async () => {
|
|
|
88
58
|
if (!ZOMBIE_DETECTION) return
|
|
89
59
|
|
|
90
60
|
try {
|
|
91
|
-
const apps = await
|
|
61
|
+
const apps = await listAppsAsync()
|
|
92
62
|
|
|
93
63
|
apps.forEach((app) => {
|
|
94
64
|
const { name, pm_id, monit, pm2_env } = app
|
|
@@ -96,6 +66,7 @@ const zombieProcessChecker = async () => {
|
|
|
96
66
|
const appStatus = pm2_env?.status
|
|
97
67
|
const appCpuUsage = monit?.cpu || 0
|
|
98
68
|
|
|
69
|
+
// 非目标应用,跳过
|
|
99
70
|
if (
|
|
100
71
|
MODULE_NAME === name ||
|
|
101
72
|
(INCLUDE_APPS.length > 0 && !INCLUDE_APPS.includes(name)) ||
|
|
@@ -104,7 +75,7 @@ const zombieProcessChecker = async () => {
|
|
|
104
75
|
return
|
|
105
76
|
}
|
|
106
77
|
|
|
107
|
-
//
|
|
78
|
+
// 只处理 online 状态的进程
|
|
108
79
|
if (appStatus !== 'online') {
|
|
109
80
|
// 进程不在 online 状态时,清空其历史记录,避免干扰
|
|
110
81
|
cpuHistory.delete(pm_id)
|
|
@@ -124,10 +95,12 @@ const zombieProcessChecker = async () => {
|
|
|
124
95
|
history.shift()
|
|
125
96
|
}
|
|
126
97
|
|
|
127
|
-
//
|
|
128
|
-
|
|
98
|
+
// 判断是否为僵尸:最近 ZOMBIE_MAX_HITS 次全是 0%
|
|
129
99
|
if (isZombie(history)) {
|
|
130
|
-
logger(
|
|
100
|
+
logger(
|
|
101
|
+
'info',
|
|
102
|
+
`Zombie detected: ${name} (pm_id: ${pm_id}, pid: ${app.pid})`,
|
|
103
|
+
)
|
|
131
104
|
|
|
132
105
|
if (AUTO_RESTART_WHEN_ZOMBIE_DETECTED) {
|
|
133
106
|
if (
|
package/lib/defaults.js
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
const defaultOptions = {
|
|
2
|
+
enabled: true,
|
|
3
|
+
/**
|
|
4
|
+
* 排除的 app 名
|
|
5
|
+
*/
|
|
6
|
+
excludeApps: [],
|
|
7
|
+
/**
|
|
8
|
+
* 包含的 app 名
|
|
9
|
+
*/
|
|
10
|
+
includeApps: [],
|
|
11
|
+
/**
|
|
12
|
+
* 定时检测间隔(ms)
|
|
13
|
+
*/
|
|
14
|
+
workerInterval: 60000,
|
|
15
|
+
/**
|
|
16
|
+
* 是否开启僵尸进程守护
|
|
17
|
+
*/
|
|
18
|
+
zombieDetection: true,
|
|
19
|
+
/**
|
|
20
|
+
* 僵尸状态最大出现次数
|
|
21
|
+
*/
|
|
22
|
+
zombieMaxHits: 10,
|
|
23
|
+
/**
|
|
24
|
+
* 僵尸状态达到最大容忍度时,是否自动重启僵尸进程
|
|
25
|
+
*/
|
|
26
|
+
autoRestartWhenZombieDetected: true,
|
|
27
|
+
/**
|
|
28
|
+
* 僵尸进程最大重启次数,设置为0表示不限制
|
|
29
|
+
*/
|
|
30
|
+
zombieMaxRestarts: 0,
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
module.exports = {
|
|
34
|
+
defaultOptions,
|
|
35
|
+
}
|
package/lib/message.js
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
const pm2 = require('pm2')
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @param { number } pid 进程id
|
|
5
|
+
* @param { string } eventName 事件名
|
|
6
|
+
* @param { Object } data
|
|
7
|
+
*/
|
|
8
|
+
const sendMessage = (pid, eventName, data) => {
|
|
9
|
+
pm2.sendDataToProcessId(pid, {
|
|
10
|
+
id: pid,
|
|
11
|
+
type: 'process:msg',
|
|
12
|
+
topic: true,
|
|
13
|
+
data: {
|
|
14
|
+
event: `pm2-perfmonitor:${eventName}`,
|
|
15
|
+
data,
|
|
16
|
+
},
|
|
17
|
+
})
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
module.exports = {
|
|
21
|
+
sendMessage,
|
|
22
|
+
}
|
package/lib/pm2-extra.js
CHANGED
|
@@ -3,7 +3,7 @@ const pm2 = require('pm2')
|
|
|
3
3
|
/**
|
|
4
4
|
* @returns { Promise<pm2.ProcessDescription[]> }
|
|
5
5
|
*/
|
|
6
|
-
const
|
|
6
|
+
const listAppsAsync = () => {
|
|
7
7
|
return new Promise((resolve, reject) => {
|
|
8
8
|
pm2.list((err, apps) => {
|
|
9
9
|
if (err) {
|
|
@@ -19,7 +19,7 @@ const getPm2ListAsync = () => {
|
|
|
19
19
|
* @param { string | number} pm_id
|
|
20
20
|
* @returns { Promise<void> }
|
|
21
21
|
*/
|
|
22
|
-
const
|
|
22
|
+
const stopAppAsync = (pm_id) => {
|
|
23
23
|
return new Promise((resolve, reject) => {
|
|
24
24
|
pm2.stop(pm_id, (err) => {
|
|
25
25
|
if (err) {
|
|
@@ -32,6 +32,6 @@ const pm2StopAsync = (pm_id) => {
|
|
|
32
32
|
}
|
|
33
33
|
|
|
34
34
|
module.exports = {
|
|
35
|
-
|
|
36
|
-
|
|
35
|
+
listAppsAsync,
|
|
36
|
+
stopAppAsync,
|
|
37
37
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "pm2-perfmonitor",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.3",
|
|
4
4
|
"description": "A pm2 module for performance monitoring. Automatically detect zombie processes and restart it",
|
|
5
5
|
"author": {
|
|
6
6
|
"name": "elenh",
|
|
@@ -20,8 +20,12 @@
|
|
|
20
20
|
},
|
|
21
21
|
"homepage": "https://github.com/yisibell/pm2-perfmonitor",
|
|
22
22
|
"scripts": {
|
|
23
|
-
"start": "pm2
|
|
24
|
-
"
|
|
23
|
+
"start-or-restart:app": "pm2 startOrRestart ecosystem.app.config.cjs --update-env",
|
|
24
|
+
"start:app": "pm2 start ecosystem.app.config.cjs",
|
|
25
|
+
"restart:app": "pm2 restart ecosystem.app.config.cjs",
|
|
26
|
+
"delete-start:app": "pm2 delete app1 || true && pm2 start ecosystem.app.config.cjs",
|
|
27
|
+
"start": "node ./scripts/app.js --env=app",
|
|
28
|
+
"dev": "pm2 restart ecosystem.dev.config.cjs",
|
|
25
29
|
"release": "changelogen --release && npm publish --access=public && git push --follow-tags"
|
|
26
30
|
},
|
|
27
31
|
"keywords": [
|
|
@@ -43,6 +47,7 @@
|
|
|
43
47
|
},
|
|
44
48
|
"devDependencies": {
|
|
45
49
|
"changelogen": "^0.6.2",
|
|
46
|
-
"cz-conventional-changelog": "^3.3.0"
|
|
50
|
+
"cz-conventional-changelog": "^3.3.0",
|
|
51
|
+
"minimist": "^1.2.8"
|
|
47
52
|
}
|
|
48
53
|
}
|