-
Notifications
You must be signed in to change notification settings - Fork 4
/
monitor.js
74 lines (59 loc) · 1.72 KB
/
monitor.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
/**
* Copyright (c) 2012 Andreas Madsen
* MIT License
*/
(function () {
"use strict";
var util = require('util');
var events = require('events');
var helpers = require('../helpers.js');
var streams = helpers.core('streams');
function MonitorAbstract(error, settings, daemonPid, shutdown, child, ready) {
var self = this;
this.error = error;
this.settings = {
file: settings.file,
args: settings.args,
env: settings.env,
exec: settings.exec
};
this.strategy = settings.strategy;
this.options = settings.options;
this.ready = function () {
process.nextTick(ready);
};
this.stdout = new streams.RelayStream({ paused: true });
this.stderr = new streams.RelayStream({ paused: true });
this.pid = {
process: null,
monitor: process.pid,
daemon: daemonPid
};
// the shutdown argument contain shutdown methods
// this function will simply execute them all
this.shutdown = function (done) {
var methods = Object.keys(shutdown);
// keep track of shutdown
var track = new helpers.ProgressTracker(done);
track.add(methods);
// execute all shutdown methods
methods.forEach(function (type) {
shutdown[type](track.set.bind(track, type));
});
};
// restart the child process
this.restart = function () {
if (child.alive) {
child.once('stop', child.spawn.bind(child, true));
child.kill();
return;
}
// if auto is true the process will restart by itself
if (settings.auto === false) {
child.spawn(true);
}
};
}
util.inherits(MonitorAbstract, events.EventEmitter);
module.exports = MonitorAbstract;
})();