laravel-schedule-monitor

repository·main·Indexed 21 days ago

https://github.com/spatie/laravel-schedule-monitor

A Laravel package for monitoring scheduled tasks, providing tools for tracking task execution and output with optional integration for the Oh Dear service. It includes Artisan commands for syncing and listing tasks (schedule-monitor:sync, schedule-monitor:list, schedule-monitor:verify), database logging for task states (starting, finished, failed, skipped), and SchedulerEvent macros to configure monitoring behavior such as grace times and custom monitor names.

Tokens
4.7K
Snippets
25
Records
30
Agent score
76%

What's inside spatie/laravel-schedule-monitor

  1. Upgrade from v3 to v4

    main

    The Oh Dear integration has been simplified. You no longer need to install the separate Oh Dear SDK package to sync with Oh Dear.

    Configuration Changes:

    1. In config/schedule-monitor.php, replace the old ohdear.site_id configuration key with monitor_id.
    2. In your .env file, rename the environment variable OH_DEAR_SITE_ID to OH_DEAR_MONITOR_ID.
    // In config/schedule-monitor.php
    'monitor_id' => env('OH_DEAR_MONITOR_ID'),
  2. Publish the Schedule Monitor configuration file

    main

    To customize the behavior of the monitor (such as log retention or Oh Dear integration), publish the config file:

    php artisan vendor:publish --provider="Spatie\ScheduleMonitor\ScheduleMonitorServiceProvider" --tag="schedule-monitor-config"
  3. Prune old scheduled task logs

    main

    To prevent the monitored_scheduled_task_log_items table from growing too large, use Laravel's model pruning feature. The package respects the delete_log_items_older_than_days config setting.

    Add the model:prune command to your app/Console/Kernel.php:

    // app/Console/Kernel.php
    
    use Spatie//ScheduleMonitor\Models\MonitoredScheduledTaskLogItem;
    
    class Kernel extends ConsoleKernel
    {
        protected function schedule(Schedule $schedule)
        {
            $schedule->command('model:prune', ['--model' => MonitoredScheduledTaskLogItem::class])->daily();
        }
    }
    // app/Console/Kernel.php
    
    use Spatie\ScheduleMonitor\Models\MonitoredScheduledTaskLogItem;
    
    class Kernel extends ConsoleKernel
    {
        protected function schedule(Schedule $schedule)
        {
            $schedule->command('model:prune', ['--model' => MonitoredScheduledTaskLogItem::class])->daily();
        }
    }
  4. Configure Schedule Monitor settings

    main

    The published configuration file allows you to manage log retention, date formats, and custom models.

    Key configuration options include:

    • delete_log_items_older_than_days: Number of days to keep log items before they are eligible for pruning.
    • date_format: The date format used in CLI command outputs.
    • models: Allows you to swap the default MonitoredScheduledTask and MonitoredScheduledTaskLogItem models with your own implementations.
    • oh_dear: A comprehensive array of settings for syncing your schedule with Oh Dear, including api_token, monitor_id, queue, and grace_time_in_minutes (the amount of minutes a task can run before being considered late).
    return [
        'delete_log_items_older_than_days' => 30,
        'date_format' => 'Y-m-d H:i:s',
        'models' => [
            'monitored_scheduled_task' => Spatie\ScheduleMonitor\Models\MonitoredScheduledTask::class,
            'monitored_scheduled_log_item' => Spatie\ScheduleMonitor\Models\MonitoredScheduledTaskLogItem::class,
        ],
        'oh_dear' => [
            'api_token' => env('OH_DEAR_API_TOKEN', ''),
            'monitor_id' => env('OH_DEAR_MONITOR_ID'),
            'queue' => env('OH_DEAR_QUEUE'),
            'ping_oh_dear_job' => Spatie\ScheduleMonitor\Jobs\PingOhDearJob::class,
            'retry_job_for_minutes' => 10,
            'silence_ping_oh_dear_job_in_horizon' => true,
            'send_starting_ping' => env('OH_DEAR_SEND_STARTING_PING', false),
            'grace_time_in_minutes' => 5,
            'endpoint_url' => env('OH_DEAR_PING_ENDPOINT_URL'),
            'api_url' => env('OH_DEAR_API_URL', 'https://ohdear.app/api/'),
            'retry_delay_ms' => env('OH_DEAR_RETRY_DELAY_MS', 10_000),
            'debug_logging' => env('OH_DEAR_DEBUG_LOGGING', false),
        ],
    ];
  5. Debug failed Oh Dear pings

    main

    If you encounter connection issues or timeouts when syncing with Oh Dear, enable debug logging in your .env file. This will trigger a Log::warning containing detailed diagnostics like cURL timing (DNS, TCP, TLS), connection info, and server responses.

    OH_DEAR_DEBUG_LOGGING=true
  6. Configure custom models for monitoring

    main

    You can customize which Eloquent models are used to store monitoring data by updating the models key in your config/schedule-monitor.php file. This is useful if you need to extend the default models or use them in a multi-tenant environment.

    Supported keys:

    • monitored_scheduled_task: The class used for MonitoredScheduledTask.
    • monitored_scheduled_log_item: The class used for MonitoredScheduledTaskLogItem.

    Note: The provided classes must extend the package's base model classes.

  7. Sync your schedule with the database and Oh Dear

    main

    You should run the schedule-monitor:sync command every time you deploy your application. This command synchronizes your Laravel schedule with the database and, if configured, with Oh Dear.

    Warning: This command is destructive; it will remove any cron monitors defined in Oh Dear that are not part of your application's schedule.

    php artisan schedule-monitor:sync

    To perform a non-destructive sync (useful for monitoring tasks outside of Laravel), use the --keep-old flag. This will only add new tasks to Oh Dear and will not remove existing ones.

    php artisan schedule-monitor:sync --keep-old