@@ -96,6 +96,8 @@ Another important difference is that messages in the Scheduler component are
9696recurring. They are represented via the :class: `Symfony\\ Component\\ Scheduler\\ RecurringMessage `
9797class.
9898
99+ .. _scheduler_attaching-recurring-messages :
100+
99101Attaching Recurring Messages to a Schedule
100102------------------------------------------
101103
@@ -180,6 +182,8 @@ methods::
180182Most of them can be created via the :class: `Symfony\\ Component\\ Scheduler\\ RecurringMessage `
181183class, as shown in the following examples.
182184
185+ .. _scheduler_cron-expression-triggers :
186+
183187Cron 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' => 'admin@symfony.com'])]
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+
267309Periodical 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' => 'admin@symfony.com'])]
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+
282373Custom Triggers
283374~~~~~~~~~~~~~~~
284375
0 commit comments