-
Notifications
You must be signed in to change notification settings - Fork 10
/
index.js
180 lines (146 loc) · 3.51 KB
/
index.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
var pstree = require("ps-tree");
var spawn = require("child_process").spawn;
var exec = require("child_process").exec;
var logPrefix = "[go-run]";
function log() {
var args = Array.prototype.slice.call(arguments);
args.unshift(logPrefix);
console.log.apply(console, args);
}
// ps is a collection object to hold references to running GoRuns
var ps = {};
function delps(pid) {
delete ps[pid];
}
function addps(pid, go) {
ps[pid] = go;
return go;
}
// fshut provides a method to kill running processes started through GoRuns
// This is not a general shutdown method, but a way to handle shutdowns when
// gulp exits due to an uncaughtException.
var fshut = function(callback) {
var keys = Object.keys(ps);
if (keys.length === 0) {
if ("function" === typeof callback) {
callback();
}
return;
}
var go = ps[keys[0]];
if (go) {
go.stop(function() {
fshut(callback); // shutdown the next process
});
}
};
process.on("uncaughtException", function(err) {
log("uncaught exception", err);
log("forcing shutdown");
fshut(function() {
log("forcing shutdown: complete");
process.exit(1);
});
});
/*
* expose
*/
module.exports = {
ps: function() {
return ps;
},
run: function(main, args, opts) {
var go = new GoRun(main, args, opts);
return go.run();
},
GoRun: GoRun
};
// GoRun is the runner class for a single `go run ...` process
function GoRun(main, args, opts) {
this.main = main || "main.go";
if (typeof(this.main) == 'string') {
this.main = [this.main];
}
this.args = args || [];
this.opts = opts || {};
}
GoRun.prototype._spawn = function() {
var args = Array.prototype.slice.call(arguments);
log("starting process...");
if (this.opts.godep) {
log("using `godep go`");
args.unshift("go");
return spawn("godep", args, this.opts);
} else {
return spawn("go", args, this.opts);
}
};
var noop = function() {
//
};
GoRun.prototype.run = function() {
var args = ["run"].concat(this.main);
args = args.concat(this.args, Array.prototype.slice.call(arguments));
var proc = this.proc = this._spawn.apply(this, args);
var pid = proc.pid;
log("["+pid+"]", "processs started");
if (proc.stdout) {
proc.stdout.on("data", this.opts.onStdout || noop);
}
if (proc.stderr) {
proc.stderr.on("data", this.opts.onStderr || noop);
}
proc.on("close", this.opts.onClose || noop);
proc.on("exit", this.opts.onExit || noop);
return addps(pid, this);
};
GoRun.prototype._stop = function(callback) {
if (!!!this.proc.pid) {
log("no pid:", "exit");
callback();
return;
}
var pid = this.proc.pid;
pstree(pid, function (err, children) {
var i = 0;
var len = children.length;
var kill = function(n) {
var child = children[n];
if (!!!child) {
callback();
return;
}
exec("kill " + child.PID, function() {
i++;
if (i === len) {
log("["+pid+"]", "process stopped");
callback();
return;
}
kill(i);
});
};
kill(i);
});
};
GoRun.prototype.stop = function(callback) {
if ("function" !== typeof callback) {
callback = noop;
}
var pid = this.proc.pid;
log("["+pid+"]", "stopping process...");
var self = this;
var fn = function() {
delps(pid);
callback();
};
this._stop(fn);
};
GoRun.prototype.restart = function() {
var pid = this.proc.pid;
log("["+pid+"]", "restarting process...");
var self = this;
self.stop(function() {
self.run();
});
};