为什么Laravel队列中的作业以状态200

问题描述

我有一个Laravel应用程序,一个进程将一份工作放在laravel队列上。尝试执行异步发布http请求的作业。当请求失败时,我会毫无问题地捕获异常,但是当我的状态为200时,应用程序会记录响应,但几秒钟后,尝试再次执行请求。

我的请求:

 class NotifyPayment implements ShouldQueue {
    
     /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct(SettlementModel $settlement) {
        $this->settlement = $settlement;
    }

     /**
     * Execute the job.
     *
     * @return void
     */
    public function handle() {

        $settlement = $this->settlement;

        $id = $settlement->id;
        Log::info("Executing job: ".$settlement->id);
        $client = new Client(); 
        try {
            
            $promise = $client->requestAsync('POST',$settlement->siam_response_url);
            $promise->then(
                function (ResponseInterface $res) use ($id) {
                    Log::info('Status:'.$res->getStatusCode().',NOTIFY SUCCESS: '.$id.'
                    ');
                }
            );
            $promise->wait();
        }
        catch (RequestException $e)
        {   
            Log::error("NOTIFY WAS Failed: ID ".$settlement->id." ".$e->getMessage());
            Log::error( $e->getRequest()->getmethod()." ".$e->getMessage() . "\n");
            $this->job->fail(null);
        }


    }
}

这是我的日志文件

[2020-09-10 12:21:16] production.INFO:已支付的定居点:22638

[2020-09-10 12:21:17] production.INFO:执行作业:23423

[2020-09-10 12:21:18] production.INFO:状态:200,通知成功:22638

[2020-09-10 12:23:02] production.INFO:执行作业:23423

[2020-09-10 12:23:03]生产。错误通知失败:ID 22638客户端错误POST https://some-url-of-my-system/callback-paid?token=8832fa192f059598e064b905导致400 Bad Request响应

[2020-09-10 12:23:03]生产。错误:POST客户端错误POST https://some-url-of-my-system/callback-paid?token=8832fa192f059598e064b90524dc018ad1331f763e5c45ba21470e0112645f4f22dbb703468998e22cfcb4fe6ce328db9a730e900bbb75757aba3a97210810ef导致400 Bad Request响应

使用此日志

Log::info("Executing job: ".$settlement->id); 

我可以看到作业执行了两次,这是怎么回事?

在此处发出通知的地方:

NotifyPayment::dispatch($settlement);

PD:帖子请求的网址只能执行一次,第二次抛出状态400。

重要信息

当我的流程开始时,它就是从该路由/cashier开始的,并且当该流程完成所有步骤,运行作业队列等之后,以相同的路由/cashier重定向

return redirect()
            ->route('cashier')
            ->withMessages(['type' => 'success','text' => 'The payment was successful!!!']);

解决方法

当我的进程开始时,它是从此路由/ cashier开始的,并且当该进程执行了所有步骤,运行作业队列等后,在同一路由/ cashier上重定向

仅更改了我到队列上的作业的重定向路由,仅运行了一次。 我不知道为什么当您从一条路线开始时该作业运行两次,并在完成处理后转到同一条路线。

解决方案:

更改此

return redirect()
            ->route('cashier')
            ->withMessages(['type' => 'success','text' => 'The payment was successful!']);

对此:

return redirect()
            ->route('cashier.details') //to any other route from you app
            ->withMessages(['type' => 'success','text' => 'The payment was successful!']);