Laravel:如何在失败的情况下编程运行作业5次?

zi8p0yeb  于 2023-06-07  发布在  其他
关注(0)|答案(2)|浏览(133)

这是作业的代码,如果失败,我需要在5秒后运行作业5次。在深入代码之前,$this->attempts()总是返回1,所以我没有在代码中包含它。

<?php

namespace Modules\Order\Jobs;

use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Modules\Order\Entities\Order;
use Modules\User\Entities\User;

class CreditPurchase implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    private $order;
    private $user;
    private $paid_at;

    public function __construct(Order $order, User $user, $paid_at)
    {
        $this->order = $order;
        $this->user = $user;
        $this->paid_at = $paid_at;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        try {
            $t = 4 / 0;

        } catch (\Throwable $exception) {
            $this->fail();
            $this->delete();
            $this->release(5);
        }
    }
}

但我得到了这个错误:
[2021 - 01 - 20 23:16:09]本地。错误:SQLSTATE [23000]:违反完整性约束:1062关键字“failed_jobs_uuid_unique”的重复条目“014c3080 - 3ee3 - 4198 - 946c-dfe1d8d858a7”(SQL:插入failed_jobsuuidconnectionqueuepayloadexceptionfailed_at)values(014c3080 - 3ee3 - 4198 - 946c-dfe1d8d858a7,redis,default,{"uuid ":"014c3080 - 3ee3 - 4198 - 946c-dfe1d8d858a7","超时":null,"id":" HDww70Nm2TmRcmeO5cqXN8qrxbvzBfDq ","回退":null," displayName ":"Modules\Order\Jobs\CreditPurchase","maxTries":null,"maxExceptions":null,"retryUntil":null,"data":{"command ":" O:33:"Modules\Order\Jobs\CreditPurchase":十三:{s:40:"\u0000模块\Order\Jobs\CreditPurchase\u0000order "; O:45:"Illuminate\Contracts\Database\ModelIdentifier":4:{s:5:"class"; s:28:"模块\订单\实体\订单"; s:2:"id"; i:1; s:9:"relations"; a:1:{i:0; s:5:"items";} s:10:"connection"; s:"mysql";} s:39:"\u0000Modules\Order\Jobs\CreditPurchase\u0000user"; O:45:"Illuminate\Contracts\Database\ModelIdentifier":4:{s:5:"class"; s:26:"模块\用户\实体\用户"; s:2:"id"; i:1; s:9:"relations"; a:0:{} s:10:"连接"; s:"mysql";} s:42:"\u0000Modules\Order\Jobs\CreditPurchase\u0000paid_at"; O:13:"Carbon\Carbon":3:{s:4:"date"; s:26:"2021 - 01 - 20 23:16:00.344324"; s:13:"时区类型"; i:3; s:8:"时区"; s:3:"UTC";} s:3:"job"; N; s:10:"连接"; N; s:5:"队列"; N; s:15:"chainConnection"; N; s:10:"chainQueue"; N; s:19:"chainCatchCallback"; N; s:5:"delay"; i:2; s:11:"afterCommit"; n; s:10:"中间件"; a:0:{} s:7:"链接"; a:0:{}}"," commandName ":" Modules\Order\Jobs\CreditPurchase "},"job":"Illuminate\Queue\CallQueuedHandler@call","attempts":1},Illuminate\Queue\MaxAttemptsExceedException:Modules\Order\Jobs\CreditPurchase尝试次数过多或运行时间过长。该作业以前可能已超时。在C:\xampp\htdocs\avakala\vendor\laravel\framework\src\Illuminate\Queue\Worker. php:713

5vf7fwbs

5vf7fwbs1#

Jobpublic $tries属性对您有帮助吗?

class CreditPurchase implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    public $tries = 5;

    //
}

我们也可以使用基于时间的尝试:

public function retryUntil()
{
    return now()->addSeconds(1);
}
wixjitnu

wixjitnu2#

另一种方法可能是告诉队列工作线程重试它的所有作业5次

php artisan queue:work --tries=5

public $tries = 5public function retryUntil()将优先于命令行中提供的--tries值,这是一种更细粒度的方法

相关问题