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.
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