My recurring tasks are defined in a Doctrine entity. Using the UI, the user can change the task frequency and enable or disable each task:
#[ORM\Entity(repositoryClass: ScheduledTaskRepository::class)]
class ScheduledTask
{
// ...
#[ORM\Column(length: 16)]
public ?string $frequency = null;
#[ORM\Column]
public bool $enabled = true;
public function toRecurringMessage(): RecurringMessage
{
if (null === $this->type || null === $this->frequency) {
throw new \LogicException(static::class.' is not properly initialized.');
}
return RecurringMessage::cron($this->frequency, new SyncEmployees());
}
}
My scheduler, at startup, loads all enabled tasks from the database and adds their recurring messages to the job queue as follows:
#[AsSchedule]
class Schedule implements ScheduleProviderInterface
{
private ?SymfonySchedule $schedule = null;
public function __construct(
private readonly CacheInterface $cache,
private readonly ScheduledTaskRepository $tasks,
) {
}
#[\Override]
public function getSchedule(): SymfonySchedule
{
return $this->schedule ??= new SymfonySchedule()
->stateful($this->cache)
->processOnlyLastMissedRun(true)
->add(
// Add enabled ScheduledTask to the scheduler
...array_map(
fn (ScheduledTask $t) => $t->toRecurringMessage()->withJitter(30),
$this->tasks->allEnabled()
),
)
;
}
}
This works, but it has an important limitation: when a task is disabled, the worker doesn’t know it and continues to execute it. The same happens when a task is enabled: the worker doesn’t pick it up because it only loads tasks at startup.
In other words: how can I instruct the worker — which is managed by Supervisor — to reload itself so it can re-evaluate the list of tasks?
[program:messenger-scheduler]
command=php /app/bin/console messenger:consume scheduler_default --time-limit=3600
user=root
numprocs=1
startsecs=0
autostart=true
autorestart=true
startretries=10
process_name=%(program_name)s_%(process_num)02d