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

Skip to content

Commit f41721c

Browse files
alamiraultjaviereguiluz
authored andcommitted
[Scheduler] Document hashed Cron expression
1 parent 8ac82c9 commit f41721c

File tree

1 file changed

+67
-15
lines changed

1 file changed

+67
-15
lines changed

scheduler.rst

Lines changed: 67 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ the task of creating a report::
4848

4949
class SendDailySalesReports
5050
{
51-
public function __construct(private string $id) {}
51+
public function __construct(private int $id) {}
5252

5353
public function getId(): int
5454
{
@@ -61,6 +61,9 @@ Next, create the handler that processes that kind of message::
6161
// src/Scheduler/Handler/SendDailySalesReportsHandler.php
6262
namespace App\Scheduler\Handler;
6363

64+
use App\Scheduler\Message\SendDailySalesReports;
65+
use Symfony\Component\Messenger\Attribute\AsMessageHandler;
66+
6467
#[AsMessageHandler]
6568
class SendDailySalesReportsHandler
6669
{
@@ -105,9 +108,13 @@ The :class:`Symfony\\Component\\Scheduler\\Attribute\\AsSchedule` attribute,
105108
which by default references the schedule named ``default``, allows you to register
106109
on a particular schedule::
107110

108-
// src/Scheduler/MyScheduleProvider.php
111+
// src/Scheduler/SaleTaskProvider.php
109112
namespace App\Scheduler;
110113

114+
use Symfony\Component\Scheduler\Attribute\AsSchedule;
115+
use Symfony\Component\Scheduler\Schedule;
116+
use Symfony\Component\Scheduler\ScheduleProviderInterface;
117+
111118
#[AsSchedule]
112119
class SaleTaskProvider implements ScheduleProviderInterface
113120
{
@@ -168,22 +175,67 @@ Then, define the trigger date/time using the same syntax as the
168175

169176
RecurringMessage::cron('* * * * *', new Message());
170177

178+
.. tip::
179+
180+
Check out the `crontab.guru website`_ if you need help to construct/understand
181+
cron expressions.
182+
171183
You can also use some special values that represent common cron expressions:
172184

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 * * * *``
178190

179191
For example::
180192

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+
====================== ========================================================================
182230

183-
.. tip::
231+
For example::
184232

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.
187239

188240
.. versionadded:: 6.4
189241

@@ -260,7 +312,7 @@ Then, define your recurring message::
260312

261313
Finally, the recurring messages has to be attached to a schedule::
262314

263-
// src/Scheduler/MyScheduleProvider.php
315+
// src/Scheduler/SaleTaskProvider.php
264316
namespace App\Scheduler;
265317

266318
#[AsSchedule('uptoyou')]
@@ -344,7 +396,7 @@ via the ``stateful`` option (and the :doc:`Cache component </components/cache>`)
344396
This way, when it wakes up again, it looks at all the dates and can catch up on
345397
what it missed::
346398

347-
// src/Scheduler/MyScheduleProvider.php
399+
// src/Scheduler/SaleTaskProvider.php
348400
namespace App\Scheduler;
349401

350402
#[AsSchedule('uptoyou')]
@@ -366,7 +418,7 @@ To scale your schedules more effectively, you can use multiple workers. In such
366418
cases, a good practice is to add a :doc:`lock </components/lock>` to prevent the
367419
same task more than once::
368420

369-
// src/Scheduler/MyScheduleProvider.php
421+
// src/Scheduler/SaleTaskProvider.php
370422
namespace App\Scheduler;
371423

372424
#[AsSchedule('uptoyou')]
@@ -395,7 +447,7 @@ your message in a :class:`Symfony\\Component\\Messenger\\Message\\RedispatchMess
395447
This allows you to specify a transport on which your message will be redispatched
396448
before being further redispatched to its corresponding handler::
397449

398-
// src/Scheduler/MyScheduleProvider.php
450+
// src/Scheduler/SaleTaskProvider.php
399451
namespace App\Scheduler;
400452

401453
#[AsSchedule('uptoyou')]

0 commit comments

Comments
 (0)