@@ -782,8 +782,17 @@ and their priorities:
782
782
The ``PreRunEvent ``, ``PostRunEvent `` and ``FailureEvent `` events were
783
783
introduced in Symfony 6.4.
784
784
785
- Consuming Messages (Running the Worker)
786
- ---------------------------------------
785
+ Consuming Messages
786
+ ------------------
787
+
788
+ The Scheduler component offers two ways to consume messages, depending on your
789
+ needs: using the ``messenger:consume `` command or creating a worker programmatically.
790
+ The first solution is the recommended one when using the Scheduler component in
791
+ the context of a full stack Symfony application, the second one is more suitable
792
+ when using the Scheduler component as a standalone component.
793
+
794
+ Running a Worker
795
+ ~~~~~~~~~~~~~~~~
787
796
788
797
After defining and attaching your recurring messages to a schedule, you'll need
789
798
a mechanism to generate and consume the messages according to their defined frequencies.
@@ -800,6 +809,45 @@ the Messenger component:
800
809
.. image :: /_images/components/scheduler/generate_consume.png
801
810
:alt: Symfony Scheduler - generate and consume
802
811
812
+ Creating a Consumer Programmatically
813
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
814
+
815
+ An alternative to the previous solution is to create and call a worker that
816
+ will consume the messages. The component comes with a ready-to-use worker
817
+ named :class: `Symfony\\ Component\\ Scheduler\\ Scheduler ` that you can use in your
818
+ code::
819
+
820
+ use Symfony\Component\Scheduler\Scheduler;
821
+
822
+ $schedule = (new Schedule())
823
+ ->with(
824
+ RecurringMessage::trigger(
825
+ new ExcludeHolidaysTrigger(
826
+ CronExpressionTrigger::fromSpec('@daily'),
827
+ ),
828
+ new SendDailySalesReports()
829
+ ),
830
+ );
831
+
832
+ $scheduler = new Scheduler(handlers: [
833
+ SendDailySalesReports::class => new SendDailySalesReportsHandler(),
834
+ // add more handlers if you have more message types
835
+ ], schedules: [
836
+ $schedule,
837
+ // the scheduler can take as many schedules as you need
838
+ ]);
839
+
840
+ // finally, run the scheduler once it's ready
841
+ $scheduler->run();
842
+
843
+ .. note ::
844
+
845
+ The :class: `Symfony\\ Component\\ Scheduler\\ Scheduler ` may be used
846
+ when using the Scheduler component as a standalone component. If
847
+ you are using it in the Framework context, it is highly recommended to
848
+ use the ``messenger:consume `` command as explained in the previous
849
+ section.
850
+
803
851
Debugging the Schedule
804
852
----------------------
805
853
0 commit comments