<?php
declare(strict_types=1);
namespace App\EventsSubscriber\Worker;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Messenger\Event\WorkerRunningEvent;
/**
* Writes a heartbeat file on every messenger worker loop (WorkerRunningEvent fires each iteration,
* even when the worker is idle). The scheduler container's ECS health check reads this file's mtime,
* so a HUNG-but-running consumer (running != consuming — the failure mode behind the silent scheduler
* outages) goes stale and ECS replaces the task instead of leaving it wedged.
*/
class WorkerHeartbeatSubscriber implements EventSubscriberInterface
{
public const HEARTBEAT_FILE = '/tmp/worker-heartbeat';
public function onWorkerRunning(WorkerRunningEvent $event): void
{
@touch(self::HEARTBEAT_FILE);
}
public static function getSubscribedEvents(): array
{
return [
WorkerRunningEvent::class => 'onWorkerRunning',
];
}
}