-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
[Scheduler] Add new component documentation #18136
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -54,6 +54,7 @@ Topics | |
profiler | ||
rate_limiter | ||
routing | ||
scheduler | ||
security | ||
session | ||
setup | ||
|
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,129 @@ | ||||||||||
Scheduler | ||||||||||
========= | ||||||||||
|
||||||||||
.. versionadded:: 6.3 | ||||||||||
|
||||||||||
The Scheduler component was introduced in Symfony 6.3 and is marked | ||||||||||
as experimental. | ||||||||||
|
||||||||||
The Scheduler component provides a way to schedule periodical tasks or handling | ||||||||||
recurring messages without using an external tool. | ||||||||||
|
||||||||||
The component shares somes keys concepts with :ref:`Messenger <messenger>`. | ||||||||||
|
||||||||||
Installation | ||||||||||
------------ | ||||||||||
|
||||||||||
You can install the Scheduler component with: | ||||||||||
|
||||||||||
.. code-block:: terminal | ||||||||||
|
||||||||||
$ composer require symfony/scheduler | ||||||||||
|
||||||||||
.. include:: /components/require_autoload.rst.inc | ||||||||||
|
||||||||||
.. _register-schedule-provider: | ||||||||||
|
||||||||||
Register a Schedule provider | ||||||||||
---------------------------- | ||||||||||
|
||||||||||
A Schedule provider is a class defining your schedule : which messages should | ||||||||||
be processed depending a trigger you choose:: | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
|
||||||||||
namespace App\Schedule; | ||||||||||
|
||||||||||
use App\Message\EndofTrialMessage; | ||||||||||
use Symfony\Component\Scheduler\Attribute\AsSchedule; | ||||||||||
use Symfony\Component\Scheduler\RecurringMessage; | ||||||||||
use Symfony\Component\Scheduler\Schedule; | ||||||||||
use Symfony\Component\Scheduler\ScheduleProviderInterface; | ||||||||||
|
||||||||||
#[AsSchedule('trial')] | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
I'm thinking we should just use default here. Multiple schedules is an advanced use-case imo. |
||||||||||
final class EndOfTrialScheduleProvider implements ScheduleProviderInterface | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
{ | ||||||||||
public function getSchedule(string $id): Schedule | ||||||||||
{ | ||||||||||
return (new Schedule()) | ||||||||||
->add(new RecurringMessage::every('1 week', new EndofTrialMessage())) | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The
Suggested change
|
||||||||||
; | ||||||||||
} | ||||||||||
} | ||||||||||
|
||||||||||
Run the Messenger consumer | ||||||||||
-------------------------- | ||||||||||
|
||||||||||
Scheduler uses the same :ref:`Messenger worker <messenger-worker>` to consume | ||||||||||
messages dispatched by your schedule class. | ||||||||||
|
||||||||||
You can do this with the ``messenger:consume`` command: | ||||||||||
|
||||||||||
.. code-block:: terminal | ||||||||||
|
||||||||||
$ php bin/console messenger:consume scheduler_trial | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
|
||||||||||
Trigger your schedule | ||||||||||
--------------------- | ||||||||||
|
||||||||||
Symfony provides by default two triggers for your schedules : `every` and | ||||||||||
`cron`. By extending the provided TriggerInterface you can also create | ||||||||||
your own trigger for your app. | ||||||||||
Comment on lines
+67
to
+69
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
|
||||||||||
Every | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
~~~~~ | ||||||||||
|
||||||||||
This trigger allows you to define you schedule with frequency as text, using | ||||||||||
intervals or relative date format allowed by PHP:: | ||||||||||
|
||||||||||
RecurringMessage::every('10 seconds', $msg); | ||||||||||
RecurringMessage::every('1 day', $msg); | ||||||||||
RecurringMessage::every('2 weeks', $msg); | ||||||||||
|
||||||||||
RecurringMessage::every('first day of month', $msg); | ||||||||||
RecurringMessage::every('next tuesday', $msg); | ||||||||||
|
||||||||||
It is possible to specify an exact hour to trigger the schedule with `from` | ||||||||||
option:: | ||||||||||
|
||||||||||
RecurringMessage::every('first day of month', $msg, from: '13:47'); | ||||||||||
|
||||||||||
// using timezone | ||||||||||
RecurringMessage::every('first day of month', $msg, from: '13:47+0400'); | ||||||||||
|
||||||||||
// using a DateTimeImmutable object | ||||||||||
$from = new \DateTimeImmutable('13:47', new \DateTimeZone('Europe/Paris')); | ||||||||||
RecurringMessage::every('first day of month', $msg, from: $from); | ||||||||||
|
||||||||||
If your schedule do not need to run indefinitely, you can use `until` option | ||||||||||
to let the Component know when the trigger should stop:: | ||||||||||
|
||||||||||
RecurringMessage::every('monday', $msg, until: '2023-06-12'); | ||||||||||
|
||||||||||
Cron | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
~~~~ | ||||||||||
|
||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We'll need a note that https://github.com/dragonmantank/cron-expression is required to use this trigger. |
||||||||||
This trigger can be configured following same rules as the eponymous Unix | ||||||||||
utility:: | ||||||||||
|
||||||||||
// trigger every hour | ||||||||||
RecurringMessage::cron('0 * * * *', $msg); | ||||||||||
|
||||||||||
// trigger every monday at 12:00 | ||||||||||
RecurringMessage::cron('0 12 * * 1', $msg); | ||||||||||
|
||||||||||
.. note:: | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should document symfony/symfony#49792 |
||||||||||
|
||||||||||
The minimal interval allowed with cron is 1 minute. | ||||||||||
|
||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. TODO:
|
||||||||||
Define your own trigger | ||||||||||
~~~~~~~~~~~~~~~~~~~~~~~ | ||||||||||
|
||||||||||
By implementing the TriggerInterface your can create your own trigger using | ||||||||||
``getNextRunDate`` method:: | ||||||||||
|
||||||||||
final class CustomTrigger implements TriggerInterface | ||||||||||
{ | ||||||||||
public function getNextRunDate(\DateTimeImmutable $run): ?\DateTimeImmutable | ||||||||||
{ | ||||||||||
// your logic | ||||||||||
} | ||||||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.