Skip to content

Commit def8d6b

Browse files
committed
Add runtime schedule modification feature to docs
Document the new ability introduced in Symfony 6.4 to modify schedules dynamically at runtime. This includes recalculating the internal trigger heap for better control over recurring tasks.
1 parent 6556d97 commit def8d6b

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

scheduler.rst

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -890,6 +890,46 @@ code::
890890
use the ``messenger:consume`` command as explained in the previous
891891
section.
892892

893+
Modifying the Schedule at Runtime
894+
---------------------------------
895+
896+
.. versionadded:: 6.4
897+
898+
Modifying the schedule at runtime and recalculating the heap was introduced in Symfony 6.4.
899+
900+
When a recurring message is added to or removed from the schedule,
901+
the scheduler automatically restarts and recalculates the internal trigger heap.
902+
This allows dynamic control over scheduled tasks during runtime.
903+
code::
904+
905+
// src/Scheduler/DynamicScheduleProvider.php
906+
namespace App\Scheduler;
907+
908+
#[AsSchedule('uptoyou')]
909+
class DynamicScheduleProvider implements ScheduleProviderInterface
910+
{
911+
private ?Schedule $schedule = null;
912+
913+
public function getSchedule(): Schedule
914+
{
915+
return $this->schedule ??= (new Schedule())
916+
->with(
917+
// ...
918+
)
919+
;
920+
}
921+
922+
public function clearAndAddMessages(): void
923+
{
924+
// Clear the current schedule (if any) and add new recurring messages
925+
$this->schedule?->clear();
926+
$this->schedule?->add(
927+
RecurringMessage::cron('@hourly', new DoActionMessage()),
928+
RecurringMessage::cron('@daily', new DoAnotherActionMessage()),
929+
);
930+
}
931+
}
932+
893933
Debugging the Schedule
894934
----------------------
895935

0 commit comments

Comments
 (0)