I am wondering if anyone has any experience with making the Laravel Task Scheduler work without overlapping across multiple servers.
To put it in context: I have 3 API servers behind a load balancer. I know the task scheduler allows for the withoutOverlap flag on a single server, but how could I use that same principal across all of my servers? I want only 1 server to run the scheduled tasks and have the other 2 servers not run them.
As far as I can tell, my options are:
Remove the crontab from 2 of the servers. I have my servers automated and would rather not remove the crontab job from any of the servers because that would complicate my automated builds.
Write a package that uses the DB to limit the tasks to only the first server that runs. I could do this, but am wondering if it has already been done (cannot find anything on google)
Any advice?
This feature is available out of the box as of Laravel 5.6+.
Per the docs you would implement it with the onOneServer
function, like so...
$schedule->command('report:generate')
->fridays()
->at('17:00')
->onOneServer();
One guy implement it already. check out this composer package https://packagist.org/packages/jdavidbakr/multi-server-event
How about add a variable on .env file (SERVER=1; SERVER=2; SERVER=3)
then on Kernel.php, just add a condition if (env('SERVER') == 1) {}
. Simple but it works.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With