Created by: scinos
This PR makes a Job an EventEmitter, so it can trigger events too. I implemented two events, 'completed' and 'failed'. These events are triggered after the queue's events, because there is little value in doing it before: in that case, you could as well run the code handling the event in the Job processor.
Our use case:
We have a WebServer that creates jobs, and a Worker that picks those jobs, generates the output, and then has to do some expensive cleanup before accepting the next job. We can control it with pause
/resume
, but it is hard to schedule the cleanup and not interfere with the "main loop". For example, if we use setTimeout
inside the processor to schedule the clean up, it might end up running before the job is marked as done in Redis, delaying the whole process.
Having a event we can listen to that signals "the job has been processed and handled by the queue" is a great solution for us.