我在一个函数中有多个任务,我在函数中使用事务
我想为这个事务性任务使用 redis 队列
例如,我有如下功能:
private function check_destination_delivered($request,$id,$order)
{
if ($request->get('status') == 'destination_delivered')
{
$this->destination_verification($request);
DB::beginTransaction();
try
{
$this->calculate_credit($id,$order);
$this->calculate_customer_gift_credit($id,$order);
DB::commit();
}
catch (\Exception $e)
{
DB::rollback();
return $this->respondUnprocessable(1180,'error', $e);
}
}
}
在这个函数中我想要这一行
$this->destination_verification($request);
在事务开始之前和之后运行:
$this->calculate_credit($id,$order);
$this->calculate_customer_gift_credit($id,$order);
几个小时后使用redis队列进行计算,并使用事务处理其中的每个待完成任务,如果某些任务失败,则再次运行队列,直到所有待完成任务
最佳答案
我通过将函数放入队列来修复它,如下所示:
class CreditJob extends Job implements ShouldQueue
{
use InteractsWithQueue, SerializesModels;
protected $order;
protected $trip;
protected $promotion;
protected $customer;
public function __construct($order,Order $trip,Customer $customer, Promotion $promotion)
{
$this->order = $order;
$this->trip = $trip;
$this->promotion = $promotion;
$this->customer = $customer;
}
public function handle()
{
$retry=0;
$notDone=TRUE;
DB::beginTransaction();
while($notDone && $retry < 5)
{
try
{
$this->calculate_promotion($this->order);
$this->calculate_credit($this->order);
DB::commit();
$notDone=FALSE;
}
catch (\Exception $e)
{
DB::rollback();
$retry++;
sleep(30);
}
}
if($retry == 5)
{
$this->trip->fail_calculate_credit_and_promotion($this->order);
}
}
}
如果所有的任务都没有完成.in queue loop 再次运行
对吗?
关于laravel - 处理 redis 队列上的事务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41701601/