src/EventsSubscriber/Worker/WorkerHeartbeatSubscriber.php line 20

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\EventsSubscriber\Worker;
  4. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  5. use Symfony\Component\Messenger\Event\WorkerRunningEvent;
  6. /**
  7.  * Writes a heartbeat file on every messenger worker loop (WorkerRunningEvent fires each iteration,
  8.  * even when the worker is idle). The scheduler container's ECS health check reads this file's mtime,
  9.  * so a HUNG-but-running consumer (running != consuming — the failure mode behind the silent scheduler
  10.  * outages) goes stale and ECS replaces the task instead of leaving it wedged.
  11.  */
  12. class WorkerHeartbeatSubscriber implements EventSubscriberInterface
  13. {
  14.     public const HEARTBEAT_FILE '/tmp/worker-heartbeat';
  15.     public function onWorkerRunning(WorkerRunningEvent $event): void
  16.     {
  17.         @touch(self::HEARTBEAT_FILE);
  18.     }
  19.     public static function getSubscribedEvents(): array
  20.     {
  21.         return [
  22.             WorkerRunningEvent::class => 'onWorkerRunning',
  23.         ];
  24.     }
  25. }