Is it possible to control the failure behaviour of scheduled tasks?

Is it possible to control the failure behaviour of scheduled tasks?

by Tim Hunt -
Number of replies: 3
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

With scheduled tasks, it seems to be hard-coded that if your task fails, it get re-tried after 1, 2, 4, 8, 16, ... minutes.

What if that is not the correct logic for your task? Has anyone else hit this, and how did you handle it?

I can see three options:

  1. Pretend that your task succeeded (e.g. manually catch the exception). This way you lose the standard admin UI showing you what has failed.
  2. Fix the core scheduled tasks system, so there is a propery way to configure the retry schedule when necessary.
  3. Hack it: Override get_fail_delay for your task to return half the failure delay you want.


Average of ratings: -
In reply to Tim Hunt

Re: Is it possible to control the failure behaviour of scheduled tasks?

by James Brisland -

Another question....

If the tasks fails, can you control it so you throw an exception and see the error, but it doesn't get run again until the next scheduled time?

In reply to Tim Hunt

Re: Is it possible to control the failure behaviour of scheduled tasks?

by Dan Poltawski -

Well, why is ensuring you task gets run succesfully not the correct logic for your task? What is the use case?

I see it the job of the scheduled tasks system to run tasks and it works on the basis those tasks are expected to succeed. Imagine someone does nagios monitoring for scheduled tasks which fail and they install your plugin which has a task which is 'expected' to fail intermittently - thats going to make for unhappy sysadmins.

1. Its not really pretending, if the task you are defining is actually 'maybe do this if you can' rather than 'definitely make sure this is done', then that logic can be defined by you in your task code. I don't think it makes sense to define tasks which are expected to fail intermittently. 

2/3. I don't see why you count overriding get_fail_delay() as a hack, that seems like a perfectly reasonable way to override this behaviour really.

In reply to Dan Poltawski

Re: Is it possible to control the failure behaviour of scheduled tasks?

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

2. It is a hack because get_fail_delay is really get_previous_fail_delay, and is meant to return the delay that lead to the previous invocatoin of the task, if this was the second or later failure in a row. It is not get_suggested_fail_delay_for_this_task.

Hence the need to return have the number you want, because the system is going to double it, or use 60 if null is returned.