简体   繁体   中英

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 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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