withoutOverlapping()使用redis缓存,因故障导致对应的定时任务不被执行的问题处理

使用了laravel定时任务中的 withoutOverlapping() 特性引发了一个bug,导致某个定时任务被无故终止。

$schedule->command('behavior:behavior_stat_daily')->withoutOverlapping()->everyTenMinutes();

查询crontab执行日志发现某个时间点因为redis内网闪断链接故障,导致key未被释放。

No scheduled commands are ready to run.
No scheduled commands are ready to run.
Running scheduled command: '/usr/bin/php' artisan behavior:behavior_stat_daily > '/dev/null' 2>&1

In AbstractConnection.php line 155:
                                                                               
  Error while reading line from the server. [tcp://r-b******64.redis.rds.aliyuncs.com:6379]                                                        
                                                                               

No scheduled commands are ready to run.
No scheduled commands are ready to run.

但是因为使用了redis缓存,所以一时间找不到定时任务的使用了哪个缓存互斥锁的key。

这个是调度器中对应的代码

/**
* Do not allow the event to overlap each other.
*
* @param  int  $expiresAt
* @return $this
*/
public function withoutOverlapping($expiresAt = 1440)
{
$this->withoutOverlapping = true;

$this->expiresAt = $expiresAt;

return $this->then(function () {
    $this->mutex->forget($this);
})->skip(function () {
    return $this->mutex->exists($this);
});
}

后来在本地观察了一下新生成的key,键值名称为

laravel:framework/schedule-.......

这就好办了,上redis,用前缀搜索找到这个key删除就行了。

你可能感兴趣的:(PHP,laravel,缓存,redis,数据库)