skip to Main Content

I have the following Job:

<?php

namespace AppJobs;

use GuzzleHttpClient;
use IlluminateBusQueueable;
use IlluminateContractsQueueShouldQueue;
use IlluminateFoundationBusDispatchable;
use IlluminateQueueInteractsWithQueue;
use IlluminateQueueSerializesModels;
use IlluminateSupportFacadesDB;

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(),
        ]);
    }

}

When the job calls, the __construct() executes as well and the log successfully inserts inside the system_logs table.

But the rest of the code doesn’t get executed, neither the catch () nor failed() insert nothing inside the database.

Also, the result of php artisan queue:listen is:

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

   INFO  Processing jobs from the [default] queue.

  2024-09-05 13:37:51 AppJobsSendSMSJob ............................ RUNNING
  2024-09-05 13:37:51 AppJobsSendSMSJob ...................... 418.23ms FAIL  

Any idea how can I debug and find the reason why the job fails?

2

Answers


  1. Try adding normal laravel log to debug each step

    for example:

        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()]);
        }
    
    }
    
    Login or Signup to reply.
  2. There are a few discussion points with this job.

    First, that SpecificException exception did not happen. Because if it had, that catch block would have been executed, and that job should be successful.

    Second, logging to DB is very unconventional. What about logging to files, or one of those channels?

    Now to debug that job:

    1. Create the failed_jobs table.
    2. Remove the try catch block.
    3. If possible, stop logging to DB. In the failed method and the constructor, just log to files with Log::info and Log::error().
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search