首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >处理redis队列上的事务

处理redis队列上的事务
EN

Stack Overflow用户
提问于 2017-01-17 15:56:33
回答 1查看 412关注 0票数 0

我在一个函数中有多个任务,我在函数中使用事务。

我希望将redis队列用于此事务性任务。

例如,我有一个函数如下所示:

代码语言:javascript
复制
 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);
      }
    }
}

在这个函数中,我想要这一行

代码语言:javascript
复制
$this->destination_verification($request);

在事务开始之前运行,然后运行以下行:

代码语言:javascript
复制
    $this->calculate_credit($id,$order);
    $this->calculate_customer_gift_credit($id,$order);

使用redis队列计算几个小时后,对要完成的每个任务使用事务,如果某些任务失败,.queue将再次运行,直到所有要完成的任务

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-01-19 15:24:22

我通过在队列中放置函数来修复它,如下所示:

代码语言:javascript
复制
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队列循环,则再次运行

是对的吗?

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41701601

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档