Cron tasks

Recurring jobs on SilverStripe Platform are managed in one of two ways - the CronTask module, or as a server-defined cronjob.

Using a cron task

As part of using SilverStripe Platform, you should already have the silverstripe/crontask module installed. The dev/cron cron task is automatically set-up on your stack and utilises sera in order to ensure that only one server will run the task at any one time, regardless of how many servers are in-use on the current environment. This is important to note if you are migrating from a non-distributed system. See the CronTask docs for guidance on creating a new CronTask.

Using a cron job

Starting with SilverStripe Platform version 3.3.0, you are able to define cronjobs in your .platform.yml file like so:

crons:
  every_server_task:
    time: "0 20 * * *"
    sake_all: "dev/tasks/MyTask"
    vhost: "mysite"
  try_for_the_lock_task:
    time: "1 21 * * *"
    sake_once: "dev/tasks/MySecondTask"
    vhost: "mysite"

In the example above, the first task will be run once a day, and on each server in the environment because it uses sake_all. In some cases this may be one server; in others, up to 16. Make sure you take this into consideration when adding a cronjob to your .platform.yml file, as running the same task from multiple boxes at the same time can cause race conditions and other issues.

The second task will also be run once a day, but only on at most one server because it uses sake_once.

The cron always runs as the web user www-data. You can read more about cronjobs in the .platform.yml documentation

This cron syntax will work for both SilverStripe 3 and SilverStripe 4 projects.

When using sake_once it is normal to see logs such as Timeout out trying to acquire the lock. each time the task runs. This message is logged because the task is triggered on both servers, but sake_once sets up a lock which only one server can acquire, any other servers which cannot acquire this lock will log the timeout message. If you see these errors coming from all servers in the load balance pool it means that the task is taking longer to run than the time available between runs. In that case you should consider decreasing the frequency your task runs at or improving the speed of your task.

Please note that this is a more advanced option for configuration - if you have any questions at all, please don’t hesitate to contact the support team.