Repository: symfony/symfony
File: src/Symfony/Component/Messenger/Bridge/Doctrine/EventListener/PostgreSqlNotifyOnIdleListener.php (PHP)

Code before:
        }
    }

    public function onWorkerRunning(WorkerRunningEvent $event): void
    {
        if (!$event->isWorkerIdle() || !$this->activeConnection) {
            return;
        }

        $config = $this->activeConnection->getConfiguration();

        if (0 >= $timeout = $config['get_notify_timeout']) {
            return;
        }

        // Cap by earliest delayed message: wake up in time to process it
        if (null !== $earliest = $this->activeConnection->getEarliestDelayedMessageTime()) {
            $now = $this->clock->now();
            $deadline = ($earliest->format('U.u') - $now->format('U.u')) * 1000;
            if (0 >= $timeout = min($timeout, $deadline)) {
                return;
            }
        }

        // Cap by worker deadline (--time-limit)
        if (null !== $this->deadline) {
            $deadline = ($this->deadline - $this->clock->now()->format('U.u')) * 1000;
            if (0 >= $timeout = min($timeout, $deadline)) {
                return;
            }
        }

        // Cap by sleep duration when non-PG transports are present to ensure they are still polled regularly
        if (0 >= $timeout = min($timeout, $this->sleepCapMs ?? $timeout)) {
            return;
        }

        $this->logger?->debug('Worker waiting for PostgreSQL LISTEN/NOTIFY wake-up.', ['timeout_ms' => $timeout]);

        $this->activeConnection->waitForNotify((int) $timeout);
    }

    public static function getSubscribedEvents(): array
    {
        return [
            WorkerStartedEvent::class => 'onWorkerStarted',
            WorkerRunningEvent::class => 'onWorkerRunning',
        ];
    }

    /**

Review comment (suggestion):
#optional

It would be nice to put this if-condition higher (to avoid a SQL query in `getEarliestDelayedMessageTime()` when the worker is already past its `--time-limit`). But this is "extreme cosmetics", so I'm not to fussed about it.

Code after:
            $connection->listen(null === $this->activeConnection);
            $this->activeConnection ??= $connection;
        }
    }

    public function onWorkerRunning(WorkerRunningEvent $event): void
    {
        if (!$event->isWorkerIdle() || !$this->activeConnection) {
            return;
        }

        $config = $this->activeConnection->getConfiguration();

        if (0 >= $timeout = $config['get_notify_timeout']) {
            return;
        }

        // Cap by worker deadline (--time-limit) — checked first to avoid a SQL query
        // in getEarliestDelayedMessageTime() when the worker is already past its deadline.
        if (null !== $this->deadline) {
            $deadline = ($this->deadline - $this->clock->now()->format('U.u')) * 1000;
            if (0 >= $timeout = min($timeout, $deadline)) {
                return;
            }
        }

        // Cap by earliest delayed message across all PG queues: wake up in time to process it
        if (null !== $earliest = $this->getEarliestDelayedMessageTime()) {
            $now = $this->clock->now();
            $msUntilEarliest = ($earliest->format('U.u') - $now->format('U.u')) * 1000;
            if (0 >= $timeout = min($timeout, $msUntilEarliest)) {
                return;
            }
        }

        // Cap by sleep duration when non-PG transports are present to ensure they are still polled regularly
        if (0 >= $timeout = (int) min($timeout, $this->sleepCapMs ?? $timeout)) {
            return;
        }

        $this->logger?->debug('Worker waiting for PostgreSQL LISTEN/NOTIFY wake-up.', ['timeout_ms' => $timeout]);

        $this->activeConnection->waitForNotify($timeout);
    }

    public static function getSubscribedEvents(): array
    {
        return [
            WorkerStartedEvent::class => 'onWorkerStarted',
            WorkerRunningEvent::class => 'onWorkerRunning',
        ];