Scheduler

Contents
A schedule
Add messages
Run it
One cron line

A schedule

The Scheduler runs repeated jobs without an extra daemon. A job is a small message object, and a schedule maps each message to a rhythm like every hour or every Monday. The block below is the whole shape: one schedule plus one recurring message.

use Symfony\Component\Scheduler\RecurringMessage; use Symfony\Component\Scheduler\Schedule; $schedule = new Schedule(); $schedule->add(RecurringMessage::every("1 hour", new CleanupMessage()));

Add messages

Each job is a plain PHP object that carries the job settings, and a handler does the work when the message arrives. Keep messages tiny: data in, no services inside, so they stay easy to store and retry.

class CleanupMessage { public function __construct(public readonly int $olderThanDays = 30) { } } class CleanupHandler { public function __invoke(CleanupMessage $message): void { // delete rows older than $message->olderThanDays } }

Run it

Schedules travel through Messenger, so a worker process executes the jobs. Start a worker that listens on the scheduler transport and watch the first run in the terminal before wiring any cron line.

$ php bin/console messenger:consume scheduler_default -vv

Handled 1 message from scheduler_default

One cron line

The worker exits after its time limit, so one cron line restarts it every minute. That single line is the full production setup for small projects: cron wakes the worker, the worker runs due messages, then it exits.

* * * * * cd /var/www/app && php bin/console messenger:consume scheduler_default --time-limit=60

Empty output: cron restarts the worker each minute

Next: Webhooks

Article author: Arthur Isaev

Related articles
Development with PHP
What is Symfony and where it runs
Install Symfony
Your first page
Routes
Controllers
Twig basics
Twig advanced
Doctrine setup
Entities
Migrations
Repositories
Relations
Forms
Validation
Services and DI
JSON controllers
Serializer
Security intro
Users
Login form
Voters
Console commands
Events
Messenger
Mailer
Cache
Sessions
CSRF
Translations
Dotenv and env
Profiler
Testing
Fixtures
Pagination
File uploads
API Platform in one lesson
DQL
Transactions
Migrations advanced
Security advanced
HTTP client
Scheduler
Webhooks
Asset Mapper
Deployment
Performance
Capstone: blog
Capstone: JSON API
Modern Symfony tour
Hero roadmap: the whole course on one page

Search this site

Channel @aofeed Chat @aofeedchat

Contacts and cooperation:
I recommend our hosting beget.com
Write to info@aredel.com if you:
1. Want to write an article for our site or translate an article into your native language.
2. Want to place thematically relevant ads on the site.
3. Ads on my site pass maximum censorship. If you see an ad block unsuitable for school-age children, shocking or misleading - please contact us by e-mail
4. Found a mistake, inaccuracy, bug, etc. on the site. ... .......
5. Articles can be shared on social media by clicking a network icon: