*/ class TrustedServerRemovedListener implements IEventListener { public function __construct( private readonly IJobList $jobList, ) { } public function handle(Event $event): void { if (!$event instanceof TrustedServerRemovedEvent) { return; } if ($event->getUrl() === null) { return; // safe guard } $this->removeJobsByUrl(RequestSharedSecret::class, $event->getUrl()); $this->removeJobsByUrl(GetSharedSecret::class, $event->getUrl()); } /** * Remove RequestSharedSecret or GetSharedSecret jobs from the job list by their URL. * The jobs are scheduled with url, token, and created as arguments. * Thus, we have to loop over the jobs here and cannot use IJobList.remove. */ private function removeJobsByUrl(string $class, string $url): void { foreach ($this->jobList->getJobsIterator($class, null, 0) as $job) { $arguments = $job->getArgument(); if (isset($arguments['url']) && $arguments['url'] === $url) { try { $this->jobList->removeById($job->getId()); } catch (\Exception) { // Removing the background jobs is optional because they will expire sometime. // Therefore, we are using catch and ignore. } } } } }