Skip to content

Commit dac7b2c

Browse files
committed
minor #19667 [Scheduler] Mention Scheduler worker (alexandre-daubois)
This PR was merged into the 6.4 branch. Discussion ---------- [Scheduler] Mention `Scheduler` worker Fix #18088 Commits ------- e03649f [Scheduler] Mention `Scheduler` worker
2 parents 62a2a10 + e03649f commit dac7b2c

File tree

1 file changed

+50
-2
lines changed

1 file changed

+50
-2
lines changed

scheduler.rst

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -782,8 +782,17 @@ and their priorities:
782782
The ``PreRunEvent``, ``PostRunEvent`` and ``FailureEvent`` events were
783783
introduced in Symfony 6.4.
784784

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+
~~~~~~~~~~~~~~~~
787796

788797
After defining and attaching your recurring messages to a schedule, you'll need
789798
a mechanism to generate and consume the messages according to their defined frequencies.
@@ -800,6 +809,45 @@ the Messenger component:
800809
.. image:: /_images/components/scheduler/generate_consume.png
801810
:alt: Symfony Scheduler - generate and consume
802811

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+
803851
Debugging the Schedule
804852
----------------------
805853

0 commit comments

Comments
 (0)