如何检查 Laravel 作业失败的原因?

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

我有以下工作:

<?php

namespace App\Jobs;

use GuzzleHttp\Client;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\DB;

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

    private $mobile;
    private $code;
    private $temp;
    private $token2;
    private $token3;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($mobile, $code, $temp, $token2 = null, $token3 = null)
    {
        $this->mobile = $mobile;
        $this->code = $code;
        $this->temp = $temp;
        $this->token2 = $token2;
        $this->token3 = $token3;

        DB::table('system_logs')->insert([
         'message' => 'SendSMSJob constructor executed',
        ]);

    }

    public function handle()
    {
        try{
            $client = new Client();
            $client->request('POST','https://api.kavenegar.com/6D/vrfy/lookup.json',[
                'form_params' => [
                    'receptor' => $this->mobile,
                    'token' => $this->code,
                    'template' => $this->temp,
                    'token2' => $this->token2,
                    'token3' => $this->token3
                ]
            ]);

        } catch (\SpecificException $e) {
            DB::table('system_logs')->insert([
             'message' => $e->getMessage(),
            ]);

        }

    }


    public function failed(\Exception $e)
    {
        DB::table('system_logs')->insert([
           'message' => $e->getMessage(),
        ]);
    }

}

当作业调用时,

__construct()
也会执行,并且日志成功插入到
system_logs
表中。

但是其余代码不会被执行,

catch ()
failed()
都没有在数据库中插入任何内容。

另外,

php artisan queue:listen
的结果是:

root@mm:/var/www/api# php artisan queue:listen

   INFO  Processing jobs from the [default] queue.

  2024-09-05 13:37:51 App\Jobs\SendSMSJob ............................ RUNNING
  2024-09-05 13:37:51 App\Jobs\SendSMSJob ...................... 418.23ms FAIL  

知道如何调试并找到作业失败的原因吗?

php laravel jobs
1个回答
0
投票

尝试添加普通的 Laravel 日志来调试每个步骤

例如:

    public function handle()
{
    Log::info("Started SendSMSJob handle");
    try{
        $client = new Client();
        $client->request('POST','https://api.kavenegar.com/6D/vrfy/lookup.json',[
            'form_params' => [
                'receptor' => $this->mobile,
                'token' => $this->code,
                'template' => $this->temp,
                'token2' => $this->token2,
                'token3' => $this->token3
            ]
        ]);
        Log::info("SendSMSJob success", ["data" => [
            'form_params' => [
                'receptor' => $this->mobile,
                'token' => $this->code,
                'template' => $this->temp,
                'token2' => $this->token2,
                'token3' => $this->token3
            ]
        ]]);
    } catch (\SpecificException $e) {
        DB::table('system_logs')->insert([
         'message' => $e->getMessage(),
        ]);
        Log::info("SendSMSJob failed:", ["error" => $e->getMessage()]);
    }

}
© www.soinside.com 2019 - 2024. All rights reserved.