In some cases, we want to perform some action when job has failed. For example, send an email or a notification.
For this purpose, we can use failed()
method in the job class, just like the handle()
method:
1namespace App\Jobs\Invoice; 2use Illuminate\Bus\Batchable; 3use Illuminate\Bus\Queueable; 4use Illuminate\Queue\SerializesModels; 5use Illuminate\Queue\InteractsWithQueue; 6use Illuminate\Contracts\Queue\ShouldQueue; 7use Illuminate\Foundation\Bus\Dispatchable; 8 9class CalculateSingleConsignment implements ShouldQueue10{11 use Batchable, Dispatchable, InteractsWithQueue, Queueable, SerializesModels;12 13 // ... __construct() method, handle() method, etc.14 15 public function failed()16 {17 // Perfom any action here when job has failed18 }19}
Tip given by @pauloimon