@@ -749,8 +749,17 @@ and their priorities:
749749
750750 $ php bin/console debug:event-dispatcher "Symfony\Component\Scheduler\Event\FailureEvent"
751751
752- Consuming Messages (Running the Worker)
753- ---------------------------------------
752+ Consuming Messages
753+ ------------------
754+
755+ The Scheduler component offers two ways to consume messages, depending on your
756+ needs: using the ``messenger:consume `` command or creating a worker programmatically.
757+ The first solution is the recommended one when using the Scheduler component in
758+ the context of a full stack Symfony application, the second one is more suitable
759+ when using the Scheduler component as a standalone component.
760+
761+ Running a Worker
762+ ~~~~~~~~~~~~~~~~
754763
755764After defining and attaching your recurring messages to a schedule, you'll need
756765a mechanism to generate and consume the messages according to their defined frequencies.
@@ -767,6 +776,45 @@ the Messenger component:
767776 .. image :: /_images/components/scheduler/generate_consume.png
768777 :alt: Symfony Scheduler - generate and consume
769778
779+ Creating a Consumer Programmatically
780+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
781+
782+ An alternative to the previous solution is to create and call a worker that
783+ will consume the messages. The component comes with a ready-to-use worker
784+ named :class: `Symfony\\ Component\\ Scheduler\\ Scheduler ` that you can use in your
785+ code::
786+
787+ use Symfony\Component\Scheduler\Scheduler;
788+
789+ $schedule = (new Schedule())
790+ ->with(
791+ RecurringMessage::trigger(
792+ new ExcludeHolidaysTrigger(
793+ CronExpressionTrigger::fromSpec('@daily'),
794+ ),
795+ new SendDailySalesReports()
796+ ),
797+ );
798+
799+ $scheduler = new Scheduler(handlers: [
800+ SendDailySalesReports::class => new SendDailySalesReportsHandler(),
801+ // add more handlers if you have more message types
802+ ], schedules: [
803+ $schedule,
804+ // the scheduler can take as many schedules as you need
805+ ]);
806+
807+ // finally, run the scheduler once it's ready
808+ $scheduler->run();
809+
810+ .. note ::
811+
812+ The :class: `Symfony\\ Component\\ Scheduler\\ Scheduler ` may be used
813+ when using the Scheduler component as a standalone component. If
814+ you are using it in the framework context, it is highly recommended to
815+ use the ``messenger:consume `` command as explained in the previous
816+ section.
817+
770818Debugging the Schedule
771819----------------------
772820
0 commit comments