Merge pull request #13 from w33ble/fix/worker-destroy

Worker destroy() will stops job polling
This commit is contained in:
Joe Fleming
2016-12-16 16:57:52 -07:00
committed by GitHub
2 changed files with 28 additions and 1 deletions

View File

@@ -35,12 +35,14 @@ export default class Worker extends events.EventEmitter {
this.debug = (...msg) => debug(...msg, `id: ${this.id}`); this.debug = (...msg) => debug(...msg, `id: ${this.id}`);
this._checker = false; this._checker = false;
this._running = true;
this.debug(`Created worker for job type ${this.jobtype}`); this.debug(`Created worker for job type ${this.jobtype}`);
this._startJobPolling(); this._startJobPolling();
} }
destroy() { destroy() {
clearInterval(this._checker); this._running = false;
this._stopJobPolling();
} }
toJSON() { toJSON() {
@@ -237,6 +239,10 @@ export default class Worker extends events.EventEmitter {
} }
_startJobPolling() { _startJobPolling() {
if (!this._running) {
return;
}
this._checker = setInterval(() => { this._checker = setInterval(() => {
this._getPendingJobs() this._getPendingJobs()
.then((jobs) => this._claimPendingJobs(jobs)); .then((jobs) => this._claimPendingJobs(jobs));

View File

@@ -185,6 +185,27 @@ describe('Worker class', function () {
clock.tick(interval); clock.tick(interval);
sinon.assert.calledOnce(searchSpy); sinon.assert.calledOnce(searchSpy);
}); });
it('should not poll once destroyed', function () {
const worker = new Worker(mockQueue, 'test', noop);
// move the clock a couple times, test for searches each time
sinon.assert.notCalled(searchSpy);
clock.tick(defaults.interval);
sinon.assert.calledOnce(searchSpy);
clock.tick(defaults.interval);
sinon.assert.calledTwice(searchSpy);
// destroy the worker, move the clock, make sure another search doesn't happen
worker.destroy();
clock.tick(defaults.interval);
sinon.assert.calledTwice(searchSpy);
// manually call job poller, move the clock, make sure another search doesn't happen
worker._startJobPolling();
clock.tick(defaults.interval);
sinon.assert.calledTwice(searchSpy);
});
}); });
describe('query for pending jobs', function () { describe('query for pending jobs', function () {