从作业表中删除时Laravel队列死锁

时间:2019-06-16 12:46:42

标签: php laravel

我刚让Laravel Queue处理我的项目。我启用了Supervisor,因此一切都是自动化的,但是当从作业表中删除作业时,就会发生死锁。我正在使用MySQL作为驱动程序。

如何停止这些僵局?

Serialization failure: 1213 Deadlock found when trying to get lock; try restarting transaction (SQL: delete from `jobs` where `id` = 813)
[program:laravel-worker]
process_name=%(program_name)s_%(process_num)02d
command=php /home/controlapi/public_html/total-control-api/artisan queue:work database --sleep=3 --tries=3
autostart=true
autorestart=true
user=controlapi
numprocs=10
redirect_stderr=true
stdout_logfile=/home/controlapi/public_html/total-control-api/storage/logs/worker.log

编辑:以下是我与每个作业一起运行的查询

DB::transaction(function() use ($decode, $status, $c_decode, $m_decode, $d_decode) {

    try {
        $query = DB::connection('mysql2')->statement('INSERT INTO transactions (json, uuid, status, divisionName, divisionUuid, merchantName, merchantUuid, channelName, channelUuid, channelLogin, channelPwd)
                                                        VALUES (:transaction_1, :uuid, :status_1, :divisionName, :divisionUuid, :merchantName, :merchantUuid, :channelName, :channelUuid, :channelLogin, :channelPwd)
                                                        ON DUPLICATE KEY UPDATE json = :transaction_2, status = :status_2',
                                                        array('transaction_1' => $this->transaction,
                                                            'transaction_2' => $this->transaction,
                                                            'uuid' => $decode->payload->id,
                                                            'status_1' => $status->status,
                                                            'status_2' => $status->status,
                                                            'divisionName' => $d_decode->divisionInfo->name,
                                                            'divisionUuid' => $d_decode->divisionInfo->id,
                                                            'merchantName' => $m_decode->merchantInfo->name,
                                                            'merchantUuid' => $m_decode->merchantInfo->id,
                                                            'channelName' => $c_decode->channelInfo->name,
                                                            'channelUuid' => $c_decode->channelInfo->channel,
                                                            'channelLogin' => $c_decode->channelInfo->login,
                                                            'channelPwd' => $c_decode->channelInfo->pwd));
    }
    catch(Exception $e) {
        Storage::prepend('transactions_insert_errors.txt', Carbon::now('UTC')->toDateTimeString()."\nUUID:".$decode->payload->id."\n".$e->getMessage()."\n\n");
    }
}, 5);

1 个答案:

答案 0 :(得分:0)

不幸的是,对于使用多个进程的大型队列,“ 数据库”作为驱动程序不是一个好的解决方案。如果可能,请尝试减少超级用户配置中“ numprocs ”的数量(最好为1)。升级到 MySQL 8 可能也有帮助。

否则,请使用更合适的队列驱动程序:例如 Redis

查看有关此问题的讨论:https://github.com/laravel/framework/issues/7046