General developer forum

Deprecation of the legacy cron

Picture of Andrew Nicols
Deprecation of the legacy cron
Core developersMoodle HQParticularly helpful MoodlersPlugin developersTesters


As you're aware, Moodle has both a Cron system, and a Scheduled Task API.

The scheduled task API was released in Moodle 2.7, whilst the Cron has been around a very long time, and is typically referred to as a legacy system.

I'd like to propose that we begin the process of formally deprecating the legacy cron system and encouraging developers to migrate their jobs to the Task API.

This has actually been proposed for some time in MDL-52846.

What are the benefits?

The legacy cron system was designed to run tasks in serial, and it was highly discouraged to run run cron threasd in parallel. This led to issues with high load, delayed tasks, and a huge backlog of jobs. It was also practically impossible for administrators to control how frequently a job was run, or when that job might start.

The Task API breaks everything up into individual tasks. Whilst individual tasks may not run in parallel, a number of different tasks can be run in parallel. The Task API also gave administrators are far greater degree of control with the ability to entirely disable tasks, and change the frequency with which they are run. These changes mean that one long-running task (such as the sending of forum e-mails) no longer blocks other important tasks on a site, and a failing task is much less likely to prevent subsequent tasks from running.

The proposal

Moodle 3.5 is going to be our next Long Term Service (LTS) release and so marks a good point at which to start a formal deprecation process for such a core system.

The proposed change is simply to add a debugging notice when a legacy cron is called, pointing developers are the location of the documentation to update their code.

We would also find any unconverted legacy cron task in the core codebase and convert it to a new scheduled task at this time.

Why is this important?

There are a couple of reasons why I'd like us to consider this deprecation now:

  • The reason that we created the Tasks API is still an important factor. Any legacy cron function can be blocked by another other legacy cron function.

  • The scheduled task API is much more flexible than the cron API and we are extending it to give a better view for administrators such that they can spot failures more easily.
  • It would be good to have one single way of doing things to reduce confusion for all developers.

But my cron task is really small, do I have to?

The main reason to make this change is to capture all of the bigger jobs which have the potential to be fragile, or to block other jobs.

The easiest way to capture all of these is to capture all jobs using the legacy system and encourage their developers to make the changes.

What does this mean for me?

If you really want to, you can choose to ignore the message for a while more, but I'd encourage you not to.

The better answer is to convert your legacy cron task to a scheduled task.

The good news is that it's really pretty easy.

Refer to the Scheduled Task Docs for some code examples, but essentially:

Create a new file in your_plugin_directory/classes/task/taskname.php as per the example, and move your existing cron function there (or call it from there) Create a new file in your_plugin_directory/db/tasks.php containing the name of your new task, and its scheduled run times. Add any new language strings to your language file. Remove the old cron timing in your version.php


Average of ratings: Useful (8)
Tim at Lone Pine Koala Sanctuary
Re: Deprecation of the legacy cron
Core developersDocumentation writersParticularly helpful MoodlersPlugin developers

+1 The time has come to do this. Thank you for moving it forwards.

Average of ratings: -
Picture of Michael Aherne
Re: Deprecation of the legacy cron
Core developersParticularly helpful MoodlersPlugin developers

Great idea! The legacy cron system causes extra complexity when troubleshooting issues and I for one would be happy to see it removed in 3.5.

Average of ratings: -
Picture of sam marshall
Re: Deprecation of the legacy cron
Core developersParticularly helpful MoodlersPlugin developersTesters

Agree, I'd definitely like it deprecated. The new system is much better especially for troubleshooting live problems (since it's so easy to disable a task); anything left in legacy cron is basically a liability.


Average of ratings: -