Laravel有尝试的队列作业,触发新尝试的正确方法?

问题描述 投票:0回答:1

我正在尝试找出执行此操作的正确方法。

public $tries = 10;

/**
 * Execute the job.
 *
 * @return void
 */
public function handle(){
    $result_of_some_logic = false;
    if($result_of_some_logic){
        // for the purpose of this example, we're all done here.
    } else{
        // we need to retry 10 minutes from now. how to trigger this attempt, and this attempt only!, to fail? 
    }
}

我阅读了laravel文档,但对我来说,目前尚不清楚正确的方法是什么。我注意到,如果创建php错误(例如,抛出新的whatisnotdeclaredinnamespace()),则作业尝试将失败,工作程序将重试,直到超过$ tries为止。这几乎是我想要的行为,但是我显然想要一个干净的代码解决方案。

总结:在Laravel 5.8中,正确标记在handle()函数中失败的尝试的正确方法是什么?

laravel queue jobs artisan
1个回答
0
投票

为什么不使用失败的函数来处理您的错误?

https://laravel.com/docs/5.8/queues#dealing-with-failed-jobs

<?php

namespace App\Jobs;

use Exception;
use App\Podcast;
use App\AudioProcessor;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;

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

    protected $podcast;

    /**
     * Create a new job instance.
     *
     * @param  Podcast  $podcast
     * @return void
     */
    public function __construct(Podcast $podcast)
    {
        $this->podcast = $podcast;
    }

    /**
     * Execute the job.
     *
     * @param  AudioProcessor  $processor
     * @return void
     */
    public function handle(AudioProcessor $processor)
    {
        // Process uploaded podcast...
    }

    /**
     * The job failed to process.
     *
     * @param  Exception  $exception
     * @return void
     */
    public function failed(Exception $exception)
    {
        // Send user notification of failure, etc...
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.