grantcarthew / node-rethinkdb-job-queue

A persistent job or task queue backed by RethinkDB.
https://github.com/grantcarthew/node-rethinkdb-job-queue/wiki
MIT License
156 stars 16 forks source link

Cancelling long-running jobs #6

Closed TomKaltz closed 8 years ago

TomKaltz commented 8 years ago

Lets say I have a video transcoding job that is running/active. What is the intended behavior If I use cancelJob() for that jobId? If I call next(result) from my job processor after the job is cancelled will the result be ignored?

Is there a way a job can be notified of the cancellation so it can cleanup/shutdown?

grantcarthew commented 8 years ago

Hi @TomKaltz,

There are two ways to cancel a job; call Queue.cancelJob, or by calling next() with an error hosting a custom property.

In your scenario above you would want to use the second cancel option and pass an error object to the next() function call.

If you used the Queue.cancelJob() and then called next(), you would still end up with the job log being populated with the result.

It is an interesting order of events which I had not thought of.

With regards to cancelling a job that is being processed by another Queue object, at the moment there is no algorithm to listen to cancel events and gracefully shutdown the job.

It could be done in your code by listening to the cancelled event, check the cancelled job id against the running job and close it then.

I don't suppose it would be too hard to add this feature. It would just be a matter of listening to the cancelled event when a job is being processed. Once cancelled a user supplied callback could be run.

What are your thoughts?

TomKaltz commented 8 years ago

Yeah I was thinking external cancallation of long-running jobs would be really nice. Perhaps...

q.process((job, next, onCancel) => {})

...would be a nice option to allow users specify how to cleanup should occur if the job was cancelled from the outside. My use case would be video converting. I stole this idea from Bluebird cancellation feature.

grantcarthew commented 8 years ago

OK, I agree. I'll look into it. Thanks mate.

grantcarthew commented 8 years ago

New v0.2.0 supports global job cancellation. It is using the signature you have above with a slight change on the Bluebird version due to the design. You need to pass the job to the onCancel function.


q.process((job, next, onCancel) => {
  onCancel(job, () => {
    // Cancel your processing here.
  })

I'll update the documentation today sometime. Thanks for the suggestion @TomKaltz.