Skip to content

Commit 43c1209

Browse files
committed
Merge branch '7.0' into 7.1
* 7.0: [Scheduler] Mention `Scheduler` worker
2 parents 6c75e09 + f14651d commit 43c1209

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
@@ -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

755764
After defining and attaching your recurring messages to a schedule, you'll need
756765
a 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+
770818
Debugging the Schedule
771819
----------------------
772820

0 commit comments

Comments
 (0)