Skip to content

Commit 1560e64

Browse files
author
Piotr Oleś
committed
Fix memory limit in multi-process mode
1 parent 8d9b794 commit 1560e64

File tree

4 files changed

+65
-29
lines changed

4 files changed

+65
-29
lines changed

lib/cluster.js

Lines changed: 35 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,47 @@
1-
var cluster = require('cluster');
21
var process = require('process');
2+
var childProcess = require('child_process');
33
var path = require('path');
44

55
var WorkResult = require('./WorkResult');
66
var NormalizedMessage = require('./NormalizedMessage');
77

8-
// setup master
9-
cluster.setupMaster({
10-
exec: path.join(__dirname, 'service.js'),
11-
args: ['--max-old-space-size=' + process.env.MEMORY_LIMIT]
12-
});
13-
148
// fork workers...
159
var division = parseInt(process.env.WORK_DIVISION);
10+
var workers = [];
1611

17-
for (var i = 0; i < division; i++) {
18-
cluster.fork({ WORK_NUMBER: i });
12+
for (var number = 0; number < division; number++) {
13+
workers.push(
14+
childProcess.fork(
15+
path.resolve(__dirname, './service.js'),
16+
[],
17+
{
18+
execArgv: ['--max-old-space-size=' + process.env.MEMORY_LIMIT],
19+
env: Object.assign({}, process.env, { WORKER_NUMBER: number }),
20+
stdio: ['inherit', 'inherit', 'inherit', 'ipc']
21+
}
22+
)
23+
);
1924
}
2025

21-
var workerIds = Object.keys(cluster.workers);
22-
var result = new WorkResult(workerIds);
26+
var pids = workers.map(function (worker) { return worker.pid; });
27+
var result = new WorkResult(pids);
2328

2429
process.on('message', function (message) {
2530
// broadcast message to all workers
26-
workerIds.forEach(function (workerId) {
27-
cluster.workers[workerId].send(message);
31+
workers.forEach(function (worker) {
32+
worker.send(message);
2833
});
2934

3035
// clear previous result set
3136
result.clear();
3237
});
3338

3439
// listen to all workers
35-
workerIds.forEach(function (workerId) {
36-
cluster.workers[workerId].on('message', function (message) {
40+
workers.forEach(function (worker) {
41+
worker.on('message', function (message) {
3742
// set result from worker
3843
result.set(
39-
workerId,
44+
worker.pid,
4045
{
4146
diagnostics: message.diagnostics.map(NormalizedMessage.createFromJSON),
4247
lints: message.lints.map(NormalizedMessage.createFromJSON)
@@ -67,3 +72,17 @@ workerIds.forEach(function (workerId) {
6772
}
6873
});
6974
});
75+
76+
process.on('SIGINT', function () {
77+
process.exit();
78+
});
79+
80+
process.on('exit', function () {
81+
workers.forEach(function (worker) {
82+
try {
83+
worker.kill();
84+
} catch (e) {
85+
// do nothing...
86+
}
87+
});
88+
});

lib/index.js

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -130,15 +130,11 @@ ForkTsCheckerWebpackPlugin.prototype.pluginStart = function () {
130130

131131
ForkTsCheckerWebpackPlugin.prototype.pluginStop = function () {
132132
this.compiler.plugin('done', function () {
133-
if (!this.isWatching && this.service) {
134-
try {
135-
this.service.kill();
136-
} catch (e) {
137-
if (this.logger && !this.silent) {
138-
this.logger.error(e);
139-
}
140-
}
141-
}
133+
this.killService();
134+
}.bind(this));
135+
136+
process.on('exit', function () {
137+
this.killService(true);
142138
}.bind(this));
143139
};
144140

@@ -251,6 +247,23 @@ ForkTsCheckerWebpackPlugin.prototype.spawnService = function () {
251247
this.service.on('exit', this.handleServiceExit.bind(this));
252248
};
253249

250+
ForkTsCheckerWebpackPlugin.prototype.killService = function (force) {
251+
if ((force || !this.isWatching) && this.service) {
252+
try {
253+
if (this.cancellationToken) {
254+
this.cancellationToken.requestCancellation();
255+
}
256+
257+
this.service.kill();
258+
this.service = undefined;
259+
} catch (e) {
260+
if (this.logger && !this.silent) {
261+
this.logger.error(e);
262+
}
263+
}
264+
}
265+
};
266+
254267
ForkTsCheckerWebpackPlugin.prototype.handleServiceMessage = function (message) {
255268
if (this.cancellationToken) {
256269
this.cancellationToken.cleanupCancellation();

lib/service.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ var checker = new IncrementalChecker(
1111
parseInt(process.env.WORK_DIVISION, 10)
1212
);
1313

14-
var diagnostics = [];
15-
var lints = [];
16-
1714
function run (cancellationToken) {
15+
var diagnostics = [];
16+
var lints = [];
17+
1818
checker.nextIteration();
1919

2020
try {
@@ -46,3 +46,7 @@ function run (cancellationToken) {
4646
process.on('message', function (message) {
4747
run(CancellationToken.createFromJSON(message));
4848
});
49+
50+
process.on('SIGINT', function () {
51+
process.exit();
52+
});

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "fork-ts-checker-webpack-plugin",
3-
"version": "0.1.0",
3+
"version": "0.1.1",
44
"description": "Runs typescript type checker and linter on separate process.",
55
"main": "lib/index.js",
66
"scripts": {

0 commit comments

Comments
 (0)