forked from aspectron/testnet
-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.js
98 lines (78 loc) · 2.38 KB
/
app.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
var JSON = require("json"),
_ = require("underscore"),
spawn = require("child_process").spawn,
colors = require('colors');
function dpc(t,fn) { if(typeof(t) == 'function') setTimeout(t,0); else setTimeout(fn,t); }
function Process(options)
{
var self = this;
self.options = options;
self.relaunch = true;
if(!options.descr)
throw new Error("descr option is required");
self.terminate = function()
{
if(self.process) {
self.relaunch = false;
self.process.kill('SIGTERM');
delete self.process;
}
else
console.error("Unable to terminate process, no process present");
}
self.restart = function()
{
if(self.process)
self.process.kill('SIGTERM');
}
self.run = function()
{
if(self.process) {
console.error(self.options);
throw new Error("Process is already running!");
}
self.relaunch = true;
self.process = spawn(self.options.process, self.options.args)
self.process.stdout.on('data',function (data) {
process.stdout.write(data);
if(options.logger)
options.logger.write(data);
});
self.process.stderr.on('data',function (data) {
process.stderr.write(data);
if(options.logger)
options.logger.write(data);
});
self.stdin = process.openStdin();
self.stdin.on('data', function(data) {
self.process.stdin.write(data);
});
self.process.on('exit',function (code) {
if(code) {
console.log("WARNING - Child process '"+self.options.descr.toUpperCase()+"' exited with code "+code);
}
delete self.process;
if(self.relaunch) {
console.log("Restarting '"+self.options.descr.toUpperCase()+"'");
dpc(self.run, options.restart_delay || 0);
}
});
}
}
function Application() {
var self = this;
self.process = { }
var processes =
{
'testnet' : { script: 'testnet.js' },
}
_.each(processes, function(o, name){
self.process[name] = new Process({
process: o.process || process.execPath,
args: o.script ? [o.script] : o.args,
descr : name
});
self.process[name].run();
});
}
GLOBAL.app = new Application();