-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
325 lines (281 loc) · 7.9 KB
/
index.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
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
const Vinyl = require('vinyl');
const assert = require('assert');
const cluster = require('cluster');
const fs = require('fs');
const globStream = require('glob-stream');
const log = require('fancy-log');
const minimist = require('minimist');
const os = require('os');
const through2 = require('through2');
require('colors');
/*
Grab all the options, but ignore the task names passed on the parent
command line, since we'll be supplying our own task name in the
child process.
*/
const argv = minimist(process.argv.slice(2));
function getWorkerArgs() {
const args = [];
const names = Object.keys(argv);
for (let k of names) {
if (k === '_')
continue;
if (k.length === 1) {
args.push(`-${k}=${JSON.stringify(argv[k])}`);
} else {
args.push(`--${k}`);
args.push(argv[k]);
}
}
return args;
}
/**
* IPC function helpers
*/
function sendLog(message) {
if (cluster.isMaster) {
throw new Error(
'sendLog() cannot be called from master process.'
);
}
process.send({
type: 'log',
message
});
}
function sendRaw(message) {
if (cluster.isMaster) {
throw new Error(
'sendRaw() cannot be called from master process.'
);
}
process.send({
type: 'raw',
message
});
}
function requestFile() {
if (cluster.isMaster) {
throw new Error(
'requestFile() cannot be called from master process.'
);
}
process.send({
type: 'getfile'
});
}
function sendFile(worker, file) {
if (cluster.isWorker) {
throw new Error(
'sendFile() cannot be called from worker process.'
);
}
worker.send({
type: 'file',
file
});
}
function sendDone(worker) {
if (cluster.isWorker) {
throw new Error(
'sendDone() cannot be called from worker process.'
);
}
worker.send({
type: 'end'
});
}
function sendError(worker, e) {
if (cluster.isMaster) {
throw new Error(
'sendError() cannot be called from master process.'
);
}
process.send({
type: 'error',
message: e
});
}
/**
* Fork our gulpfile and kick off the task.
* Returns a Promise that resolves when the process ends.
*/
function createWorker(task, args, handlers) {
cluster.setupMaster({
args: [task, ...args, '--silent']
});
const worker = cluster.fork();
/*log(
'spawned ' +
`worker #${worker.id}`.dim.red +
' with task ' +
task
);*/
worker.on('message', message => {
handlers[message.type](worker, message)
});
return new Promise(function (resolve, reject) {
worker.on('exit', resolve);
worker.on('error', reject);
});
}
/**
* Sets up the IPC message handlers, and spawn `workerCount` child processes
* to actually process the files.
*/
function spawnWorkers(taskName, workerCount, fileStream) {
const handlers = {
getfile(worker) {
const file = fileStream.read();
if (file === null) {
sendDone(worker);
return;
}
sendFile(worker, file);
},
log(worker, msg) {
log(
'[' + `worker ${worker.id}`.red + ']: ' +
msg.message
);
},
raw(worker, msg) {
console.log(msg.message);
},
error(worker, msg) {
throw new Error(msg.message.error.message);
}
}
const workerArgs = getWorkerArgs();
log(
`spawning ${workerCount.toString().yellow} worker ` +
`processes for task ${taskName.cyan }`
);
const promises = [];
for (let i = 0; i < workerCount; ++i) {
promises.push(createWorker(
taskName,
workerArgs,
handlers
));
}
return Promise.all(promises);
}
/**
* Creates a vinyl stream from messages received from the cluster IPC channel.
* We read the file's contents in here, because sending file contents over the
* IPC channel would be really dumb.
*/
function createWorkerFilestream() {
const fileStream = through2.obj();
const messageHandlers = {
file(msg) {
const file = msg.file;
file.contents = fs.readFileSync(file.path);
fileStream.push(new Vinyl(file));
},
end() {
fileStream.push(null); // We done
//sendLog(`Task '${taskName.cyan}' DONE`);
cluster.worker.disconnect();
}
}
process.on('message', message => messageHandlers[message.type](message));
return fileStream;
}
/**
* The meat of the worker process
*/
function setupChildPipeline(taskName, builder) {
const pipeline = builder(createWorkerFilestream(taskName))
/*
We tack this on at the tail end so that whenever we'red
done with a file, we request a new one from the parent process.
*/
.pipe(through2.obj((file, enc, done) => {
requestFile();
done();
}));
// Let's kick things off
requestFile();
return pipeline;
}
module.exports = function (gulp) {
if (cluster.isWorker) {
gulp.on('start', () => {
/*
We have to override Gulp's (really Undertaker's) dependency and
task tree in the child process to make it appear as though the
task we want to run in parallel is the *only* task in the gulpfile.
Since the task dependencies have already been processed in the
parent process, we don't want to run them *again* in the forked
worker process.
This is probably *completely* unsupported and will likely break
in the future.
*/
const [workerTaskName] = argv._;
const { constructor: DefaultRegistry } = Object.getPrototypeOf(gulp.registry());
const registry = new class extends DefaultRegistry {
set(taskName, task) {
if (taskName === workerTaskName) {
return super.set(taskName, task);
}
}
};
gulp.registry(registry);
});
gulp.on('error', (e) =>
{
sendError(cluster.worker, e)
})
}
/**
* The main attraction
*/
function clusterSrc(globs, opts, builder) {
builder = typeof opts === 'function' ? opts : builder;
opts = typeof opts === 'function' ? {} : opts;
assert(typeof opts.taskName === 'string', 'opts.taskName is not a string');
const { taskName } = opts;
if (cluster.isMaster) {
const workerCount = +(opts.concurrency || os.cpus().length);
const fileStream = globStream(globs, opts);
return spawnWorkers(taskName, workerCount, fileStream);
} else {
//sendLog(`Task '${taskName.cyan}' STARTING`);
return setupChildPipeline(taskName, builder);
}
}
/**
* Convenience plugin to list processed files out from parent process.
* Child processes are invoked with --silent, so they can't output things
* directly.
*/
clusterSrc.logfiles = () => {
if (cluster.isMaster) {
throw new Error(
'logfiles() cannot be called from the master process'
);
}
return through2.obj((file, enc, done) => {
sendLog(file.path);
done(null, file);
});
}
/**
* Expose this for custom pipelines
*/
clusterSrc.log = (msg) => {
if (cluster.isWorker)
sendLog(msg);
else
log(msg);
}
clusterSrc.raw = (msg) => {
if (cluster.isWorker)
sendRaw(msg);
else
console.log(msg);
};
return clusterSrc;
};