Skip to content

Commit b5bfafe

Browse files
[Scheduler] Add AsCronTask and AsPeriodicTask
1 parent 13aeb58 commit b5bfafe

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed

reference/attributes.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,13 @@ Routing
8686

8787
* :doc:`Route </routing>`
8888

89+
Scheduler
90+
~~~~~~~~~
91+
92+
* :ref:`AsCronTask <scheduler_cron-expression-triggers>`
93+
* :ref:`AsPeriodicTask <scheduler_periodical-triggers>`
94+
* :ref:`AsSchedule <scheduler_attaching-recurring-messages>`
95+
8996
Security
9097
~~~~~~~~
9198

scheduler.rst

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,8 @@ Another important difference is that messages in the Scheduler component are
9393
recurring. They are represented via the :class:`Symfony\\Component\\Scheduler\\RecurringMessage`
9494
class.
9595

96+
.. _scheduler_attaching-recurring-messages:
97+
9698
Attaching Recurring Messages to a Schedule
9799
------------------------------------------
98100

@@ -173,6 +175,8 @@ methods::
173175
Most of them can be created via the :class:`Symfony\\Component\\Scheduler\\RecurringMessage`
174176
class, as shown in the following examples.
175177

178+
.. _scheduler_cron-expression-triggers:
179+
176180
Cron Expression Triggers
177181
~~~~~~~~~~~~~~~~~~~~~~~~
178182

@@ -211,6 +215,34 @@ For example::
211215
Check out the `crontab.guru website`_ if you need help to construct/understand
212216
cron expressions.
213217

218+
Another way of declaring cron triggers is to use the
219+
:class:`Symfony\\Component\\Scheduler\\Attribute\\AsCronTask` attribute
220+
on an invokable class::
221+
222+
// src/Scheduler/Task/SendDailySalesReports.php
223+
namespace App\Scheduler\Task;
224+
225+
use Symfony\Component\Scheduler\Attribute\AsCronTask;
226+
227+
#[AsCronTask('0 0 * * *')]
228+
class SendDailySalesReports
229+
{
230+
public function __invoke()
231+
{
232+
// ...
233+
}
234+
}
235+
236+
If you want to use another method name than ``__invoke()``, you can specify it
237+
by setting the ``method`` parameter of the attribute.
238+
239+
.. versionadded:: 6.4
240+
241+
The :class:`Symfony\\Component\\Scheduler\\Attribute\\AsCronTask` attribute
242+
was introduced in Symfony 6.4.
243+
244+
.. _scheduler_periodical-triggers:
245+
214246
Periodical Triggers
215247
~~~~~~~~~~~~~~~~~~~
216248

@@ -226,6 +258,32 @@ defined by PHP datetime functions::
226258
$until = '2023-06-12';
227259
RecurringMessage::every('first Monday of next month', new Message(), $from, $until);
228260

261+
Like cron triggers, you can also use the
262+
:class:`Symfony\\Component\\Scheduler\\Attribute\\AsPeriodicTask` attribute
263+
on an invokable class::
264+
265+
// src/Scheduler/Task/SendDailySalesReports.php
266+
namespace App\Scheduler\Task;
267+
268+
use Symfony\Component\Scheduler\Attribute\AsPeriodicTask;
269+
270+
#[AsPeriodicTask(frequency: '1 day', from: '2022-01-01', until: '2023-06-12')]
271+
class SendDailySalesReports
272+
{
273+
public function __invoke()
274+
{
275+
// ...
276+
}
277+
}
278+
279+
Or if you want to use another method name than ``__invoke()``, you can specify it
280+
by setting the ``method`` parameter of the attribute.
281+
282+
.. versionadded:: 6.4
283+
284+
The :class:`Symfony\\Component\\Scheduler\\Attribute\\AsPeriodicTask` attribute
285+
was introduced in Symfony 6.4.
286+
229287
Custom Triggers
230288
~~~~~~~~~~~~~~~
231289

0 commit comments

Comments
 (0)