-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy paththreadStorm.js
More file actions
213 lines (175 loc) · 5.45 KB
/
Copy paththreadStorm.js
File metadata and controls
213 lines (175 loc) · 5.45 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
var events = require('events'),
eventEmitter = new events.EventEmitter(),
threadStorm=module.exports={},
ee = threadStorm.ee = eventEmitter,
cluster = require('cluster'),
config;
threadStorm.isMaster = cluster.isMaster;
if(cluster.isMaster) {
var workers={},
availableWorkers=[],
CPUs = require('os').cpus(),
taskMapping={};
threadStorm.start = function(threadStormConfig) {
config = threadStormConfig || {
threadsPerCore: 1
};
registerListeners();
waitForForking();
CPUs.forEach(function() {
for(let i=0; i < config.threadsPerCore; i++) {
cluster.fork();
}
});
};
threadStorm.runTask = function(task,data) {
var msgObj,
msg;
var worker = getAvailableWorker();//workers[availableWorkers[0]];
if(worker) {
msg = "Running Worker";
msgObj = {
status: 1,
msg:msg
};
taskMapping[worker.id]=task;
setWorkerUnAvailable(worker);
return worker.send({
cmd: 'run',
task: task,
data: data
});
}
else {
msg = "Currently there are no available workers to run " + task + ". What a sham.";
msgObj = {
status: 0,
msg:msg
};
console.log(msg);
return false;
}
};
getAvailableWorker = function() {
var worker = null;
for(let i = 0; i<availableWorkers.length;i++) {
if(availableWorkers[i] !== null) {
worker = workers[availableWorkers[i]];
break;
}
}
return worker;
};
setWorkerAvailable = function(worker) {
var index = availableWorkers.indexOf(worker.id);
/*if(index === -1) {
availableWorkers.push(worker.id);
}
else {
availableWorkers[index] = worker.id;
}*/
availableWorkers.push(worker.id);
console.log("worker " + worker.id + " is ready for work, and a little too excited about it.");
};
setWorkerUnAvailable = function(worker) {
var index = availableWorkers.indexOf(worker.id);
if(config.killOnComplete) {
delete workers[worker.id];
}
if(index > -1) {
//availableWorkers[index] = null;
availableWorkers.splice(index,1);
//availableWorkers.splice(index,index+1);
console.log("worker " + worker.id + " has been removed from available workers");
}
};
registerListeners = function() {
cluster.on('fork',function(worker) {
});
cluster.on('online',function(worker) {
workers[worker.id]=worker;
//console.log("my name is worker "+worker.id+", and I am online");
setWorkerAvailable(worker);
worker.on('message', function(msgObj) {
if(msgObj.msg === "completed") {
console.log("worker " + worker.id + " has completed running your code! TURN UP!");
//workers[worker.id]=worker;
if(config.killOnComplete) {
worker.kill();
}
else {
setWorkerAvailable(worker);
}
ee.emit('taskComplete',{task: taskMapping[worker.id]});
}
else {
ee.emit('msg',msgObj);
}
});
worker.on('error', function(err) {
console.log(`worker error ${err}`);
});
});
cluster.on('listening',function(worker,address) {
console.log("worker is listening");
});
cluster.on('disconnect', function(worker) {
if(worker.exitedAfterDisconnect) {
console.log("worker " + worker.id + " has disconnected. It is ok, it was intentional and very tasteful if I may be so bold.");
}
else {
console.log("worker " + worker.id + " has disconnected. The nerve...");
ee.emit('taskFailed',{task: taskMapping[worker.id]});
}
setWorkerUnAvailable(worker);
});
cluster.on('exit', function(worker, code, signal) {
if(worker.exitedAfterDisconnect) {
console.log("worker " + worker.id + " died. It completed its mission in life.");
}
else {
console.log("worker " + worker.id + " died. It was a massacre");
}
setWorkerUnAvailable(worker);
cluster.fork();
});
};
waitForForking = function() {
setTimeout(function() {
if(Object.keys(workers).length < CPUs.length) {
console.log("Not all of the workers are up yet. No worries, going to keep waiting for these slackers to come online. You can sit back and watch Dragon Ball Z");
waitForForking();
}
else {
ee.emit("ready");
console.log("All workers are up and running. Let's put them to work.");
}
},1000);
};
process.on('uncaughtException',function(err) {
console.log("totally uncaught exception");
console.log(err.stack);
});
}
else if(cluster.isWorker) {
var worker = cluster.worker,
i=0,
taskCommunicator,
taskToRun;
//can be called from task
module.exports.taskCommunicator = function(msgObj) {
process.send(msgObj);
}
//you can call when your task is completed
module.exports.completed = function(msgObj) {
process.send({msg: 'completed'});
//worker.kill();
}
process.on('message', function(msgObj) {
if(msgObj.cmd === "run") {
console.log("worker " + worker.id + " here, about run task "+msgObj.task+". Thank you for this opportunity, I am stoked. :)");
taskToRun=require(process.cwd()+"/"+msgObj.task);
taskToRun(msgObj.data,module.exports);
}
});
}