run webman job schedule on kubernetes

lee

Hi everyone, I am from Thailand
I start using webman with kuberbetes. it will run on multiple server (pods) at the same time
I then have cron task setup (workerman/crontab) like this

        new Crontab('0 * * * * *', function(){
            \Webman\RedisQueue\Redis::send('SendCartRemiderAfter1Hour', []);
        });

how to control or make sure it run only on one pod?

767 1 0
1个回答

six

Hello leekung.

The following is my plan.
Add a file mark the server which crontab needs to be run. Code similar

if (is_file(config_path() . '/crontab.lock')) {
    new Crontab('0 * * * * *', function(){
        \Webman\RedisQueue\Redis::send('SendCartRemiderAfter1Hour', []);
    });
}

Remember file config/crontab.lock do not push to git (add config/crontab.lock to .gitignore).
File config/crontab.lock only exists on the server which running crontab.

  • lee 2022-11-09

    Thanks, I have got the idea with lock file but it is redis lock key instead.
    the Crontab is left as it is with no change

    I will change the consumer like this (borrow from Laravel onOneServer())

    class SendCartRemiderAfter1Hour implements Consumer
    {
        public $queue = 'SendCartRemiderAfter1Hour';
        public $connection = 'default';
    
        public function consume($data)
        {
            $should_run = $this->should_run();
            if (!$should_run) {
                return;
            }
    
            // Do my schedule job
            // ...
    
            $this->run_finished();
        }
    
        public function lock_key()
        {
            $this->startedAt = Date::now();
            $key = "schedule-{$this->connection}:{$this->queue}";
            return $key.$this->startedAt->format('Hi');
        }
    
        public function should_run()
        {
            return \Shopwwi\LaravelCache\Cache::add($this->lock_key(), true, 3600); // return true/false
        }
    
        public function run_finished()
        {
            \Shopwwi\LaravelCache\Cache::delete($this->lock_key());
        }
    }
年代过于久远,无法发表回答
×
🔝