@@ -205,42 +205,6 @@ Then, define the trigger date/time using the same syntax as the
205
205
206
206
Since version 6.4, it is now possible to add and define a timezone as a 3rd argument.
207
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
-
243
-
244
208
.. tip ::
245
209
246
210
Check out the `crontab.guru website `_ if you need help to construct/understand
@@ -258,6 +222,10 @@ For example::
258
222
259
223
RecurringMessage::cron('@daily', new Message());
260
224
225
+ .. tip ::
226
+
227
+ You can also define cron tasks using :ref: `the AsCronTask attribute <scheduler-attributes-cron-task >`.
228
+
261
229
Hashed Cron Expressions
262
230
.......................
263
231
@@ -321,54 +289,9 @@ defined by PHP datetime functions::
321
289
$until = '2023-06-12';
322
290
RecurringMessage::every('first Monday of next month', new Message(), $from, $until);
323
291
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
292
+ .. tip ::
369
293
370
- The :class: `Symfony\\ Component\\ Scheduler\\ Attribute\\ AsPeriodicTask ` attribute
371
- was introduced in Symfony 6.4.
294
+ You can also define periodic tasks using :ref: `the AsPeriodicTask attribute <scheduler-attributes-periodic-task >`.
372
295
373
296
Custom Triggers
374
297
~~~~~~~~~~~~~~~
@@ -516,17 +439,94 @@ be used. Also, by default, the ``__invoke`` method of your service will be calle
516
439
but, it's also possible to specify the method to call via the ``method``option
517
440
and you can define arguments via ``arguments``option if necessary.
518
441
519
- The distinction between these two attributes lies in the trigger options:
442
+ .. scheduler-attributes-cron-task::
443
+
444
+ ``AsCronTask `` Example
445
+ ......................
446
+
447
+ This is the most basic way to define a cron trigger with this attribute::
448
+
449
+ // src/Scheduler/Task/SendDailySalesReports.php
450
+ namespace App\Scheduler\Task;
451
+
452
+ use Symfony\Component\Scheduler\Attribute\AsCronTask;
453
+
454
+ #[AsCronTask('0 0 * * *')]
455
+ class SendDailySalesReports
456
+ {
457
+ public function __invoke()
458
+ {
459
+ // ...
460
+ }
461
+ }
462
+
463
+ The attribute takes more parameters to customize the trigger::
520
464
521
- * :class:`Symfony\\Component\\Scheduler\\Attribute\\AsPeriodicTask` attribute
522
- defines the following trigger options: ``frequencies ``, ``from ``, ``until `` and
523
- ``jitter ``,
524
- * :class: `Symfony\\ Component\\ Scheduler\\ Attribute\\ AsCronTask ` attribute
525
- defines the following trigger options: ``expression ``, ``jitter ``.
465
+ // adds randomly up to 6 seconds to the trigger time to avoid load spikes
466
+ #[AsCronTask('0 0 * * *', jitter: 6)]
526
467
527
- By defining one of these two attributes, it enables the execution of your
528
- service or command, considering all the options that have been specified within
529
- the attributes.
468
+ // defines the method name to call instead as well as the arguments to pass to it
469
+ #[AsCronTask('0 0 * * *', method: 'sendEmail', arguments: ['email' => '[email protected] '])]
470
+
471
+ // defines the timezone to use
472
+ #[AsCronTask('0 0 * * *', timezone: 'Africa/Malabo')]
473
+
474
+ .. versionadded :: 6.4
475
+
476
+ The :class: `Symfony\\ Component\\ Scheduler\\ Attribute\\ AsCronTask ` attribute
477
+ was introduced in Symfony 6.4.
478
+
479
+ .. scheduler-attributes-periodic-task ::
480
+
481
+ ``AsPeriodicTask `` Example
482
+ ..........................
483
+
484
+ This is the most basic way to define a periodic trigger with this attribute::
485
+
486
+ // src/Scheduler/Task/SendDailySalesReports.php
487
+ namespace App\Scheduler\Task;
488
+
489
+ use Symfony\Component\Scheduler\Attribute\AsPeriodicTask;
490
+
491
+ #[AsPeriodicTask(frequency: '1 day', from: '2022-01-01', until: '2023-06-12')]
492
+ class SendDailySalesReports
493
+ {
494
+ public function __invoke()
495
+ {
496
+ // ...
497
+ }
498
+ }
499
+
500
+ .. note ::
501
+
502
+ The ``from `` and ``until `` options are optional. If not defined, the task
503
+ will be executed indefinitely.
504
+
505
+ The ``#[AsPeriodicTask] `` attribute takes many parameters to customize the trigger::
506
+
507
+ // the frequency can be defined as an integer representing the number of seconds
508
+ #[AsPeriodicTask(frequency: 86400)]
509
+
510
+ // adds randomly up to 6 seconds to the trigger time to avoid load spikes
511
+ #[AsPeriodicTask(frequency: '1 day', jitter: 6)]
512
+
513
+ // defines the method name to call instead as well as the arguments to pass to it
514
+ #[AsPeriodicTask(frequency: '1 day', method: 'sendEmail', arguments: ['email' => '[email protected] '])]
515
+ class SendDailySalesReports
516
+ {
517
+ public function sendEmail(string $email): void
518
+ {
519
+ // ...
520
+ }
521
+ }
522
+
523
+ // defines the timezone to use
524
+ #[AsPeriodicTask(frequency: '1 day', timezone: 'Africa/Malabo')]
525
+
526
+ .. versionadded :: 6.4
527
+
528
+ The :class: `Symfony\\ Component\\ Scheduler\\ Attribute\\ AsPeriodicTask ` attribute
529
+ was introduced in Symfony 6.4.
530
530
531
531
Managing Scheduled Messages
532
532
---------------------------
0 commit comments