laravel - 处理 redis 队列上的事务

标签 laravel transactions redis queue laravel-5.2

我在一个函数中有多个任务,我在函数中使用事务
我想为这个事务性任务使用 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/

相关文章:

linux - Redisused_memory_rss 大于配置集 'maxmemory' 吗?

jquery - 如何更改字体很棒的图标和按钮类 onclick?

PHP MVC 框架 Laravel 似乎出错 Artisan Serve

laravel - 使用 TDD 构建 Laravel 应用程序 - 第 4 集,InvalidArgumentException : Unable to locate factory with name [default] [App\Project]

laravel - vue-i18n 输出用作类星体数据表标题值

php - Codeigniter 3 个连续事务不起作用

java - @transactional带注释的类,使用代理包装,但未创建事务

java - Jedis 永远阻止与亚马逊的连接

c# - 在 C# 中使用文件的 TransactionScope

java - 是否有某种具有空闲 TTL 的锁/信号量