-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPartitioner.js
80 lines (69 loc) · 2.32 KB
/
Partitioner.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
76
77
78
79
80
var cluster = require('cluster');
var jobService = require("./Services/JobService");
var partitionService = require("./Services/PartitionService");
var q = require("q");
var jobs = require("./Application/Jobs");
var Lock = require("./Infrastructure/ExecuteLocked");
var lock = new Lock();
var workers = [];
var workerPartitionIndex = 0;
var numberOfWorkers;
function Worker(worker){
this.worker = worker;
worker.on('message', function(id) {
console.log("complete notify received for id " + id);
jobService.done(id);
});
}
if(cluster.isWorker) {
console.log("worker %d registered", process.pid);
process.on('message', function(job) {
console.log("job " + job.id + " received");
executeJob(job).then(function(){});
});
}
function Partitioner(configuration){
numberOfWorkers = configuration.numberOfWorkers || 1;
if(cluster.isWorker)
throw new Error("a worker is trying to instantiate a partitioner");
for(var i=0; i < numberOfWorkers; i++){
workers.push(new Worker(cluster.fork()));
}
}
Partitioner.prototype.enqueueJob = function(job, callback){
if(job === null
|| job === undefined
|| job.id === null
|| job.id === undefined
|| job.partitionId === null
|| job.partitionId === undefined
|| job.type === null
|| job.type === undefined)
throw new Error("Job null or invalid, should contain id, partitionId, type, data: {}");
lock.execWrite(function(){
return partitionService.get(job.partitionId)
.then(function(partition){
if(partition == null) {
var index = ++workerPartitionIndex % numberOfWorkers;
return partitionService.push(job.partitionId, workers[index].worker);
}else{
return partition;
}
});
}).then(function(partition){
jobService.push(job.id, callback).then(function(){
partition.worker.send(job);
});
});
};
function executeJob(job){
return q.Promise(function(resolve, reject){
jobs[job.type](job).then(function(){
process.send(job.id);
resolve();
}).catch(function(err){
reject(err);
});
});
}
module.exports = Partitioner;