Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel Task Scheduler across multiple servers without overlap

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?

like image 938
Daniel Avatar asked Nov 02 '15 19:11

Daniel


3 Answers

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();
like image 162
shmuels Avatar answered Nov 03 '22 01:11

shmuels


One guy implement it already. check out this composer package https://packagist.org/packages/jdavidbakr/multi-server-event

like image 23
Pitipong Guntawong Avatar answered Nov 03 '22 01:11

Pitipong Guntawong


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.

like image 1
Mr. Kenneth Avatar answered Nov 03 '22 02:11

Mr. Kenneth