@@ -48,7 +48,7 @@ the task of creating a report::
48
48
49
49
class SendDailySalesReports
50
50
{
51
- public function __construct(private string $id) {}
51
+ public function __construct(private int $id) {}
52
52
53
53
public function getId(): int
54
54
{
@@ -61,6 +61,9 @@ Next, create the handler that processes that kind of message::
61
61
// src/Scheduler/Handler/SendDailySalesReportsHandler.php
62
62
namespace App\Scheduler\Handler;
63
63
64
+ use App\Scheduler\Message\SendDailySalesReports;
65
+ use Symfony\Component\Messenger\Attribute\AsMessageHandler;
66
+
64
67
#[AsMessageHandler]
65
68
class SendDailySalesReportsHandler
66
69
{
@@ -105,9 +108,13 @@ The :class:`Symfony\\Component\\Scheduler\\Attribute\\AsSchedule` attribute,
105
108
which by default references the schedule named ``default ``, allows you to register
106
109
on a particular schedule::
107
110
108
- // src/Scheduler/MyScheduleProvider .php
111
+ // src/Scheduler/SaleTaskProvider .php
109
112
namespace App\Scheduler;
110
113
114
+ use Symfony\Component\Scheduler\Attribute\AsSchedule;
115
+ use Symfony\Component\Scheduler\Schedule;
116
+ use Symfony\Component\Scheduler\ScheduleProviderInterface;
117
+
111
118
#[AsSchedule]
112
119
class SaleTaskProvider implements ScheduleProviderInterface
113
120
{
@@ -168,22 +175,67 @@ Then, define the trigger date/time using the same syntax as the
168
175
169
176
RecurringMessage::cron('* * * * *', new Message());
170
177
178
+ .. tip ::
179
+
180
+ Check out the `crontab.guru website `_ if you need help to construct/understand
181
+ cron expressions.
182
+
171
183
You can also use some special values that represent common cron expressions:
172
184
173
- * ``# yearly ``, ``# annually `` - Run once a year, midnight, Jan. 1 - ``0 0 1 1 * ``
174
- * ``# monthly `` - Run once a month, midnight, first of month - ``0 0 1 * * ``
175
- * ``# weekly `` - Run once a week, midnight on Sun - ``0 0 * * 0 ``
176
- * ``# daily ``, ``# midnight `` - Run once a day, midnight - ``0 0 * * * ``
177
- * ``# hourly `` - Run once an hour, first minute - ``0 * * * * ``
185
+ * ``@ yearly ``, ``@ annually `` - Run once a year, midnight, Jan. 1 - ``0 0 1 1 * ``
186
+ * ``@ monthly `` - Run once a month, midnight, first of month - ``0 0 1 * * ``
187
+ * ``@ weekly `` - Run once a week, midnight on Sun - ``0 0 * * 0 ``
188
+ * ``@ daily ``, ``@ midnight `` - Run once a day, midnight - ``0 0 * * * ``
189
+ * ``@ hourly `` - Run once an hour, first minute - ``0 * * * * ``
178
190
179
191
For example::
180
192
181
- RecurringMessage::cron('#daily', new Message());
193
+ RecurringMessage::cron('@daily', new Message());
194
+
195
+ Hashed Cron Expression
196
+ ~~~~~~~~~~~~~~~~~~~~~~
197
+
198
+ If you have many trigger scheduled at same time (for example, at midnight, ``0 0 * * * ``)
199
+ this will create a very long running schedules list right at this time.
200
+ This may cause an issue if a task has a memory leak.
201
+
202
+ You can add a ``#``(for hash) symbol in expression to generate random value. The value
203
+ is deterministic based on the message. This means that while the value is random, it is
204
+ predictable and consistent. A message with string representation ``my task ``
205
+ and a defined frequency of ``# # * * * `` will have an idempotent frequency
206
+ of ``56 20 * * * `` (every day at 8:56pm).
207
+
208
+ A hash range ``#(x-y) `` can also be used. For example, ``# #(0-7) * * * `` means daily,
209
+ some time between midnight and 7am. Using the ``# `` without a range creates a range
210
+ of any valid value for the field. ``# # # # # `` is short for ``#(0-59) #(0-23) #(1-28)
211
+ #(1-12) #(0-6) ``.
212
+
213
+ You can also use some special values that represent common hashed cron expressions:
214
+
215
+ ====================== ========================================================================
216
+ Alias Converts to
217
+ ====================== ========================================================================
218
+ ``#hourly `` ``# * * * * `` (at some minute every hour)
219
+ ``#daily `` ``# # * * * `` (at some time every day)
220
+ ``#weekly `` ``# # * * # `` (at some time every week)
221
+ ``#weekly@midnight `` ``# #(0-2) * * # `` (at ``#midnight `` one day every week)
222
+ ``#monthly `` ``# # # * * `` (at some time on some day, once per month)
223
+ ``#monthly@midnight `` ``# #(0-2) # * * `` (at ``#midnight `` on some day, once per month)
224
+ ``#annually `` ``# # # # * `` (at some time on some day, once per year)
225
+ ``#annually@midnight `` ``# #(0-2) # # * `` (at ``#midnight `` on some day, once per year)
226
+ ``#yearly `` ``# # # # * `` alias for ``#annually ``
227
+ ``#yearly@midnight `` ``# #(0-2) # # * `` alias for ``#annually@midnight ``
228
+ ``#midnight `` ``# #(0-2) * * * `` (at some time between midnight and 2:59am, every day)
229
+ ====================== ========================================================================
182
230
183
- .. tip ::
231
+ For example ::
184
232
185
- Check out the `crontab.guru website `_ if you need help to construct/understand
186
- cron expressions.
233
+ RecurringMessage::cron('#midnight', new Message());
234
+
235
+ .. note ::
236
+
237
+ The day of month range is ``1-28 ``, this is to account for February
238
+ which has a minimum of 28 days.
187
239
188
240
.. versionadded :: 6.4
189
241
@@ -260,7 +312,7 @@ Then, define your recurring message::
260
312
261
313
Finally, the recurring messages has to be attached to a schedule::
262
314
263
- // src/Scheduler/MyScheduleProvider .php
315
+ // src/Scheduler/SaleTaskProvider .php
264
316
namespace App\Scheduler;
265
317
266
318
#[AsSchedule('uptoyou')]
@@ -344,7 +396,7 @@ via the ``stateful`` option (and the :doc:`Cache component </components/cache>`)
344
396
This way, when it wakes up again, it looks at all the dates and can catch up on
345
397
what it missed::
346
398
347
- // src/Scheduler/MyScheduleProvider .php
399
+ // src/Scheduler/SaleTaskProvider .php
348
400
namespace App\Scheduler;
349
401
350
402
#[AsSchedule('uptoyou')]
@@ -366,7 +418,7 @@ To scale your schedules more effectively, you can use multiple workers. In such
366
418
cases, a good practice is to add a :doc: `lock </components/lock >` to prevent the
367
419
same task more than once::
368
420
369
- // src/Scheduler/MyScheduleProvider .php
421
+ // src/Scheduler/SaleTaskProvider .php
370
422
namespace App\Scheduler;
371
423
372
424
#[AsSchedule('uptoyou')]
@@ -395,7 +447,7 @@ your message in a :class:`Symfony\\Component\\Messenger\\Message\\RedispatchMess
395
447
This allows you to specify a transport on which your message will be redispatched
396
448
before being further redispatched to its corresponding handler::
397
449
398
- // src/Scheduler/MyScheduleProvider .php
450
+ // src/Scheduler/SaleTaskProvider .php
399
451
namespace App\Scheduler;
400
452
401
453
#[AsSchedule('uptoyou')]
0 commit comments