简体   繁体   English

跨链接队列的Laravel数据库事务(Horizo​​n / Redis)

[英]Laravel database transactions across chained queues (Horizon/Redis)

I have searched for hours on this and am thinking its maybe simply not possible? 我已经搜索了几个小时,我想它可能根本不可能? Is If a job in my queue chain fails, can I rollback all database transactions that have occurred to this point. 如果队列链中的作业失败,我是否可以回滚此时发生的所有数据库事务。

DB::transaction(function () {
   ProcessPodcast::withChain([
       new OptimizePodcast,
       new ReleasePodcast
   ])->dispatch();
});

note: I know the above will not work when asynchronously pushing jobs to the queue, but is there a way to get this effect? 注意:我知道当异步将作业推送到队列时,上述操作无效,但有没有办法获得此效果?

Since queue workers are long-lived processes it is possible to utilize database transactions in chained queue jobs. 由于队列工作者是长期存在的进程,因此可以在链式队列作业中利用数据库事务。

What you need to do is to begin a transaction on ProcessPodcast job's handle method, while making sure you rollback if the chain fails. 您需要做的是在ProcessPodcast作业的handle方法上开始一个事务,同时确保在链失败时回滚

// ProcessPodcast parent Job

use DB; // above class, of course

public function handle()
{
    DB::beginTransaction();
}

public function failed(Exception $exception)
{
    DB::rollback();
    // Send user notification of failure, etc...
}

You can commit your transaction in any of the chained jobs. 您可以在任何链接作业中提交事务。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM