我有一份工作來測試失敗是如何工作的:<?phpnamespace App\Jobs;use App\ApiUser;use App\Helpers\Helper;use Exception;use Illuminate\Bus\Queueable;use Illuminate\Queue\SerializesModels;use Illuminate\Queue\InteractsWithQueue;use Illuminate\Contracts\Queue\ShouldQueue;use Illuminate\Foundation\Bus\Dispatchable;class FakeJob implements ShouldQueue{ use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; private $apiUser; public function __construct(ApiUser $apiUser) { $this->apiUser = $apiUser; } public function handle() { throw new Exception('time is even'); } public function failed(Exception $exception) { // Send user notification of failure, etc... $path = storage_path('logs/s3_logs/FakeJob_failed_' . Helper::generateRandomString(5) . '.txt'); file_put_contents($path, $exception->getMessage()); }}當我正常調度時,它會轉到failed函數并完全按照預期寫入文件。但是,當我這樣做時FakeJob::dispatchNow($apiUser);,它根本不會那樣做...有沒有辦法dispatchNow在與請求相同的進程上運行,但像正常排隊一樣失???因為目前,看來我唯一的方法是執行以下操作: $fakeJob = new FakeJob($apiUser); try { $fakeJob->handle(); } catch(Exception $e) { $fakeJob->failed($e); }我猜這……很好,但并不理想。
Laravel 作業隊列調度現在不會像往常一樣失敗
慕的地8271018
2022-07-16 10:15:30