Skip to content

Commit

Permalink
feat: add poolRespawn flag to speed up incremental builds (#52)
Browse files Browse the repository at this point in the history
  • Loading branch information
jantimon authored and evilebottnawi committed Dec 21, 2018
1 parent bfb6271 commit 76535bf
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 0 deletions.
5 changes: 5 additions & 0 deletions README.md
Expand Up @@ -72,6 +72,11 @@ use: [
// additional node.js arguments
workerNodeArgs: ['--max-old-space-size=1024'],

// Allow to respawn a dead worker pool
// respawning slows down the entire compilation
// and should be set to false for development
poolRespawn: false,

// timeout for killing the worker processes when idle
// defaults to 500 (ms)
// can be set to Infinity for watching builds to keep workers alive
Expand Down
7 changes: 7 additions & 0 deletions src/WorkerPool.js
Expand Up @@ -263,6 +263,10 @@ export default class WorkerPool {
this.setupLifeCycle();
}

isAbleToRun() {
return !this.terminated;
}

terminate(force) {
if (!this.terminated) {
this.terminated = true;
Expand Down Expand Up @@ -341,5 +345,8 @@ export default class WorkerPool {
}
this.workers.clear();
}
if (!this.options.poolRespawn) {
this.terminate();
}
}
}
3 changes: 3 additions & 0 deletions src/index.js
Expand Up @@ -4,6 +4,9 @@ import { getPool } from './workerPools';
function pitch() {
const options = loaderUtils.getOptions(this) || {};
const workerPool = getPool(options);
if (!workerPool.isAbleToRun()) {
return;
}
const callback = this.async();
workerPool.run({
loaders: this.loaders.slice(this.loaderIndex + 1).map((l) => {
Expand Down
1 change: 1 addition & 0 deletions src/workerPools.js
Expand Up @@ -20,6 +20,7 @@ function getPool(options) {
workerParallelJobs: options.workerParallelJobs || 20,
poolTimeout: options.poolTimeout || 500,
poolParallelJobs: options.poolParallelJobs || 200,
poolRespawn: options.poolRespawn || false,
};
const tpKey = JSON.stringify(workerPoolOptions);
workerPools[tpKey] = workerPools[tpKey] || new WorkerPool(workerPoolOptions);
Expand Down
1 change: 1 addition & 0 deletions test/pitch.test.js
Expand Up @@ -11,6 +11,7 @@ jest.mock('../src/workerPools', () => {
const runGetPoolMock = (error) => {
getPool.mockImplementationOnce(() => {
return {
isAbleToRun: () => true,
run: jest.fn((opts, cb) => {
cb(error, {
fileDependencies: [],
Expand Down
11 changes: 11 additions & 0 deletions test/workerPool.test.js
Expand Up @@ -30,4 +30,15 @@ describe('workerPool', () => {
const workerPool = new WorkerPool({});
expect(() => workerPool.createWorker()).not.toThrow();
});

it('should be able to run if the worker pool was not terminated', () => {
const workerPool = new WorkerPool({});
expect(workerPool.isAbleToRun()).toBe(true);
});

it('should not be able to run if the worker pool was not terminated', () => {
const workerPool = new WorkerPool({});
workerPool.terminate();
expect(workerPool.isAbleToRun()).toBe(false);
});
});

0 comments on commit 76535bf

Please sign in to comment.