Repository navigation
Expand file tree
/
Copy pathworkers.js
More file actions
225 lines (182 loc) · 5.19 KB
/
Copy pathworkers.js
File metadata and controls
225 lines (182 loc) · 5.19 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
import Cluster from 'cluster';
import vm from 'vm';
let Workers = {
Jobs: new Mongo.Collection('workers.jobs'),
initialize() {
global.__Workers = Workers;
this.module = module;
if (this.isMaster) {
this.initMaster();
}
else if (this.isWorker) {
this.initWorker();
}
},
////////////////////
// Master //
////////////////////
isMaster: Cluster.isMaster,
poolSize: require('os').cpus().length,
workers: {},
promises: {},
jobCount: 0,
pollingInterval: 100,
initMaster() {
this.Jobs.remove({});
this.watchCompletedJobs();
this.watchExitedWorkers();
},
watchCompletedJobs() {
this.Jobs.find({status: 'completed'}).observe({
added: job => {
if (!this.promises[job._id])
return;
this.jobCount -= 1;
this.Jobs.remove(job._id);
this.promises[job._id](job.result);
delete this.promises[job._id];
},
});
},
watchExitedWorkers() {
Cluster.on('exit', (worker, code, signal) => {
if (worker.exitedAfterDisconnect) {
console.log(`\x1b[36mWorker ${worker.id}\x1b[0m timed out`);
}
else {
console.log(`\x1b[36mWorker ${worker.id}\x1b[0m exited with \x1b[${code ? '31' : '32'}mcode ${code}\x1b[0m`);
}
delete this.workers[worker.id];
});
},
spawn() {
let worker = Cluster.fork({
PORT: 0,
METEOR_SHELL_DIR: '',
KADIRA_OPTIONS_HOSTNAME: '',
KADIRA_APP_ID: '',
KADIRA_APP_SECRET: '',
BABEL_CACHE_DIR: `/tmp/meteor-workers/${Random.id()}/.babel-cache`,
});
this.workers[worker.id] = worker;
return worker;
},
run(f, params={}) {
this.jobCount += 1;
// If all the workers are active, and the number of workers
// is less than poolSize, spawn a new worker.
this.maybeSpawnWorker();
return new Promise(resolve => {
let id = this.Jobs.insert({
status: 'queued',
code: _.isString(f) ? f : f.toString(),
params: EJSON.stringify(params),
});
this.promises[id] = resolve;
});
},
map(jobs, f, chunkSize=5) {
let code = f.toString(),
chunks = this.chunk(jobs, chunkSize);
let promises = chunks.map(chunk => {
return this.run(({chunk, code}) => {
let Workers = global.__Workers;
let f = Workers.parseCode(code);
return Workers.mapFibers(chunk, f);
}, {chunk, code});
});
return _.flatten(Promise.awaitAll(promises), true);
},
mapFibers(jobs, f) {
let promises = jobs.map(job => {
return Promise.asyncApply(f, this, [job], jobs.length === 1);
});
return Promise.awaitAll(promises);
},
maybeSpawnWorker() {
let numWorkers = _.size(this.workers);
if (numWorkers < this.jobCount && numWorkers < this.poolSize)
this.spawn();
},
wait(job) {
return Promise.await(job);
},
waitAll(jobs) {
return Promise.awaitAll(jobs);
},
////////////////////
// Worker //
////////////////////
isWorker: Cluster.isWorker,
workerTimeout: 60000,
lastRunJob: undefined,
initWorker() {
console.log(`Starting \x1b[36mworker ${Cluster.worker.id}\x1b[0m.`);
this.lastRunJob = new Date();
this.watchJobs();
},
watchJobs() {
// If there haven't been any jobs for workerTimeout milliseconds, exit
if (this.lastRunJob && new Date() - this.lastRunJob >= this.workerTimeout) {
Cluster.worker.kill();
}
// Pop a job off the queue
let job;
while (true) {
job = this.Jobs.findOne({status: 'queued'});
if (!job)
break;
// Claim job and avoid race conditions
let success = this.Jobs.update({
_id: job._id,
status: 'queued'
}, {
$set: {
status: 'started',
},
});
if (success)
break;
}
if (job) {
this.doJob(job);
}
if (this.Jobs.find({status: 'queued'}).count() > 0)
this.watchJobs();
else
Meteor.setTimeout(() => this.watchJobs(), this.pollingInterval);
},
doJob(job) {
// Run job
let f = this.parseCode(job.code),
result = f.call(global, EJSON.parse(job.params));
// Mark job as completed
this.Jobs.update(job._id, {
$set: {
status: 'completed',
result,
}
});
// Log work completed
let numJobs = this.Jobs.find({status: {$in: ['started', 'queued']}}).count();
console.log(`\x1b[36mWorker ${Cluster.worker.id}\x1b[0m has finished job ${job._id}. ${numJobs} jobs remaining.`);
this.lastRunJob = new Date();
},
parseCode(rawCode) {
let compiled = Package.ecmascript.ECMAScript.compileForShell(`(${rawCode})`),
code = code = `var module = global.__Workers.module; module.importSync = module.importSync || module.import; var _module = module; var require = module.require; \n\n${compiled}`;
script = new vm.Script(code);
return script.runInThisContext();
},
chunk(list, chunkSize) {
let chunks = [];
for (let i=0; i < list.length; i += chunkSize) {
chunks.push(list.slice(i, i + chunkSize));
}
return chunks;
},
};
if (process.env.METEOR_WORKERS_MANUAL_INIT !== 'true') {
Meteor.startup(() => Workers.initialize());
}
export default Workers;