Skip to content

Commit

Permalink
Merge a8d3cd0 into 9b64f73
Browse files Browse the repository at this point in the history
  • Loading branch information
dawiss1337 committed Jan 21, 2019
2 parents 9b64f73 + a8d3cd0 commit 9be2b93
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 3 deletions.
1 change: 1 addition & 0 deletions REFERENCE.md
Expand Up @@ -96,6 +96,7 @@ interface AdvancedSettings {
retryProcessDelay: number = 5000; // delay before processing next job in case of internal error.
backoffStrategies: {}; // A set of custom backoff strategies keyed by name.
drainDelay: number = 5; // A timeout for when the queue is in drained state (empty waiting for jobs).
reuseProcesses: boolean = true; // Flag if true then child processes are kept for each processor, false kills child processes after finish
}
```
Expand Down
15 changes: 13 additions & 2 deletions lib/process/child-pool.js
Expand Up @@ -9,10 +9,17 @@ const ChildPool = function ChildPool() {
return new ChildPool();
}

// Flag for keeping or killing processes after finished processing, default keep always
this.reuseProcesses = true;

this.retained = {};
this.free = {};
};

ChildPool.prototype.setReuseProcesses = function(reuseProcesses) {
this.reuseProcesses = reuseProcesses;
};

ChildPool.prototype.retain = function(processFile) {
let child = this.getFree(processFile).pop();

Expand Down Expand Up @@ -42,8 +49,12 @@ ChildPool.prototype.retain = function(processFile) {
};

ChildPool.prototype.release = function(child) {
delete this.retained[child.pid];
this.getFree(child.processFile).push(child);
if (this.reuseProcesses === true) {
delete this.retained[child.pid];
this.getFree(child.processFile).push(child);
} else {
this.kill(child);
}
};

ChildPool.prototype.remove = function(child) {
Expand Down
4 changes: 3 additions & 1 deletion lib/queue.js
Expand Up @@ -213,7 +213,8 @@ const Queue = function Queue(name, url, opts) {
guardInterval: 5000,
retryProcessDelay: 5000,
drainDelay: 5,
backoffStrategies: {}
backoffStrategies: {},
reuseProcesses: true
});

this.settings.lockRenewTime =
Expand Down Expand Up @@ -673,6 +674,7 @@ Queue.prototype.setHandler = function(name, handler) {
}

this.childPool = this.childPool || require('./process/child-pool')();
this.childPool.setReuseProcesses(this.settings.reuseProcesses);

const sandbox = require('./process/sandbox');
this.handlers[name] = sandbox(handler, this.childPool).bind(this);
Expand Down
14 changes: 14 additions & 0 deletions test/test_child-pool.js
Expand Up @@ -141,4 +141,18 @@ describe('Child pool', () => {
expect(children).to.include(child);
});
});

it('should kill child after processing is finished and not retain it', function() {
var processor = __dirname + '/fixtures/fixture_processor_bar.js';

pool.setReuseProcesses(false);

return pool.retain(processor).then(function(_child) {
expect(_child).to.be.ok;
pool.release(_child);

expect(pool.retained).to.be.empty;
expect(pool.free[processor]).to.be.empty;
});
});
});

0 comments on commit 9be2b93

Please sign in to comment.