Redis 限制 Laravel 作业“尝试次数过多或运行时间过长”

我想使用 Laravel Queue 和 Redis 来通过 nexmo 发送短信,但有些作业失败了,我不明白为什么。在我的本地环境中,我每 10 秒发送 1 条短信,所以这是我工作的处理方法:


public function handle()

    { 

        Redis::throttle('throttle:sms')->allow(1)->every(10)->then(function(){

            Log::info($this->notifId." start : ".date('H:m:s'));

            try{

                $before = microtime(true);

                $response = Nexmo::message()->send([

                                'to'   => $this->to,

                                'from' => '16105552344',

                                'text' => $this->message

                            ]);

                $notif = NotificationHistory::find($this->notifId);

                $notif->nexmo_message_id=$response->getMessageId();

                $notif->save();


                $after = microtime(true);

                Log::info(($after-$before)." sec\n");

            }catch(Exeption $e){

                log::warning($e);

            }

        },function($error){

            Log::error($error);

            return $this->release(10);//release job in X second

        });

    }

但当我达到尝试限制时,我Illuminate\Contracts\Redis\LimiterTimeoutException终于得到了一些。MaxAttemptsExceededException


我开始我的工作人员php artisan queue:work --queue=sms --timeout=60 并像这样调度我的工作:


foreach($someEntities as $entitiy) {

            $notif = new NotificationHistory();

            $notif->notifiable()->associate($entity);

            $notif->message=$entity->message;

            $notif->status=NotificationHistory::$statusList[-1];

            $notif->save();

            dispatch(new SendSMS($entity->message."_".$notif->id,$entity->phone,$notif->id))->onConnection('redis')->onQueue('sms');

    }

当尝试发送 8 条短信时,前 5 条或 6 条有效,但其他短信例外。


编辑 我在没有 nexmo 的情况下得到同样的错误:


public function handle()

    { 

        Redis::throttle('throttle:sms')->allow(1)->every(10)->then(function(){

            Log::info($this->notifId." startAt : ".date('H:m:s'));

        },function($error){

            Log::error($error);

            return $this->release(10);//release job in X second

        });

    }


MMTTMM
浏览 48回答 1
1回答

Qyouu

问题是“horizon”文件强制尝试次数为 3。事实上,队列工作器并不只每 10 秒运行一次,如果队列中有东西,它会连续运行。当它处理一个作业时,如果它在过去 10 秒内已经处理过一个作业,它将从队列中删除当前作业并在 X 秒内将其放回(在 中定义),生成并增加它的 attemptsrelease(x)值LimiterTimeoutException。因此,当工人对同一个工作进行 3 次尝试时,就会失败。
打开App,查看更多内容
随时随地看视频慕课网APP