DEV Community

Cover image for I built a Laravel package to stop silent cron failures
Jiordi Viera
Jiordi Viera

Posted on

I built a Laravel package to stop silent cron failures

My backup job failed at 3 AM last month. I found out on Monday when a client asked why their data wasn't updated.

This kept happening. Sync tasks stuck for hours. Report generation failing silently. No notifications, no logs I could easily check.

So I built something to fix it.

What I built

Laravel Smart Scheduler tracks every scheduled task execution automatically. Install it, and you get:

  • Execution history for every task
  • Stuck task detection (when server crashes mid-task)
  • Email notifications on failure
  • Overlap prevention with visibility
composer require jiordiviera/laravel-smart-scheduler
php artisan vendor:publish --tag=smart-scheduler-migrations
php artisan migrate
Enter fullscreen mode Exit fullscreen mode

No changes to your existing tasks. It hooks into Laravel's events.

How it works

Every execution gets recorded:

use Jiordiviera\SmartScheduler\LaravelSmartScheduler\Models\ScheduleRun;

// Recent executions
$runs = ScheduleRun::latest('started_at')->limit(10)->get();

// What failed
$failed = ScheduleRun::where('status', ScheduleRun::STATUS_FAILED)->get();
Enter fullscreen mode Exit fullscreen mode

Each record has: task identifier, status, start/end time, duration, output, exception message, server name.

Stuck task detection

This is the feature I needed most.

When your server crashes or a process gets killed, the task stays in "starting" status. Next run sees an overlap and skips. Your task never runs again until you manually fix it.

Smart Scheduler detects this:

// config/smart-scheduler.php
'stuck_timeout_minutes' => 60,
Enter fullscreen mode Exit fullscreen mode

When a new execution starts, it checks for old "starting" tasks past the timeout, marks them as "stuck", notifies you, and lets the new execution proceed.

You can also run it manually:

php artisan smart-scheduler:detect-stuck
Enter fullscreen mode Exit fullscreen mode

Notifications

// config/smart-scheduler.php
'notifications' => [
    'email' => [
        'recipients' => ['admin@example.com'],
    ],
    'notify_on_stuck' => true,
],
Enter fullscreen mode Exit fullscreen mode

Or in your .env:

SMART_SCHEDULER_EMAIL_RECIPIENTS=admin@example.com
Enter fullscreen mode Exit fullscreen mode

Setup I recommend

// routes/console.php
use Illuminate\Support\Facades\Schedule;

// Clean old records weekly
Schedule::command('smart-scheduler:purge')->weekly();

// Detect stuck tasks every 30 minutes
Schedule::command('smart-scheduler:detect-stuck')->everyThirtyMinutes();
Enter fullscreen mode Exit fullscreen mode

Custom notifications

Want Slack instead of email? Implement the interface:

use Jiordiviera\SmartScheduler\LaravelSmartScheduler\Contracts\SmartNotifierInterface;

class SlackNotifier implements SmartNotifierInterface
{
    public function sendFailureNotification(ScheduleRun $run): void
    {
        // Your Slack logic
    }

    public function sendStuckNotification(ScheduleRun $run): void
    {
        // Your Slack logic
    }
}

// In a service provider
$this->app->singleton(SmartNotifierInterface::class, SlackNotifier::class);
Enter fullscreen mode Exit fullscreen mode

Requirements

  • PHP 8.2+
  • Laravel 11 or 12

Links


If this is useful to you, a star on GitHub helps.

What do you use to monitor your scheduled tasks?

Top comments (0)