Laravel Schedule on a Distributed Application

My application runs on 3 servers behind a load balancer. Therefore, it is stateless, all data is stored in redis and MySQL.

If my machines work with the cron scheduler, I assume that the same task will be performed 3 times. Once on each machine, since there is no knowledge of what was performed in conjunction with them, for example, a database table.

What are the solutions?

+4
source share
3 answers

edit: In recent versions of Laravel, this is now integrated .

$schedule->command('foo:bar')->onOneServer();

We do this in Artisan commands, which should be executed on only one server per cron execution:

public function handle()
    if(!Cache::add(get_class($this), true, 0.5)) {
        return false;
    }

Cache::add false, , , .

cron cron.

+4

redis- , , , .

. :

if(redis.get("LEADER").equals(currentappserver))
process();
else if(redis.get("LEADER")==null && selectLeader()) -> a lua script call.
process();

redis , , , lua script:

selectLeader()
if redis.get("LEADER")==null
redis.set("LEADER",current_node)
return true;
else
return false;

, .

P.s: , , - vauge, .

+2

Laravel 5.4 supports multi-server settings using a scheduler if updating is an option.

It uses a similar method as the accepted answer of ceejayoz.

Instead of using the file system to store the lock file, we use cache storage so that any server can block / check for scheduled events.

https://github.com/laravel/internals/issues/69

0
source

All Articles