Skip to main content

Know when a Symfony scheduled task did not run

Wrap the body of your Symfony scheduled task in \Sentry\withMonitor() with the task's schedule, and point the Sentry DSN at Buggregator. Each run then reports to Buggregator. It shows which runs went fine and which failed, and it marks a run as missed when it never started and as hung when it never finished.

Steps

1. Start Buggregator with a database file

Jobs and their runs live in the database. The default one is in memory, so keep a file to see runs across restarts.

bash
docker run --pull always \
  -p 127.0.0.1:8000:8000 \
  -e DATABASE_DSN=/data/buggregator.db \
  -v buggregator-data:/data \
  ghcr.io/buggregator/server:latest

2. Point the Sentry SDK at Buggregator in dev

Install sentry/sentry-symfony. Its recipe turns Sentry on for prod only. Enable the bundle for dev in config/bundles.php, then add a when@dev block.

php
// config/bundles.php
Sentry\SentryBundle\SentryBundle::class => ['dev' => true, 'prod' => true],
yaml
# config/packages/sentry.yaml
when@dev:
    sentry:
        dsn: '%env(SENTRY_DSN)%'
dotenv
SENTRY_DSN=http://sentry@127.0.0.1:8000/1

3. Wrap the task in withMonitor()

Give the monitor the same crontab as the task. checkinMargin is how many minutes a run may be late; maxRuntime is how many minutes it may take.

php
use Sentry\MonitorConfig;
use Sentry\MonitorSchedule;
use Symfony\Component\Scheduler\Attribute\AsCronTask;

#[AsCronTask('0 3 * * *')]
final class NightlyReport
{
    public function __invoke(): void
    {
        \Sentry\withMonitor(
            slug: 'nightly-report',
            callback: fn () => $this->build(),
            monitorConfig: new MonitorConfig(
                MonitorSchedule::crontab('0 3 * * *'),
                checkinMargin: 5,
                maxRuntime: 30,
                timezone: 'Europe/Berlin',
            ),
        );
    }

    private function build(): void
    {
        // the work
    }
}

4. Or call the ping URL

A task without the Sentry SDK can call a URL when it starts and when it ends. The schedule query sets the crontab (a + is a space).

bash
# when it starts
curl -fsS "http://127.0.0.1:8000/ping/nightly-report/start?schedule=0+3+*+*+*"
# when it ends fine
curl -fsS "http://127.0.0.1:8000/ping/nightly-report"
# when it fails
curl -fsS "http://127.0.0.1:8000/ping/nightly-report/fail"

5. Open Sentry, then Crons

Run the scheduler worker as usual. Then open http://127.0.0.1:8000, the Sentry page, Crons. Each job shows its status, schedule, last and next run, and its last 20 runs. Jobs that failed, hung or missed a run come first, under Needs attention. Open a job for its run history.

Why it works

withMonitor() sends a check-in when the task starts and another when it ends, with ok or error and the duration. The first check-in creates the job in Buggregator, with the schedule and limits you gave. Every 30 seconds Buggregator checks the schedules: a run that was due and never came is missed, a run that started and did not finish within maxRuntime is timeout. Buggregator sends no alerts: you see it in the UI.

Checked on with Buggregator v2.13.0

Setup in 60 seconds

No registration. No account. Start it and point your app at it.

1

Start Buggregator

One file, installed with one command. The web UI opens on port 8000. Prefer containers? Use the Docker tab.

$ curl -fsSL https://raw.githubusercontent.com/buggregator/server/master/scripts/install.sh | sh
$ HTTP_ADDR=127.0.0.1:8000 \
  SMTP_ADDR=127.0.0.1:1025 \
  VAR_DUMPER_ADDR=127.0.0.1:9912 \
  MONOLOG_ADDR=127.0.0.1:9913 \
  PROXY_ADDR=127.0.0.1:8080 \
  ~/.local/bin/buggregator

The script picks the file for your CPU: Intel/AMD or ARM.

One file for Linux, macOS and Windows, about 40 MB. Nothing else to install.

This run line keeps it on this computer. Without the *_ADDR settings it listens on all network interfaces

Rather not run a script? Download the file yourself

Runs on Windows, macOS and Linux, on Intel/AMD and ARM.

Prefer not to do it yourself? Your AI agent can set Buggregator up for you