-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
Copy pathWorker.js
149 lines (125 loc) · 4.17 KB
/
Worker.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/**
* Copyright 2013 the PM2 project authors. All rights reserved.
* Use of this source code is governed by a license that
* can be found in the LICENSE file.
*/
var vizion = require('vizion');
var cst = require('../constants.js');
var async = require('async');
var debug = require('debug')('pm2:worker');
var domain = require('domain');
module.exports = function(God) {
var timer = null;
God.Worker = {};
God.Worker.is_running = false;
var _getProcessById = function(pm_id) {
var proc = God.clusters_db[pm_id];
return proc ? proc : null;
};
var maxMemoryRestart = function(proc_key, cb) {
var proc = _getProcessById(proc_key.pm2_env.pm_id);
if (!(proc &&
proc.pm2_env &&
proc_key.monit))
return cb();
if (proc_key.monit.memory !== undefined &&
proc.pm2_env.max_memory_restart !== undefined &&
proc.pm2_env.max_memory_restart < proc_key.monit.memory &&
proc.pm2_env.axm_options &&
proc.pm2_env.axm_options.pid === undefined) {
console.log('[PM2][WORKER] Process %s restarted because it exceeds --max-memory-restart value (current_memory=%s max_memory_limit=%s [octets])', proc.pm2_env.pm_id, proc_key.monit.memory, proc.pm2_env.max_memory_restart);
God.softReloadProcessId({
id : proc.pm2_env.pm_id
}, function(err, data) {
if (err)
console.error(err.stack || err);
return cb();
});
}
else {
return cb();
}
};
var versioningRefresh = function(proc_key, cb) {
var proc = _getProcessById(proc_key.pm2_env.pm_id);
if (!(proc &&
proc.pm2_env &&
(proc.pm2_env.vizion !== false && proc.pm2_env.vizion != "false") &&
proc.pm2_env.versioning &&
proc.pm2_env.versioning.repo_path)) {
return cb();
}
if (proc.pm2_env.vizion_running === true)
{
debug('Vizion is already running for proc id: %d, skipping this round', proc.pm2_env.pm_id);
return cb();
}
proc.pm2_env.vizion_running = true;
var repo_path = proc.pm2_env.versioning.repo_path;
vizion.analyze({
folder: proc.pm2_env.versioning.repo_path
},
function(err, meta) {
if (err != null)
return cb();
proc = _getProcessById(proc_key.pm2_env.pm_id);
if (!(proc &&
proc.pm2_env &&
proc.pm2_env.versioning &&
proc.pm2_env.versioning.repo_path)) {
console.error('Proc not defined anymore or versioning unknown');
return cb();
}
proc.pm2_env.vizion_running = false;
meta.repo_path = repo_path;
proc.pm2_env.versioning = meta;
debug('[PM2][WORKER] %s parsed for versioning', proc.pm2_env.name);
return cb();
});
};
var tasks = function() {
if (God.Worker.is_running === true) {
debug('[PM2][WORKER] Worker is already running, skipping this round');
return false;
}
God.Worker.is_running = true;
God.getMonitorData(null, function(err, data) {
if (err || !data || typeof(data) !== 'object') {
God.Worker.is_running = false;
return console.error(err);
}
async.eachLimit(data, 1, function(proc_key, next) {
if (!proc_key ||
!proc_key.pm2_env ||
proc_key.pm2_env.pm_id === undefined)
return next();
debug('[PM2][WORKER] Processing proc id:', proc_key.pm2_env.pm_id);
versioningRefresh(proc_key, function() {
maxMemoryRestart(proc_key, function() {
return next();
});
});
}, function(err) {
God.Worker.is_running = false;
debug('[PM2][WORKER] My job here is done, next job in %d seconds', parseInt(cst.WORKER_INTERVAL / 1000));
});
});
};
var wrappedTasks = function() {
var d = domain.create();
d.once('error', function(err) {
console.error('[PM2][WORKER] Error caught by domain:\n' + (err.stack || err));
God.Worker.is_running = false;
});
d.run(function() {
tasks();
});
};
God.Worker.start = function() {
timer = setInterval(wrappedTasks, cst.WORKER_INTERVAL);
};
God.Worker.stop = function() {
if (timer !== null)
clearInterval(timer);
};
};