Symfony Scheduler: scheduling recurring tasks
Symfony Scheduler sends a message at regular intervals, without a crontab: the schedule lives in the code, the processing in an ordinary handler.
#[AsSchedule('soir')] final class CorbeauDuSoir implements ScheduleProviderInterface { public function getSchedule(): Schedule { return (new Schedule()) ->add(RecurringMessage::every('1 day', new ResumeDuSoir(), from: '18:00')); } } // A worker consumes the transport created by the attribute: messenger:consume scheduler_soir
A schedule (Schedule) says which message to send and when, with RecurringMessage objects: every day at 6 pm, every hour. The message then goes into the Messenger bus, and an ordinary handler processes it: scheduling and processing stay separate. #[AsSchedule('soir')] creates a scheduler_soir transport, which a worker consumes like a queue. The trap: without the attribute, or without a worker consuming that transport, nothing goes out, and nothing reports it.
The exercises have you send the innkeeper, every evening, a summary of the next day's bookings, then find out why that summary no longer goes out and why it announces the wrong day.
2 exercises work on this concept, by writing code in the browser.
In the tracks
- The evening raven Symfony for PHP developers · chapter 7: The messenger raven
- 🐉 Boss: the broken aviary Symfony for PHP developers · chapter 7: The messenger raven