Home > Blockchain >  Why Laravel Jobs console returns failed but email is sent
Why Laravel Jobs console returns failed but email is sent

Time:02-17

I don't understand why my php artisan queue:work command in the console returns failed but the email is sent and only the 1st function call is being fired.

public function handle()
{
    //1st email
    $this->sendto_client($this->request,$this->totalprodprice,$this->quotationprice,$this->featitem,$this->quoteid);

    //2nd email
    $this->sendto_branches($this->request,$this->totalprodprice,$this->quotationprice,$this->featitem,$this->quoteid);
}

Here is my dispatch method:

$job1 = new SendQuoteEmail(collect($request),$totalprodprice,$quotationprice,$featitem,$quoteid,"client");
$this->dispatch($job1);

Here are some console log.

[2022-02-15 23:50:04][28] Processing: App\Jobs\SendQuoteEmail
[2022-02-15 23:50:08][28] Failed:     App\Jobs\SendQuoteEmail

CodePudding user response:

You are getting job is failed even though mail is sent because the code that you have written after Mail triggering is throwing the exception

For Example lets consider the following Job Class

<?php

namespace App\Jobs;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;

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

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

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

        logger()->info('My Log message before Exception');

        //Your other logic

        throw new \Exception("Message is Logged before the Exception but the job is failed", 1);

        //code below the exception won't work

        logger()->info('My Log message after Exception');
        
    }
}

After disptaching the Job FailedJobWithPartialSuccessJob::dispatch() like this and staring the worker here is my cli output

cli-outout-job-processing

And here is the log file contents

[2022-02-16 05:00:21] local.INFO: My Log message before Exception  
[2022-02-16 05:00:21] local.ERROR: Message is Logged before the Exception but the job is failed {"exception":"[object] (Exception(code: 1): Message is Logged before the Exception but the job is failed at C:\\xampp\\htdocs\\serverinventory\\app\\Jobs\\FailedJobWithPartialSuccessJob.php:39)
[stacktrace]
#0 C:\\xampp\\htdocs\\serverinventory\\vendor\\laravel\\framework\\src\\Illuminate\\Container\\BoundMethod.php(36): App\\Jobs\\FailedJobWithPartialSuccessJob->handle()
"} 

As you can see the part before the Exception is Executed even the job processing is failed

  • Related