Laravel 队列为一个作业多次处理

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/48923855/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 17:26:07  来源:igfitidea点击:

Laravel queue keep processing multiple times for a job

phplaravellaravel-queue

提问by bathulah mahir

Below is what's happening when i run php artisan queue:listen and at my job table only have one job

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

enter image description here

在此处输入图片说明

and this is my code :

这是我的代码:

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

}

回答by Magus

In order for a job to leave the queue, it must reach the end of the handle function -- without errors and exceptions.

为了让作业离开队列,它必须到达句柄函数的末尾——没有错误和异常。

There must be something breaking inside one or more of your functions.

你的一个或多个函数中一定有什么东西被破坏了。

If an exception is thrown while the job is being processed, the job will automatically be released back onto the queue so it may be attempted again. https://laravel.com/docs/5.8/queues

如果在处理作业时抛出异常,作业将自动释放回队列,以便再次尝试。https://laravel.com/docs/5.8/queues

The same behavior can be achieved with

可以通过以下方式实现相同的行为

$this->release()

If you can't figure out what is breaking, you can set your job to run only once. If an error is thrown, the job will be considered failed and will be put in the failed jobs queue.

如果您无法弄清楚是什么中断了,您可以将您的作业设置为只运行一次。如果抛出错误,则作业将被视为失败并放入失败的作业队列中。

The maximum number of attempts is defined by the --triesswitch used on the queue:workArtisan command. https://laravel.com/docs/5.8/queues

最大尝试次数由Artisan 命令--tries上使用的开关定义queue:workhttps://laravel.com/docs/5.8/queues

php artisan queue:work --tries=1

If you are using the database queue, (awesome for debugging) run this command to create the failed queue table

如果您正在使用数据库队列,(非常适合调试)运行此命令来创建失败的队列表

php artisan queue:failed

Finally, to find out what is wrong with your code. You can catch and log the error.

最后,找出你的代码有什么问题。您可以捕获并记录错误。

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());
    }
}

You could also set your error log channel to be slack, bugsnag or whatever. Just be sure to check it. Please don't be offended, it's normal to screw up when dealing with laravel queues. How do you think I got here?

您还可以将错误日志通道设置为 slack、bugsnag 或其他任何内容。请务必检查它。请不要生气,处理laravel队列的时候搞砸是正常的。你觉得我是怎么来的?

回答by Adnan Mumtaz

Laravel try to run the job again and again.

Laravel 一次又一次地尝试运行该作业。

php artisan queue:work --tries=3

Upper command will only try to run the jobs 3 times.

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

Hope this helps

希望这可以帮助

回答by Amit Sharma

The solution that worked for me to delete the job after pushing them into the queue.

对我有用的解决方案是在将它们推入队列后删除作业。

Consider the e.g.

考虑例如

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
  }

}

Note: This is implemented for laravel 4.2

注意:这是为laravel 4.2 实现的

回答by José Lozano Hernandez

In my case the problem was the payload, I've created the variable private, but it needs to by protected.

在我的情况下,问题是有效载荷,我已经创建了变量private,但它需要通过protected

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;
    }
}