Laravel 5.6 Preview — Single Server Scheduling
To demonstrate the new feature, let’s look at an example scheduled job definition:
In this example, the
inspire Artisan command will run every hour; however, the command will not run if the previous iteration of the command has not finished executing within the hour (that’s a lot of inspiration!).
However, one caveat to this approach is that the scheduled job will execute on every server your application is running on. So, for example, if you have this application deployed to two web servers and two worker servers the command will run on all four servers. Sometimes, especially when generating reports or cleaning up data, you only need the command to run on a single server. Thanks to Laurence’s contribution to Laravel 5.6, it’s now a cinch:
As you can see, all we need to do is add the
onOneServer directive to our scheduled job definition. Now the job will run hourly on one of our servers!
Note: When using this feature, you will need to use the Redis or Memcached cache drivers. These drivers provide the atomicity needed to secure the locks that power this feature.
I’m really happy to see this contribution because this is a pain point I have experienced myself and I *love* getting rid of those little pain points. Thanks Laurence!
Laravel 5.6 is scheduled for release in February 2018.