Blog Detail

17

Feb
Easily Monitor Scheduled Tasks in your Laravel Application cover image

arrow_back Easily Monitor Scheduled Tasks in your Laravel Application

Spatie comes up with an awesome package called laravel-schedule-monitor. This package will monitor your Laravel schedule. It will write an entry to a log table in the db each time a scheduled task starts, ends, fails, or is skipped. Using the list command you can check when the scheduled tasks have been executed.

Installation

You can install the package via composer:

composer require spatie/laravel-schedule-monitor

If you need Laravel 8 support, you can install v2 of the package using this command:

composer require spatie/laravel-schedule-monitor:^2.

Preparing the database

You must publish and run migrations: via running this commands:

php artisan vendor:publish --provider="Spatie\ScheduleMonitor\ScheduleMonitorServiceProvider" --tag="schedule-monitor-migrations"
php artisan migrate

Publishing the config file

You can publish the config file with:

php artisan vendor:publish --provider="Spatie\ScheduleMonitor\ScheduleMonitorServiceProvider" --tag="schedule-monitor-config"

Cleaning the database

The schedule monitor will log each start, finish, and failure of all scheduled jobs. After a while, the monitored_scheduled_task_log_items might become big.

Using Laravel’s model pruning feature, you can delete old MonitoredScheduledTaskLogItem models. Models older than the number of days configured in the delete_log_items_older_than_days in the schedule-monitor config file will be deleted.

// app/Console/Kernel.php

class Kernel extends ConsoleKernel
{
    protected function schedule(Schedule $schedule)
    {
        $schedule->command('model:prune')->daily();
    }
}

Syncing the schedule

Every time you deploy your application, you should execute the schedule-monitor:sync command

schedule-monitor:sync

This command is responsible for syncing your schedule with the database, and optionally Oh Dear. We highly recommend adding this command to the script that deploys your production environment.

In a non-production environment, you should manually run schedule-monitor:sync. You can verify if everything synced correctly using schedule-monitor:list.

Usage

To monitor your schedule you should first run schedule-monitor:sync. This command will take a look at your schedule and create an entry for each task in the monitored_scheduled_tasks table.
To view all monitored scheduled tasks, you can run schedule-monitor:list. This command will list all monitored scheduled tasks. It will show you when a scheduled task has last started, finished, or failed.

The package will write an entry to the monitored_scheduled_task_log_items table in the db each time a scheduled task starts, ends, fails, or is skipped. Take a look at the contest of that table if you want to know when and how scheduled tasks did execute. The log items also hold other interesting metrics like memory usage, execution time, and more.

This package has a lot of features like:

  • Naming tasks
  • Setting a grace time
  • Ignoring scheduled tasks
  • Storing output in the database
  • Getting notified when a scheduled task doesn’t finish in time

If you want to explore this package and want to know more about its features, You can visit its complete documentation on Github.

Published at : 17-02-2022

Author : Rizwan Aslam
AUTHOR
Rizwan Aslam

I am a highly results-driven professional with 12+ years of collective experience in the grounds of web application development especially in laravel, native android application development in java, and desktop application development in the dot net framework. Now managing a team of expert developers at Codebrisk.

Launch your project

Launch project