Skip to content

Cron monitoring improvements#677

Merged
cleptric merged 2 commits into
masterfrom
cron-improvements
Jun 29, 2023
Merged

Cron monitoring improvements#677
cleptric merged 2 commits into
masterfrom
cron-improvements

Conversation

@stayallive

@stayallive stayallive commented Apr 5, 2023

Copy link
Copy Markdown
Collaborator

Making use of the new cron monitoring upsert possibilities we can add some information to automatically create a cron monitor by "simply" scheduling an command and calling ->sentryMonitor() on it.

    protected function schedule(Schedule $schedule): void
    {
        $schedule->command(MyCommand::class)->everyMinute()->sentryMonitor();
    }

The sentryMonitor method has a few (all optional) arguments:

->sentryMonitor(
    monitorSlug: null,         // you can set a custom slug here, needed for crons created from UI or duplicated scheduled commands (same command scheduled more than once)
    checkInMargin: 60,         // amount of seconds (?) the command is allowed to be late to check in
    maxRuntime: 300,           // amount of seconds (?) the command is allowed to run
    updateMonitorConfig: true, // indicates if we should send the monitor config with the checking to update the schedule and other config automatically (can be disabled in case you want control over it in the UI
)

Would be nice to have:

  • Upsert the cron name instead of it being set equal to the slug (is this possible?)

This depends on the release of:

@stayallive
stayallive marked this pull request as ready for review June 23, 2023 20:59
@stayallive
stayallive requested a review from cleptric June 23, 2023 21:00

@cleptric cleptric left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:shipit:

Will get the docs ready then.

@cleptric
cleptric merged commit 3c5b835 into master Jun 29, 2023
@cleptric
cleptric deleted the cron-improvements branch June 29, 2023 09:35
@ankurk91

Copy link
Copy Markdown
Contributor

This is how i was able to monitor app of my schedule tasks

<?php

namespace App\Providers;

use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Support\ServiceProvider;

class CronMonitorProvider extends ServiceProvider
{
    public function boot(): void
    {
        if (!$this->app->runningInConsole()) {
            return;
        }

        $this->app->booted(fn() => $this->hookMonitor());
    }

    protected function hookMonitor(): void
    {
        $events = $this->app->make(Schedule::class)->events();

        foreach ($events as $event) {
            if ($event->hasMacro('sentryMonitor')) {
                $event->sentryMonitor();
            }
        }
    }
}

@stayallive

Copy link
Copy Markdown
Collaborator Author

@ankurk91 ah, that is a smart one to hook all your scheduled events at once, nice!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants