zenstruck / messenger-monitor-bundle

Batteries included UI to monitor your Messenger workers, transports, schedules, and messages.
MIT License
153 stars 16 forks source link

Removal of stopped worker which doesn't dispatch onStop #87

Open chapterjason opened 5 months ago

chapterjason commented 5 months ago

Hey nice bundle.

My workers will restart on file changes in development to reload the used resource inside the worker. It looks like the onStop won't get triggered.

To overcome this, I created a command which I can use to clear the workers. It looks like this:

    $onChange = debounce(static function () {
        run('docker compose stop xxx_worker');
        run ('docker compose exec xxx php bin/console messenger:monitor:purge:worker'); // Custom command as workaround
        run ('docker compose exec xxx php bin/console messenger:monitor:purge --older-than all'); // to only see new messages after reload
        run('docker compose start xxx_worker');
    }, 5000);
#[AsCommand(
    name: 'messenger:monitor:purge:worker',
    description: 'Remove all worker ids from the cache.',
)]
class MessengerMonitorPurgeWorkerCommand extends Command
{
    public function __construct(
        private readonly CacheInterface $cache
    )
    {
        parent::__construct();
    }

    protected function execute(InputInterface $input, OutputInterface $output): int
    {
        $item = $this->cache->getItem("zenstruck_messenger_monitor.worker_ids");
        $item->set([]);
        $this->cache->save($item);

        return Command::SUCCESS;
    }
}

So basically, it would be nice to have a command for that like mine or any other solution to automatically remove workers which aren't really running. The command works in my case, but I am sure in other cases, without restarting the workers, they won't register themselves again.

Chris53897 commented 3 months ago

Could you please provide your symfony version and explain what 'docker compose stop xxx_worker' calls. Does it dispatch WorkerStoppedEvent?

chapterjason commented 3 months ago

Hey @Chris53897, I might wasn't explicit enough. I especially talking about the scenario in which the WorkerStoppedEvent won't get called.

In my case the xxx_worker service container is just a simple php bin/console messenger:consume async -vv. But to be explicit, in my cases the event won't get called as the service will be killed a lot in development, which results in the lose cache objects.