forked from ionic-team/ionic-app-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworker-client.ts
More file actions
119 lines (99 loc) Β· 3.18 KB
/
worker-client.ts
File metadata and controls
119 lines (99 loc) Β· 3.18 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
import { BuildContext, WorkerProcess, WorkerMessage } from './util/interfaces';
import { BuildError } from './util/errors';
import { jsonToBuildError } from './util/helpers';
import { Logger } from './logger/logger';
import { fork, ChildProcess } from 'child_process';
import { join } from 'path';
export function runWorker(taskModule: string, taskWorker: string, context: BuildContext, workerConfig: any) {
return new Promise((resolve, reject) => {
const worker = <ChildProcess>createWorker(taskModule);
const msg: WorkerMessage = {
taskModule,
taskWorker,
context: {
// only copy over what's important
// don't copy over the large data properties
rootDir: context.rootDir,
tmpDir: context.tmpDir,
srcDir: context.srcDir,
wwwDir: context.wwwDir,
wwwIndex: context.wwwIndex,
buildDir: context.buildDir,
bundledFilePaths: context.bundledFilePaths,
isProd: context.isProd,
isWatch: context.isWatch,
runAot: context.runAot,
runMinifyJs: context.runMinifyJs,
runMinifyCss: context.runMinifyCss,
optimizeJs: context.optimizeJs,
bundler: context.bundler,
inlineTemplates: context.inlineTemplates,
},
workerConfig
};
worker.on('message', (msg: WorkerMessage) => {
if (msg.error) {
reject(new BuildError(msg.error));
} else if (msg.reject) {
const buildError = jsonToBuildError(msg.reject);
reject(buildError);
} else {
resolve(msg.resolve);
}
killWorker(msg.pid);
});
worker.on('error', (err: any) => {
Logger.error(`worker error, taskModule: ${taskModule}, pid: ${worker.pid}, error: ${err}`);
});
worker.on('exit', (code: number) => {
Logger.debug(`worker exited, taskModule: ${taskModule}, pid: ${worker.pid}`);
});
worker.send(msg);
});
}
function killWorker(pid: number) {
for (var i = workers.length - 1; i >= 0; i--) {
if (workers[i].worker.pid === pid) {
try {
workers[i].worker.kill('SIGKILL');
} catch (e) {
Logger.error(`killWorker, ${pid}: ${e}`);
} finally {
delete workers[i].worker;
workers.splice(i, 1);
}
}
}
}
export function createWorker(taskModule: string): any {
for (var i = workers.length - 1; i >= 0; i--) {
if (workers[i].task === taskModule) {
try {
workers[i].worker.kill('SIGKILL');
} catch (e) {
Logger.debug(`createWorker, ${taskModule} kill('SIGKILL'): ${e}`);
} finally {
delete workers[i].worker;
workers.splice(i, 1);
}
}
}
try {
const workerModule = join(__dirname, 'worker-process.js');
const worker = fork(workerModule, process.argv, {
env: {
FORCE_COLOR: true,
npm_config_argv: process.env.npm_config_argv
}
});
Logger.debug(`worker created, taskModule: ${taskModule}, pid: ${worker.pid}`);
workers.push({
task: taskModule,
worker: worker
});
return worker;
} catch (e) {
throw new BuildError(`unable to create worker-process, task: ${taskModule}: ${e}`);
}
}
export const workers: WorkerProcess[] = [];