Skip to content

[Scheduler] Add runtime schedule modification feature to docs #20982

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: 6.4
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions scheduler.rst
Original file line number Diff line number Diff line change
Expand Up @@ -869,7 +869,7 @@
),
new SendDailySalesReports()
),
);

Check failure on line 872 in scheduler.rst

View workflow job for this annotation

GitHub Actions / Lint (DOCtor-RST)

Please only provide ".. versionadded::" if the version is greater/equal "7.0"

Check failure on line 872 in scheduler.rst

View workflow job for this annotation

GitHub Actions / Lint (DOCtor-RST)

You are not allowed to use version "6.4". Only major version "7" is allowed.

$scheduler = new Scheduler(handlers: [
SendDailySalesReports::class => new SendDailySalesReportsHandler(),
Expand All @@ -890,6 +890,46 @@
use the ``messenger:consume`` command as explained in the previous
section.

Modifying the Schedule at Runtime
---------------------------------

.. versionadded:: 6.4

Modifying the schedule at runtime and recalculating the heap was introduced in Symfony 6.4.

When a recurring message is added to or removed from the schedule,
the scheduler automatically restarts and recalculates the internal trigger heap.
This allows dynamic control over scheduled tasks during runtime.
code::

// src/Scheduler/DynamicScheduleProvider.php
namespace App\Scheduler;

#[AsSchedule('uptoyou')]
class DynamicScheduleProvider implements ScheduleProviderInterface
{
private ?Schedule $schedule = null;

public function getSchedule(): Schedule
{
return $this->schedule ??= (new Schedule())
->with(
// ...
)
;
}

public function clearAndAddMessages(): void
{
// Clear the current schedule (if any) and add new recurring messages
$this->schedule?->clear();
$this->schedule?->add(
RecurringMessage::cron('@hourly', new DoActionMessage()),
RecurringMessage::cron('@daily', new DoAnotherActionMessage()),
);
}
}

Debugging the Schedule
----------------------

Expand Down