This repository was archived by the owner on Sep 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathis-master.js
More file actions
200 lines (186 loc) · 5.23 KB
/
is-master.js
File metadata and controls
200 lines (186 loc) · 5.23 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
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
'use strict';
var mongoose = require('mongoose'),
Schema = mongoose.Schema,
util = require('util'),
EventEmitter = require("events").EventEmitter,
os = require('os');
function im() {
EventEmitter.call(this);
}
// Inherit the EventEmitter into our im prototype
util.inherits(im, EventEmitter);
/**
* Sets the default variables
*/
im.prototype.master = false;
im.prototype.collection = 'node';
im.prototype.hostname = os.hostname();
im.prototype.pid = process.pid;
im.prototype.versions = process.versions;
im.prototype.id = null;
im.prototype.timeout = 60;
/**
* Function initializes options, does some basic option verification and starts is-master
*/
im.prototype.start = function(options) {
if (options) {
if (options.timeout) {
options.timeout = parseInt(options.timeout, 10);
if (isNaN(options.timeout)) throw 'im: timeout is not a number!';
}
util._extend(this, options);
}
this.mongooseInit();
this.startWorker();
};
/**
* Function initializes the mongoose table, schema, and model
*/
im.prototype.mongooseInit = function() {
var imSchema = new Schema({
hostname: {
type: String,
trim: true,
default: '',
},
pid: {
type: Number,
},
versions: {
type: Object,
},
memory: {
type: Object,
},
uptime: {
type: Number,
},
startDate: {
type: Date,
default: Date.now,
index: {
unique: true
}
},
updateDate: {
type: Date,
default: Date.now,
index: {
expires: this.timeout + 60
}
}
});
// ensure we aren't attempting to redefine a collection that already exists
if (mongoose.models.hasOwnProperty(this.collection)) {
this.imModel = mongoose.model(this.collection);
} else{
this.imModel = mongoose.model(this.collection, imSchema);
}
this.worker = new this.imModel({
hostname: this.hostname,
pid: this.pid,
versions: this.versions,
memory: process.memoryUsage(),
uptime: process.uptime(),
startDate: new Date(),
updateDate: new Date()
});
};
/**
* Function saves the worker in the db and updates it
*/
im.prototype.startWorker = function() {
var _this = this;
this.worker.save(function(err, worker) {
if (err) {
if (err.code === 11000) {
_this.worker.startDate = new Date();
_this.worker.updateDate = new Date();
return _this.startWorker();
} else {
throw err;
}
}
_this.id = worker._id;
_this.isMaster(function(err, results) {
if (err) return console.error(err);
_this.master = results;
_this.emit('connected');
if (_this.master) {
_this.emit('master');
} else {
_this.emit('slave');
_this.emit('secondary');
}
_this.process();
});
});
};
/**
* Function that runs the worker that checks in whith the DB
*/
im.prototype.process = function() {
var _this = this;
// Update this node in the cluster every x timeout
setInterval(function() {
_this.imModel.updateOne({
_id: _this.id
}, {
hostname: _this.hostname,
pid: _this.pid,
versions: _this.versions,
memory: process.memoryUsage(),
uptime: process.uptime(),
updateDate: new Date()
}, {
upsert: true, // handle event where document was deleted
setDefaultsOnInsert: true, // on insert, make sure to set default values
}, function(err, results) {
if (err) return console.error(err);
_this.emit('synced');
_this.isMaster(function (err, results) {
if (err) return console.error(err);
if (results !== _this.master) {
_this.master = results;
_this.emit('changed');
if (_this.master) {
_this.emit('master');
} else {
_this.emit('slave');
_this.emit('secondary');
}
}
});
});
}, _this.timeout * 1000);
};
/**
* Function returns true/false if the node proc is the master (the oldest node in the cluster)
*/
im.prototype.isMaster = function(callback) {
var _this = this;
if (this.id) {
this.imModel.findOne({}, {
id: 1
}, {
sort: {
startDate: 1
}
}, function(err, results) {
if (err) return callback(err);
if(!results){
return callback(err, false);
} else if (results._id.toString() === _this.id.toString()) {
callback(err, true);
} else {
callback(err, false);
}
});
} else {
callback(null, false);
}
};
/**
* Expose im
*/
module.exports = new im();