我在一个函数中有多个任务,我在函数中使用事务。
我希望将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队列计算几个小时后,对要完成的每个任务使用事务,如果某些任务失败,.queue将再次运行,直到所有要完成的任务
发布于 2017-01-19 15:24:22
我通过在队列中放置函数来修复它,如下所示:
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队列循环,则再次运行
是对的吗?
https://stackoverflow.com/questions/41701601
复制相似问题