emit on worker completion

with test
This commit is contained in:
2016-07-08 17:14:07 -07:00
parent 362469f541
commit 057bd26b74
3 changed files with 23 additions and 0 deletions

View File

@@ -17,6 +17,7 @@ export const events = {
EVENT_JOB_CREATED: 'job:created', EVENT_JOB_CREATED: 'job:created',
EVENT_JOB_ERROR: 'job:error', EVENT_JOB_ERROR: 'job:error',
EVENT_WORKER_ERROR: 'worker:error', EVENT_WORKER_ERROR: 'worker:error',
EVENT_WORKER_COMPLETE: 'worker:job complete',
EVENT_WORKER_JOB_CLAIM_ERROR: 'worker:claim job error', EVENT_WORKER_JOB_CLAIM_ERROR: 'worker:claim job error',
EVENT_WORKER_JOB_SEARCH_ERROR: 'worker:pending jobs error', EVENT_WORKER_JOB_SEARCH_ERROR: 'worker:pending jobs error',
EVENT_WORKER_JOB_UPDATE_ERROR: 'worker:update job error', EVENT_WORKER_JOB_UPDATE_ERROR: 'worker:update job error',

View File

@@ -195,6 +195,7 @@ export default class Job extends events.EventEmitter {
version: job._version, version: job._version,
body: { doc } body: { doc }
}) })
.then(() => this.emit(constants.EVENT_WORKER_COMPLETE, Object.assign(emitJob, { output: docOutput })))
.catch((err) => { .catch((err) => {
if (err.statusCode === 409) return false; if (err.statusCode === 409) return false;
this.debug(`Failure saving job output ${job._id}`, err); this.debug(`Failure saving job output ${job._id}`, err);

View File

@@ -564,6 +564,27 @@ describe('Worker class', function () {
expect(completedTimestamp).to.be.greaterThan(startTime); expect(completedTimestamp).to.be.greaterThan(startTime);
}); });
}); });
it('should emit completion event', function (done) {
const worker = new Worker(mockQueue, 'test', noop);
worker.once(constants.EVENT_WORKER_COMPLETE, (workerJob) => {
try {
expect(workerJob).to.have.property('id');
expect(workerJob).to.have.property('index');
expect(workerJob).to.have.property('type');
expect(workerJob).to.have.property('output');
expect(workerJob.output).to.have.property('content');
expect(workerJob.output).to.have.property('content_type');
expect(workerJob).to.not.have.property('_source');
done();
} catch (e) {
done(e);
}
});
worker._performJob(job);
});
}); });
describe('worker failure', function () { describe('worker failure', function () {