trigger.dev icon indicating copy to clipboard operation
trigger.dev copied to clipboard

onFailure isn’t called when a task is canceled

Open pangolingo opened this issue 1 year ago • 3 comments

Is your feature request related to a problem? Please describe.

I would like to keep track of the status of a task in my database. When the task starts, the DB is updated with a "STARTED" status. in the task's onFailure I update the database with a "FAILED" status. I would also like to track when a job is cancelled from the dashboard "CANCELED". I don't see a way to notice that job is canceled. When I cancel a job from the dashboard, neither the onFailure or onSuccess callbacks are called.

Describe the solution you'd like to see

I would like to see a new onCanceled() callback option when creating a task:

task({
  id: 'xyz',
  run: () => ...,
  onCanceled: (payload) => ...
})

I would also be satisfied if the onFailure callback was run when a job was canceled.

Describe alternate solutions

I've considered using the Trigger API to check on the job status to see if it was canceled. But that would required setting up a polling or callback mechanism that would be extra work to set up.

Additional information

No response

pangolingo avatar Jul 11 '24 21:07 pangolingo

Yep this 100% is a problem at the moment. I think onFailure should be called probably and there should be a flag so you can tell it was canceled. There are other failure reasons too that aren’t currently sent, basically anything that happens outside of the worker.

matt-aitken avatar Jul 11 '24 21:07 matt-aitken

Good point. I see in the docs that other statuses are FROZEN, CRASHED, INTERRUPTED, and SYSTEM_FAILURE - all of those should be handleable somehow.

pangolingo avatar Jul 11 '24 22:07 pangolingo

For CRASHED, INTERRUPTED and SYSTEM_FAILURE we can't really easily still call onFailure since those statuses only arise when we cannot actually execute your code. For example, CRASHED means the process has exited with a non-zero exit code, so the entire process no longer exists. I'm not sure it's a good idea (or even possible in some cases) to try and call onFailure in that case.

I do agree about the cancelled one though, but only if the code is already in the middle of executing.

ericallam avatar Jul 12 '24 08:07 ericallam