我不明白為什么我php artisan queue:work在控制臺中的命令回傳失敗,但電子郵件已發送,并且只有第一個函式呼叫被觸發。
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);
}
這是我的調度方法:
$job1 = new SendQuoteEmail(collect($request),$totalprodprice,$quotationprice,$featitem,$quoteid,"client");
$this->dispatch($job1);
這是一些控制臺日志。
[2022-02-15 23:50:04][28] Processing: App\Jobs\SendQuoteEmail
[2022-02-15 23:50:08][28] Failed: App\Jobs\SendQuoteEmail
uj5u.com熱心網友回復:
即使發送了郵件,您的作業也會失敗,因為您在觸發后撰寫的代碼會Mail引發例外
例如,讓我們考慮以下Job類
<?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');
}
}
在像這樣派遣作業FailedJobWithPartialSuccessJob::dispatch()并盯著工人看之后是我的cli輸出

這是日志檔案內容
[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()
"}
如您所見,Exception即使作業處理失敗,也已執行之前的部分
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/425339.html
上一篇:Where子句和搜索
