The evening raven

Exercise 4 of 6 in chapter 7 · 250 XP · 5 objectives checked by tests

Topics covered: Scheduler#[AsSchedule]RecurringMessageScheduled task

Every evening, Gorm wants to know what awaits him the next day: how many tables, how many adventurers. Enough to plan the provisions, and the number of barrels.

Nobody will trigger this summary: it must leave on its own, every day at 6 p.m.

Your mission

  1. In CorbeauDuSoir (the evening raven), declare the tavern's schedule, under the name soir (evening): every day at 6 p.m., a ResumeDuSoir (evening summary).
  2. In ResumeDuSoirHandler, count tomorrow's reservations and send the summary to Gorm:
    • Tomorrow: 2 tables, 6 adventurers.;
    • singular when there is only one table or one adventurer;
    • Tomorrow: no reservations. Time to rest! if the day is free.

The Scheduler, in two ideas

A schedule (Schedule) says what to send and when. A trigger describes the rhythm: every hour, every day at 6 p.m., every Monday…

(new Schedule())->add(RecurringMessage::every('1 day', new ResumeDuSoir(), from: '18:00'));

The message then goes into the bus, like the others: an ordinary handler handles it. Scheduling and handling remain two separate things.

Who keeps track of the time?

A worker, once again. #[AsSchedule('soir')] creates a scheduler_soir transport, which you consume like a queue:

messenger:consume scheduler_soir

The worker watches the clock and releases the message when the time has come. In the preview, nothing will run at 6 p.m.: what your tests check is the scheduling itself.