lorisleiva / laravel-actions

⚡️ Laravel components that take care of one specific task
https://laravelactions.com
MIT License
2.52k stars 124 forks source link

AsJob ErrorHandling #122

Closed andrewskm closed 3 years ago

andrewskm commented 3 years ago

First off, fantastic package, has really helped me a lot!

Is there anything currently that handles errors when an AsJob action fails?

Current I'm calling the action via dispatch like so: CreatePDFExport::dispatch($data, $exportCollection, $filepath)->onQueue('export')->chain($chain); Once the job is completed, it sends a notification of completion with the chain method. If it errors, I would like to be able to send out a notification on failure.

I can wrap the AsJob method with a try-catch, but this allows it to falsely complete the job as I don't see a fail method like Laravel has here that signals to fail it.

I searched through the docs, but didn't come across anything, so apologizes if I overlooked this functionality.

leandrodiogenes commented 3 years ago

Hi @andrewskm . Maybe this helps you https://laravelactions.com/2.x/as-job.html#withchain

andrewskm commented 3 years ago

Thank you, this pointed me in the right direction of using the Bus::batch() feature of Laravel to catch any errors.

Code snippet I have to notify user of failed job.

$batch = Bus::batch([
    CreatePDFExport::makeJob($data, $exportCollection, $filepath),
        new NotifyUserOfCompletedExport($this->user, $reportName, $filepath);
])
->catch(function(Batch $batch, \Throwable $e)
{
    $message = GetExceptionMessage::run('Export', $e, $this->user);
    \Mail::to($this->user)->send(new FailedExport($this->data->get('reportName'), [$message], $batch->id));
})
->name('Queue PDF Export')
->onQueue('export')
->dispatch();