forked from victorenator/node-sd-daemon
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
96 lines (70 loc) · 2.24 KB
/
index.js
File metadata and controls
96 lines (70 loc) · 2.24 KB
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
var util = require('util');
var binding = require('./daemon');
var watchdog = null;
exports.booted = binding.booted;
exports.LISTEN_FDS_START = binding.LISTEN_FDS_START;
exports.listen_fds = function() {
return parseInt(process.env['LISTEN_FDS'], 10) || 0;
};
exports.notify = function() {
return binding.notify(util.format.apply(util, arguments));
};
exports.notifyReady = function() {
return exports.notify('READY=1');
};
exports.notifyReloading = function() {
return exports.notify('RELOADING=1');
};
exports.notifyStatus = function(status) {
return exports.notify('STATUS=%s', status);
};
exports.notifyStopping = function() {
return exports.notify('STOPPING=1');
};
exports.notifyWatchdog = function() {
return exports.notify('WATCHDOG=1');
};
exports.watchdogUsec = function() {
return parseInt(process.env['WATCHDOG_USEC'], 10) || null;
};
/**
* Starts the watchdog ping if the watchdog enabled (WatchdogSec)
* @param {number} k
*/
exports.startWatchdogPing = function(k) {
if (watchdog) return;
var timeout = exports.watchdogUsec();
if (!timeout) return;
if (!(k > 0 && k < 1)) k = 0.5;
timeout = Math.round(timeout * k / 1000);
watchdog = setInterval(function() {
exports.notifyWatchdog();
}, timeout);
};
exports.stopWatchdogPing = function() {
if (watchdog) {
clearInterval(watchdog);
watchdog = null;
}
};
var net = require('net');
var Pipe = process.binding('pipe_wrap').Pipe;
var origListen = net.Server.prototype.listen;
net.Server.prototype.listen = function(arg, cb) {
if (typeof arg === 'object' && arg !== null && 'systemd' in arg) {
if (arg.systemd >= exports.listen_fds()) {
this.emit('listening', new Error('bad socket activation descriptor'));
} else {
return this.listen({fd: exports.LISTEN_FDS_START + arg.systemd, listened: true}, cb);
}
} else if (typeof arg === 'object' && arg !== null && 'fd' in arg && arg.listened) {
if (cb) this.once('listening', cb);
this._handle = new Pipe();
this._handle.open(arg.fd);
this._listen2(null, -1, -1);
this.emit('listening');
} else {
origListen.apply(this, arguments);
}
return this;
};