@@ -96,6 +96,8 @@ Another important difference is that messages in the Scheduler component are
96
96
recurring. They are represented via the :class: `Symfony\\ Component\\ Scheduler\\ RecurringMessage `
97
97
class.
98
98
99
+ .. _scheduler_attaching-recurring-messages :
100
+
99
101
Attaching Recurring Messages to a Schedule
100
102
------------------------------------------
101
103
@@ -180,6 +182,8 @@ methods::
180
182
Most of them can be created via the :class: `Symfony\\ Component\\ Scheduler\\ RecurringMessage `
181
183
class, as shown in the following examples.
182
184
185
+ .. _scheduler_cron-expression-triggers :
186
+
183
187
Cron Expression Triggers
184
188
~~~~~~~~~~~~~~~~~~~~~~~~
185
189
@@ -199,7 +203,43 @@ Then, define the trigger date/time using the same syntax as the
199
203
200
204
.. versionadded :: 6.4
201
205
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
+
203
243
204
244
.. tip ::
205
245
@@ -264,6 +304,8 @@ For example::
264
304
The day of month range is ``1-28 ``, this is to account for February
265
305
which has a minimum of 28 days.
266
306
307
+ .. _scheduler_periodical-triggers :
308
+
267
309
Periodical Triggers
268
310
~~~~~~~~~~~~~~~~~~~
269
311
@@ -279,6 +321,55 @@ defined by PHP datetime functions::
279
321
$until = '2023-06-12';
280
322
RecurringMessage::every('first Monday of next month', new Message(), $from, $until);
281
323
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
+
282
373
Custom Triggers
283
374
~~~~~~~~~~~~~~~
284
375
0 commit comments