This repository has been archived by the owner on Feb 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
75 lines (64 loc) · 1.62 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
/*!
* hot-restart - index.js
* Copyright(c) 2014 dead_horse <dead_horse@qq.com>
* MIT Licensed
*/
'use strict';
var debug = require('debug')('hot-restart:master');
var EventEmitter = require('events').EventEmitter;
var util = require('util');
function Restarter() {
this.workers = [];
}
util.inherits(Restarter, EventEmitter);
Restarter.prototype.add = function(worker) {
var self = this;
if (Array.isArray(worker)) {
worker.forEach(function (w) {
self.add(w);
});
return;
}
if (~this.workers.indexOf(worker)) return;
this.workers.push(worker);
debug('add worker, worker list length %d', this.workers.length);
};
Restarter.prototype.remove = function(worker) {
var self = this;
if (Array.isArray(worker)) {
worker.forEach(function (w) {
self.remove(w);
});
return;
}
var index = this.workers.indexOf(worker);
if (index < 0) return;
this.workers.splice(index, 1);
debug('remove worker, worker list length %d', this.workers.length);
};
Restarter.prototype.send = function () {
this.workers.forEach(function (worker) {
try {
worker.send({action: '__reload__'});
} catch (err) {
console.error('send reload message to worker error: %s', err.message);
}
});
return this;
};
var restarter = null;
module.exports = function (sig) {
if (restarter) {
return restarter;
}
restarter = new Restarter();
sig = sig || 'SIGPIPE';
process.on(sig, function () {
debug('get restart sig: %s, send restart message to workers', sig);
restarter.send();
setImmediate(function () {
restarter.emit('restart');
});
});
return restarter;
};