Laravel队列为作业多次处理

时间:2018-02-22 09:32:33

标签: laravel laravel-queue

以下是我运行php artisan队列时发生的事情:听,在我的工作表上只有一份工作

enter image description here

这是我的代码:

public function handle(Xero $xero)
{
        $this->getAndCreateXeroSnapshotID();
        $this->importInvoices($xero);
        $this->importBankTransaction($xero);
        $this->importBankStatement($xero); 
        $this->importBalanceSheet($xero);
        $this->importProfitAndLoss($xero);

}

4 个答案:

答案 0 :(得分:11)

为了让一个作业离开队列,它必须到达句柄功能的末尾,没有错误或例外。

如果你的任何一个函数内部出现了问题,那么该作业会假定它有一个临时问题并被放回队列中。

使用

可以实现相同的行为
$this->release()

如果你无法弄清楚什么是破坏你可以将你的工作设置为只运行一次并被视为失败,那么他将被置于失败的工作队列中

php artisan queue:work --tries=1

如果您正在使用数据库队列(很棒的调试),请运行此命令以创建失败的队列表

php artisan queue:failed

最后找出你的代码有什么问题

public function handle(Xero $xero)
{
    try{
        $this->getAndCreateXeroSnapshotID();
        $this->importInvoices($xero);
        $this->importBankTransaction($xero);
        $this->importBankStatement($xero); 
        $this->importBalanceSheet($xero);
        $this->importProfitAndLoss($xero);
    }catch(\Exception $e){
        Log::error($e->getMessage());
    }
}

您还可以将错误日志通道设置为松弛,错误或其他。一定要检查一下。请不要被冒犯,在处理laravel队列时这样搞砸是正常的,你觉得我怎么来到这里?

答案 1 :(得分:2)

Laravel试图一次又一次地运行这份工作。

php artisan queue:work --tries=3

上层命令只会尝试运行3次。

希望这有帮助

答案 2 :(得分:1)

解决了将作业推入队列后删除作业的解决方案。

考虑例如。

class SomeController extends Controller{
  public function uploadProductCsv(){
   //process file here and push the code inot Queue
   Queue::push('SomeController@processFile', $someDataArray); 
  }

  public function processFile($job, $data){
    //logic to process the data
    $job->delete(); //after complete the process delete the job
  }

}

注意:这是为laravel 4.2

实现的

答案 3 :(得分:1)

在我的情况下,问题是有效负载,我创建了变量 private ,但需要对其进行保护

class EventJob implements ShouldQueue
{       
    use InteractsWithQueue, Queueable, SerializesModels;

    // payload
    protected $command;
    // Maximum tries of this event
    public $tries = 5;

    public function __construct(CommandInterface $command)
    {
        $this->command = $command;
    }

    public function handle()
    {
        $event = I_Event::create([
            'event_type_id' => $command->getEventTypeId(),
            'sender_url' => $command->getSenderUrl(),
            'sender_ip' => $command->getSenderIp()
        ]);

        return $event;
    }
}
相关问题