-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
71 lines (54 loc) · 1.71 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
var events = require('events');
var debug = require('debug')('microsvc-slave');
var util = require('util');
function Slave () {
this.id = undefined;
this.channel = new events.EventEmitter();
this.heartbeatFrequency = 1000;
this.slaves = {};
events.EventEmitter.apply(this);
}
util.inherits(Slave, events.EventEmitter);
Slave.prototype.configure = function configure (fn) {
fn(this);
};
Slave.prototype.initialize = function initialize (options) {
options = options || {};
if ( ! options.id) {
throw new Error('slave must be initialized with an id');
}
this.id = options.id;
if (options.heartbeatFrequency) {
this.heartbeatFrequency = options.heartbeatFrequency;
}
this._startHeartbeat();
this.emit('online', this);
this.channel.emit('online', this._createSlavePayload(this));
};
Slave.prototype.pauseHeartbeat = function pauseHeartbeat () {
clearInterval(this.heartbeatInterval);
};
Slave.prototype.resumeHeartbeat = function resumeHeartbeat () {
this._startHeartbeat();
};
Slave.prototype.stop = function stop () {
this.channel.removeAllListeners();
clearInterval(this.heartbeatInterval);
this.slaves = {};
this.emit('offline', this);
this.channel.emit('offline', this._createSlavePayload(this));
};
Slave.prototype._createSlavePayload = function _createSlavePayload (slave) {
return {
id: this.id
};
};
Slave.prototype._heartbeat = function _heartbeat () {
debug('heartbeat');
this.emit('heartbeat', this);
this.channel.emit('heartbeat', this._createSlavePayload(this));
};
Slave.prototype._startHeartbeat = function _startHeartbeat () {
this.heartbeatInterval = setInterval(this._heartbeat.bind(this), this.heartbeatFrequency);
};
module.exports = new Slave();