How to trigger laravel jobs failed() method when job fails?

Viewed 11852

Currently I'm calling task Report.php and generating report using generateReport() method. I have checked that the jobs have been executed using CLI command php artsan queue:listen. If anything goes wrong while calling Artisan::call() the error message will be displayed in terminal. So I want to catch the exception in failed() and I want to log the error into logs. I have tried with try catch in handle() method but it's not catching the exception.

protected $options;
public function __construct($options)
{
    $this->options = array_merge(
        [
            'task'  => 'Report',
            'do'    => 'generateReport',
            'limit' => '10000'
        ],
        $options
    );

}
public function handle()
{
    Artisan::call('execute', [
        '--task'        => $this->options['task'],
        '--do'          => $this->options['do'],
        '--parameters'  => $this->options,

    ]);

}

public function failed()
{
    //
}

How can I trigger the failed() and get the error into logs?

1 Answers
Related