-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
[Scheduler] Add AsCronTask
and AsPeriodicTask
#19444
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -96,6 +96,8 @@ Another important difference is that messages in the Scheduler component are | |
recurring. They are represented via the :class:`Symfony\\Component\\Scheduler\\RecurringMessage` | ||
class. | ||
|
||
.. _scheduler_attaching-recurring-messages: | ||
|
||
Attaching Recurring Messages to a Schedule | ||
------------------------------------------ | ||
|
||
|
@@ -180,6 +182,8 @@ methods:: | |
Most of them can be created via the :class:`Symfony\\Component\\Scheduler\\RecurringMessage` | ||
class, as shown in the following examples. | ||
|
||
.. _scheduler_cron-expression-triggers: | ||
|
||
Cron Expression Triggers | ||
~~~~~~~~~~~~~~~~~~~~~~~~ | ||
|
||
|
@@ -199,7 +203,43 @@ Then, define the trigger date/time using the same syntax as the | |
|
||
.. versionadded:: 6.4 | ||
|
||
Since version 6.4, it is now possible to add and define a timezone as a 3rd argument | ||
Since version 6.4, it is now possible to add and define a timezone as a 3rd argument. | ||
|
||
Another way of declaring cron triggers is to use the | ||
:class:`Symfony\\Component\\Scheduler\\Attribute\\AsCronTask` attribute | ||
on an invokable class:: | ||
|
||
// src/Scheduler/Task/SendDailySalesReports.php | ||
namespace App\Scheduler\Task; | ||
|
||
use Symfony\Component\Scheduler\Attribute\AsCronTask; | ||
|
||
#[AsCronTask('0 0 * * *')] | ||
class SendDailySalesReports | ||
{ | ||
public function __invoke() | ||
{ | ||
// ... | ||
} | ||
} | ||
|
||
This is the most basic way to define a cron trigger. However, the attribute | ||
takes more parameters to customize the trigger:: | ||
|
||
// adds randomly up to 6 seconds to the trigger time to avoid load spikes | ||
#[AsCronTask('0 0 * * *', jitter: 6)] | ||
|
||
// defines the method name to call instead as well as the arguments to pass to it | ||
#[AsCronTask('0 0 * * *', method: 'sendEmail', arguments: ['email' => '[email protected]'])] | ||
|
||
// defines the timezone to use | ||
#[AsCronTask('0 0 * * *', timezone: 'Africa/Malabo')] | ||
|
||
.. versionadded:: 6.4 | ||
|
||
The :class:`Symfony\\Component\\Scheduler\\Attribute\\AsCronTask` attribute | ||
was introduced in Symfony 6.4. | ||
|
||
|
||
.. tip:: | ||
|
||
|
@@ -264,6 +304,8 @@ For example:: | |
The day of month range is ``1-28``, this is to account for February | ||
which has a minimum of 28 days. | ||
|
||
.. _scheduler_periodical-triggers: | ||
|
||
Periodical Triggers | ||
~~~~~~~~~~~~~~~~~~~ | ||
|
||
|
@@ -279,6 +321,55 @@ defined by PHP datetime functions:: | |
$until = '2023-06-12'; | ||
RecurringMessage::every('first Monday of next month', new Message(), $from, $until); | ||
|
||
Like cron triggers, you can also use the | ||
:class:`Symfony\\Component\\Scheduler\\Attribute\\AsPeriodicTask` attribute | ||
on an invokable class:: | ||
|
||
// src/Scheduler/Task/SendDailySalesReports.php | ||
namespace App\Scheduler\Task; | ||
|
||
use Symfony\Component\Scheduler\Attribute\AsPeriodicTask; | ||
|
||
#[AsPeriodicTask(frequency: '1 day', from: '2022-01-01', until: '2023-06-12')] | ||
class SendDailySalesReports | ||
{ | ||
public function __invoke() | ||
{ | ||
// ... | ||
} | ||
} | ||
|
||
.. note:: | ||
|
||
The ``from`` and ``until`` options are optional. If not defined, the task | ||
will be executed indefinitely. | ||
|
||
The ``#[AsPeriodicTask]`` attribute takes many parameters to customize the trigger:: | ||
|
||
// the frequency can be defined as an integer representing the number of seconds | ||
#[AsPeriodicTask(frequency: 86400)] | ||
|
||
// adds randomly up to 6 seconds to the trigger time to avoid load spikes | ||
#[AsPeriodicTask(frequency: '1 day', jitter: 6)] | ||
|
||
// defines the method name to call instead as well as the arguments to pass to it | ||
#[AsPeriodicTask(frequency: '1 day', method: 'sendEmail', arguments: ['email' => '[email protected]'])] | ||
class SendDailySalesReports | ||
{ | ||
public function sendEmail(string $email): void | ||
{ | ||
// ... | ||
} | ||
} | ||
|
||
// defines the timezone to use | ||
#[AsPeriodicTask(frequency: '1 day', timezone: 'Africa/Malabo')] | ||
|
||
.. versionadded:: 6.4 | ||
|
||
The :class:`Symfony\\Component\\Scheduler\\Attribute\\AsPeriodicTask` attribute | ||
was introduced in Symfony 6.4. | ||
|
||
Custom Triggers | ||
~~~~~~~~~~~~~~~ | ||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Should we talk about ?
frequency
can also be an int (from my understand it's for seconds)from
can be nullableuntil
can be nullablejitter
schedule
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.
Added some other as well!