-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathDispatcher.js
More file actions
368 lines (340 loc) · 11.9 KB
/
Copy pathDispatcher.js
File metadata and controls
368 lines (340 loc) · 11.9 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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
var fs = require('fs');
var EventEmitter = require('events').EventEmitter;
var chalk = require('chalk');
var Pubsub = require('./Pubsub.js');
var helpers = require('../helpers.js');
var ENGINE_REGISTRY_NAMESPACE = 'engine-registry';
var PROGRAM_MONITOR_NAMESPACE = 'program-monitor';
/** Engine provides a **client-side** interface for CodeEngine.
* Similar object is defined in the client-side library "things.js"
*/
function Engine(dispatcher, id, meta){
var self = this;
this.dispatcher = dispatcher;
this.pubsub = dispatcher.pubsub;
this.id = id;
this.meta = meta;
this.status = "unknown";
this.stats = [];
this.console = [];
this.codes = {};
this._requests = {};
this.pubsub.subscribe(this.dispatcher.id+'/'+this.id, function(message, topic){
if (message.reply_id in self._requests){
self._requests[message.reply_id].resolve(message.payload);
clearTimeout(self._requests[message.reply_id].timer);
delete self._requests[message.reply_id];
}
else {
console.log(chalk.red('[Dispatcher:'+this.dispatcher.id+'] Received unexpected message'));
}
});
this.pubsub.subscribe(this.id+'/resource', function(message, topic){
self.stats.push(message);
});
this.pubsub.subscribe(this.id+'/console', function(message, topic){
// console.log(message);
message.forEach(function(line){
self.console.push(line);
if (self.console.length > 200) self.console.shift();
});
})
// console.log('Engine '+id+' connected');
}
Engine.prototype.getStat = function(){
return this.stats[this.stats.length-1];
}
Engine.prototype.sendCommand = function(ctrl, kwargs){
var self = this;
var deferred = helpers.defer();
var request_id = helpers.randKey(16);
this._requests[request_id] = deferred;
this.pubsub.publish(this.id+'/cmd', {
request_id: request_id,
reply_to: this.dispatcher.id+'/'+this.id,
ctrl: ctrl,
kwargs: kwargs
})
deferred.timer = setTimeout(function(){
if (request_id in self._requests){
deferred.reject('PubsubCommandTimeout');
delete self._requests[request_id];
}
}, 30000); // assume failure if reply not received
return deferred.promise
}
Engine.prototype.runCode = function(code_name, source){
return this.sendCommand('run_code', {
mode: 'raw',
code_name: code_name,
source: source
});
}
// Engine.prototype.pauseCode = function(code_name, instance_id){
// return this.sendCommand('pause_code', {
// code_name: code_name,
// instance_id: instance_id
// });
// }
// Engine.prototype.resumeCode = function(code_name, instance_id){
// return this.sendCommand('resume_code', {
// code_name: code_name,
// instance_id: instance_id
// });
// }
Engine.prototype.killCode = function(code_name, instance_id){
return this.sendCommand('kill_code', {
code_name: code_name,
instance_id: instance_id
});
}
Engine.prototype.migrateCode = function(code_name, instance_id, target_engine){
return this.sendCommand('migrate_code', {
code_name: code_name,
instance_id: instance_id,
engine: target_engine
});
}
Engine.prototype.spawnCode = function(code_name, instance_id, target_engines){
return this.sendCommand('spawn_code', {
code_name: code_name,
instance_id: instance_id,
engines: target_engines
});
}
/** Program provides a **client-side** interface for CodeEngine.
*/
function Program(dispatcher, code_name, instance_id, source){
var self = this;
this.dispatcher = dispatcher;
this.pubsub = dispatcher.pubsub;
this.code_name = code_name;
this.id = instance_id;
this.source = source;
this.status = undefined;
this.engine = undefined;
this.stats = [];
this.console = [];
this.snapshots = [];
this._requests = {};
this.pubsub.subscribe(this.dispatcher.id+'/'+this.id, function(message, topic){
// console.log(message);
if (message.reply_id in self._requests){
self._requests[message.reply_id].resolve(message.payload);
clearTimeout(self._requests[message.reply_id].timer);
delete self._requests[message.reply_id];
}
else {
console.log(chalk.red('[Dispatcher:'+this.dispatcher.id+'] Received unexpected message'));
}
});
this.pubsub.subscribe(this.code_name+'/'+this.id+'/resource', function(message, topic){
self.stats.push(message);
// console.log(message);
});
this.pubsub.subscribe(this.code_name+'/'+this.id+'/console', function(message, topic){
message.forEach(function(line){
self.console.push(line);
});
// console.log(message);
});
this.pubsub.subscribe(this.code_name+'/'+this.id+'/snapshots', function(message, topic){
self.snapshots.push(message);
// console.log(message);
});
}
Program.prototype.getStat = function(){
return this.stats[this.stats.length-1];
}
Program.prototype.sendCommand = function(ctrl, kwargs){
var self = this;
var deferred = helpers.defer();
var request_id = helpers.randKey(16);
self._requests[request_id] = deferred;
self.pubsub.publish(self.code_name+'/'+self.id+'/cmd', {
request_id: request_id,
reply_to: self.dispatcher.id+'/'+self.id,
ctrl: ctrl,
kwargs: kwargs
})
deferred.timer = setTimeout(function(){
if (request_id in self._requests){
deferred.reject('PubsubCommandTimeout');
delete self._requests[request_id];
}
}, 30000); // assume failure if reply not received
// console.log(self._requests);
return deferred.promise
}
Program.prototype.pause = function(){
return this.sendCommand('pause')
}
Program.prototype.resume = function(){
return this.sendCommand('resume')
}
Program.prototype.kill = function(){
return this.sendCommand('kill')
}
/**
* This object is used to control the CodeEngine instances in the network (i.e. connected to the same Pub/Sub service)
* @constructor
* @param {object} config - Configuration for the Dispatcher
* @param {string} config.pubsub_url - URL of the Pub/Sub server. Defaults to mqtt://localhost
* @param {object} options - options object for customizing Dispatcher behaviour
*/
function Dispatcher(config, options){
EventEmitter.call(this);
var self = this;
this.config = Object.assign({
pubsub_url: 'mqtt://localhost'
}, config);
this.id = (this.config.id || 'dispatcher-'+helpers.randKey());
this.options = Object.assign({}, options);
console.log(chalk.green('[Dispatcher:'+this.id+'] Initialized'));
this.pubsub = new Pubsub(this.config.pubsub_url);
this.engines = {};
this.programs = {};
this.pubsub.subscribe(ENGINE_REGISTRY_NAMESPACE, function(message, topic){
// self.engines[message.id] = message;
// console.log(chalk.green('[Dispatcher:'+self.id+'] engine status update ')+message.id+' - '+message.status);
// console.log(topic, message);
if (!(message.id in self.engines)){
self.engines[message.id] = new Engine(self, message.id, message.meta);
self.emit('engine-join', self.engines[message.id], message);
}
self.engines[message.id].status = message.status;
self.engines[message.id].meta = message.meta;
self.engines[message.id].codes = message.codes;
// console.log(self.engines);
// self.emit('update');
// self.printEngines();
self.emit('engine-registry-update', self.engines[message.id], message);
});
this.pubsub.subscribe(PROGRAM_MONITOR_NAMESPACE, function(message, topic){
// self.engines[message.id] = message;
// console.log(chalk.green('[Dispatcher:'+self.id+'] engine status update ')+message.id+' - '+message.status);
// self.printEngines();
// console.log(topic, message);
if (!(message.instance_id in self.programs)){
self.programs[message.instance_id] = new Program(self, message.code_name, message.instance_id, message.source);
self.emit('program-join', self.programs[message.instance_id], message);
}
self.programs[message.instance_id].engine = message.engine;
self.programs[message.instance_id].status = message.status;
if (message.source) self.programs[message.instance_id].source = message.source;
// self.printPrograms();
self.emit('program-monitor-update', self.programs[message.instance_id], message);
});
this.pubsub.on('ready', function(){
self.emit('ready');
console.log(chalk.green('[Dispatcher:'+self.id+'] connected to Pub/Sub at '+self.pubsub.url));
self.requestStatusReports();
});
};
Dispatcher.prototype = new EventEmitter();
Dispatcher.prototype.constructor = Dispatcher;
/**
* Send command to a CodeEngine to run a program.
* @param {string} engine_id - The ID of the CodeEngine instance
* @param {string} code_name - Human readable code name (e.g. example.js)
* @param {string} source - Source code in UTF-8 string
* @return {Promise} - The Promise object returned is resolved when Dispatcher receives acknowledgment from the CodeEngine.
*/
Dispatcher.prototype.runCode = function(engine_id, code_name, source){
return this.engines[engine_id].runCode(code_name, source);
};
/**
* [moveCode description]
* @param {String} from_id [description]
* @param {String} to_id [description]
* @param {String} code_name [description]
* @param {String} instance_id [description]
* @return {Promise} [description]
*/
Dispatcher.prototype.moveCode = function(from_id, to_id, code_name, instance_id){
return this.engines[from_id].migrateCode(code_name, instance_id, to_id);
};
/**
* [spawnCode description]
* @param {String} from_id [description]
* @param {Array} to_ids [description]
* @param {String} code_name [description]
* @param {String} instance_id [description]
* @return {Promise} [description]
*/
Dispatcher.prototype.spawnCode = function(from_id, to_ids, code_name, instance_id){
return this.engines[from_id].spawnCode(code_name, instance_id, to_ids);
};
/**
* [pauseCode description]
* @param {String} instance_id [description]
* @return {Promise} [description]
*/
Dispatcher.prototype.pauseCode = function(instance_id){
// return this.engines[engine_id].pauseCode(code_name, instance_id);
return this.programs[instance_id].pause();
};
/**
* [resumeCode description]
* @param {String} instance_id [description]
* @return {Promise} [description]
*/
Dispatcher.prototype.resumeCode = function(instance_id){
// return this.engines[engine_id].pauseCode(code_name, instance_id);
return this.programs[instance_id].resume();
};
/**
* [killCode description]
* @param {String} instance_id [description]
* @return {Promise} [description]
*/
Dispatcher.prototype.killCode = function(instance_id){
// return this.engines[engine_id].killCode(code_name, instance_id);
return this.programs[instance_id].kill();
};
/**
* [kill description]
* @return {Promise} [description]
*/
Dispatcher.prototype.kill = function(){
console.log(chalk.green('[Dispatcher:'+this.id+'] Killed gracefully'));
return this.pubsub.kill()
};
var ACTION_MAP = {
'run': 'runCode',
'pause': 'pauseCode',
'resume': 'resumeCode',
'kill': 'killCode',
'migrate': 'moveCode'
};
/**
* Apply the given list of actions concurrently
* @param {Array} actions - Array of objects describing actions to perform
* @return {Promise} - Promise that resolves when all actions are successful
*/
Dispatcher.prototype.applyActions = function(actions){
var self = this;
var promises = actions.map(function(action){
return self[ACTION_MAP[action.type]].apply(self, action.args);
});
return Promise.all(promises);
};
/**
* Send a Pub/Sub broadcast message to request status reports from all the CodeEngine instances
* @return {undefined} - Does not return anything, as we cannot know for sure if all CodeEngines have reported.
*/
Dispatcher.prototype.requestStatusReports = function(){
this.pubsub.publish(ENGINE_REGISTRY_NAMESPACE+'/bcast', { ctrl: 'report' });
this.pubsub.publish(PROGRAM_MONITOR_NAMESPACE+'/bcast', { ctrl: 'report' });
};
Dispatcher.prototype.printEngines = function(){
return console.log(Object.values(this.engines).map(function(engine){
return [engine.id, (engine.status === 'idle' ? chalk.green(engine.status) : chalk.red(engine.status))].join('\t')
}).join('\n'));
};
Dispatcher.prototype.printPrograms = function(){
return console.log(Object.values(this.programs).map(function(program){
return [program.id, (program.status === 'idle' ? chalk.green(program.status) : chalk.red(program.status))].join('\t')
}).join('\n'));
};
module.exports = Dispatcher;