Skip to content

ScheduledTaskFailed event is not dispatched when scheduled task is failing #55352

@sagautam5

Description

@sagautam5

Laravel Version

11.0^

PHP Version

8.4

Database Driver & Version

I am using Postgres DB

Description

I was trying to listen the laravel builtin event ScheduledTaskFailed to send notifications like email/slack etc. when scheduled command is failed in the application.

However all other events such as ScheduledTaskFinished, ScheduledTaskSkipped, ScheduledTaskStarting are dispatched correctly but ScheduledTaskFailed is not dispatched even when scheduled task is failing.

I have also looked into the implementation in framework:

try {
    $event->run($this->laravel);

    $this->dispatcher->dispatch(new ScheduledTaskFinished(
        $event,
        round(microtime(true) - $start, 2)
    ));
    
    $this->eventsRan = true;
} catch (Throwable $e) {
    $this->dispatcher->dispatch(new ScheduledTaskFailed($event, $e));

    $this->handler->report($e);
}

In this section, implementation inside catch is never executed even when failure of scheduled task.

Is there anything I am missing or misunderstood in it ?

Steps To Reproduce

  1. Add following listener logic inside boot method of app service provider
use Illuminate\Console\Events\ScheduledTaskFailed;
use Illuminate\Support\Facades\Event;

Event::listen(ScheduledTaskFailed::class, function () {
    logger('Scheduled task failed');
});
  1. Add example task to routes/console.php that runs every minute & fails
use Illuminate\Foundation\Inspiring;
use Illuminate\Support\Facades\Artisan;

Artisan::command('inspire', function () {
    $this->comment(Inspiring::quote());
    throw new \Exception('Inspirational quote sent!');
})->purpose('Display an inspiring quote')->everyMinute();
  1. Run Scheduler
    php artisan schedule:run

Console Error:

  2025-04-10 09:55:04 Running ['artisan' inspire] .................................................................................... 774.96ms FAIL
  ⇂ '/usr/local/bin/php' 'artisan' inspire > '/dev/null' 2>&1  

But nothing logged into laravel log.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions