admin管理员组文章数量:1404602
I have a scheduler that sends a recurring message every five minutes to update some cached external data. I added a feature that allows a user to change this interval in the front-end. However, I am unable to change the schedule such that the recurring messages occurs according to the new interval.
I tried to get the existing schedule in order to clear the schedule and add a new recurring message with the new interval. Unfortunately, I am always creating a new schedule instead of getting the existing one. This would be fine if the message handler used the newly created schedule, but it continues to use the old scheduled messages instead. The schedule provider and the class that handles updating the schedule are given below. I also tried to make the schedule variable static, but this did not change my results. Even when using the PreRunEvent
from the schedule's before
function I am unable to get the existing schedule.
#[AsSchedule('update_cache')]
final class UpdateCacheTaskProvider implements ScheduleProviderInterface
{
public function getSchedule(): Schedule
{
return $this->schedule ??= (new Schedule())->with(
RecurringMessage::every($this->getRefreshRateInterval(), new UpdateCacheMessage())
);
}
}
class RefreshRateSubscriber implements EventSubscriberInterface
{
public function __construct(private UpdateCacheTaskProvider $updateCacheTaskProvider) {}
public function onRefreshRateChanged(RefreshRateChangedEvent $event): void
{
$this->updateCacheTaskProvider
->getSchedule()
->clear()
->add(RecurringMessage::every($event->getRefreshRateInterval(), new UpdateCacheMessage()));
}
public static function getSubscribedEvents(): array
{
return [
RefreshRateChangedEvent::NAME => 'onRefreshRateChanged'
];
}
}
本文标签: phpHow to dynamically change the interval of a recurring message using schedulerStack Overflow
版权声明:本文标题:php - How to dynamically change the interval of a recurring message using scheduler? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744811151a2626438.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论