@@ -749,8 +749,17 @@ and their priorities:
749
749
750
750
$ php bin/console debug:event-dispatcher "Symfony\Component\Scheduler\Event\FailureEvent"
751
751
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
+ ~~~~~~~~~~~~~~~~
754
763
755
764
After defining and attaching your recurring messages to a schedule, you'll need
756
765
a mechanism to generate and consume the messages according to their defined frequencies.
@@ -767,6 +776,45 @@ the Messenger component:
767
776
.. image :: /_images/components/scheduler/generate_consume.png
768
777
:alt: Symfony Scheduler - generate and consume
769
778
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
+
770
818
Debugging the Schedule
771
819
----------------------
772
820
0 commit comments