Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f44e47b

Browse files
committed
minor #19444 [Scheduler] Add AsCronTask and AsPeriodicTask (alexandre-daubois)
This PR was merged into the 6.4 branch. Discussion ---------- [Scheduler] Add `AsCronTask` and `AsPeriodicTask` Fix #18949 Commits ------- f019732 [Scheduler] Add `AsCronTask` and `AsPeriodicTask`
2 parents d1e1bab + f019732 commit f44e47b

File tree

2 files changed

+99
-1
lines changed

2 files changed

+99
-1
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: 92 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,8 @@ Another important difference is that messages in the Scheduler component are
9696
recurring. They are represented via the :class:`Symfony\\Component\\Scheduler\\RecurringMessage`
9797
class.
9898

99+
.. _scheduler_attaching-recurring-messages:
100+
99101
Attaching Recurring Messages to a Schedule
100102
------------------------------------------
101103

@@ -180,6 +182,8 @@ methods::
180182
Most of them can be created via the :class:`Symfony\\Component\\Scheduler\\RecurringMessage`
181183
class, as shown in the following examples.
182184

185+
.. _scheduler_cron-expression-triggers:
186+
183187
Cron Expression Triggers
184188
~~~~~~~~~~~~~~~~~~~~~~~~
185189

@@ -199,7 +203,43 @@ Then, define the trigger date/time using the same syntax as the
199203

200204
.. versionadded:: 6.4
201205

202-
Since version 6.4, it is now possible to add and define a timezone as a 3rd argument
206+
Since version 6.4, it is now possible to add and define a timezone as a 3rd argument.
207+
208+
Another way of declaring cron triggers is to use the
209+
:class:`Symfony\\Component\\Scheduler\\Attribute\\AsCronTask` attribute
210+
on an invokable class::
211+
212+
// src/Scheduler/Task/SendDailySalesReports.php
213+
namespace App\Scheduler\Task;
214+
215+
use Symfony\Component\Scheduler\Attribute\AsCronTask;
216+
217+
#[AsCronTask('0 0 * * *')]
218+
class SendDailySalesReports
219+
{
220+
public function __invoke()
221+
{
222+
// ...
223+
}
224+
}
225+
226+
This is the most basic way to define a cron trigger. However, the attribute
227+
takes more parameters to customize the trigger::
228+
229+
// adds randomly up to 6 seconds to the trigger time to avoid load spikes
230+
#[AsCronTask('0 0 * * *', jitter: 6)]
231+
232+
// defines the method name to call instead as well as the arguments to pass to it
233+
#[AsCronTask('0 0 * * *', method: 'sendEmail', arguments: ['email' => '[email protected]'])]
234+
235+
// defines the timezone to use
236+
#[AsCronTask('0 0 * * *', timezone: 'Africa/Malabo')]
237+
238+
.. versionadded:: 6.4
239+
240+
The :class:`Symfony\\Component\\Scheduler\\Attribute\\AsCronTask` attribute
241+
was introduced in Symfony 6.4.
242+
203243

204244
.. tip::
205245

@@ -264,6 +304,8 @@ For example::
264304
The day of month range is ``1-28``, this is to account for February
265305
which has a minimum of 28 days.
266306

307+
.. _scheduler_periodical-triggers:
308+
267309
Periodical Triggers
268310
~~~~~~~~~~~~~~~~~~~
269311

@@ -279,6 +321,55 @@ defined by PHP datetime functions::
279321
$until = '2023-06-12';
280322
RecurringMessage::every('first Monday of next month', new Message(), $from, $until);
281323

324+
Like cron triggers, you can also use the
325+
:class:`Symfony\\Component\\Scheduler\\Attribute\\AsPeriodicTask` attribute
326+
on an invokable class::
327+
328+
// src/Scheduler/Task/SendDailySalesReports.php
329+
namespace App\Scheduler\Task;
330+
331+
use Symfony\Component\Scheduler\Attribute\AsPeriodicTask;
332+
333+
#[AsPeriodicTask(frequency: '1 day', from: '2022-01-01', until: '2023-06-12')]
334+
class SendDailySalesReports
335+
{
336+
public function __invoke()
337+
{
338+
// ...
339+
}
340+
}
341+
342+
.. note::
343+
344+
The ``from`` and ``until`` options are optional. If not defined, the task
345+
will be executed indefinitely.
346+
347+
The ``#[AsPeriodicTask]`` attribute takes many parameters to customize the trigger::
348+
349+
// the frequency can be defined as an integer representing the number of seconds
350+
#[AsPeriodicTask(frequency: 86400)]
351+
352+
// adds randomly up to 6 seconds to the trigger time to avoid load spikes
353+
#[AsPeriodicTask(frequency: '1 day', jitter: 6)]
354+
355+
// defines the method name to call instead as well as the arguments to pass to it
356+
#[AsPeriodicTask(frequency: '1 day', method: 'sendEmail', arguments: ['email' => '[email protected]'])]
357+
class SendDailySalesReports
358+
{
359+
public function sendEmail(string $email): void
360+
{
361+
// ...
362+
}
363+
}
364+
365+
// defines the timezone to use
366+
#[AsPeriodicTask(frequency: '1 day', timezone: 'Africa/Malabo')]
367+
368+
.. versionadded:: 6.4
369+
370+
The :class:`Symfony\\Component\\Scheduler\\Attribute\\AsPeriodicTask` attribute
371+
was introduced in Symfony 6.4.
372+
282373
Custom Triggers
283374
~~~~~~~~~~~~~~~
284375

0 commit comments

Comments
 (0)