Skip to content

[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 1 commit into from
Jan 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions reference/attributes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,13 @@ Routing

* :doc:`Route </routing>`

Scheduler
~~~~~~~~~

* :ref:`AsCronTask <scheduler_cron-expression-triggers>`
* :ref:`AsPeriodicTask <scheduler_periodical-triggers>`
* :ref:`AsSchedule <scheduler_attaching-recurring-messages>`

Security
~~~~~~~~

Expand Down
93 changes: 92 additions & 1 deletion scheduler.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
------------------------------------------

Expand Down Expand Up @@ -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
~~~~~~~~~~~~~~~~~~~~~~~~

Expand All @@ -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' => 'admin@symfony.com'])]

// 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::

Expand Down Expand Up @@ -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
~~~~~~~~~~~~~~~~~~~

Expand All @@ -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')]
Copy link
Contributor

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 nullable
until can be nullable
jitter
schedule

Copy link
Member Author

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!

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' => 'admin@symfony.com'])]
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
~~~~~~~~~~~~~~~

Expand Down