* * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Symfony\Component\Messenger\Command; use Psr\Cache\CacheItemPoolInterface; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\ConsoleOutputInterface; use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Style\SymfonyStyle; use Symfony\Component\Messenger\EventListener\StopWorkerOnRestartSignalListener; /** * @author Ryan Weaver */ class StopWorkersCommand extends Command { protected static $defaultName = 'messenger:stop-workers'; private $restartSignalCachePool; public function __construct(CacheItemPoolInterface $restartSignalCachePool) { $this->restartSignalCachePool = $restartSignalCachePool; parent::__construct(); } /** * {@inheritdoc} */ protected function configure(): void { $this ->setDefinition([]) ->setDescription('Stop workers after their current message') ->setHelp(<<<'EOF' The %command.name% command sends a signal to stop any messenger:consume processes that are running. php %command.full_name% Each worker command will finish the message they are currently processing and then exit. Worker commands are *not* automatically restarted: that should be handled by a process control system. EOF ) ; } /** * {@inheritdoc} */ protected function execute(InputInterface $input, OutputInterface $output) { $io = new SymfonyStyle($input, $output instanceof ConsoleOutputInterface ? $output->getErrorOutput() : $output); $cacheItem = $this->restartSignalCachePool->getItem(StopWorkerOnRestartSignalListener::RESTART_REQUESTED_TIMESTAMP_KEY); $cacheItem->set(microtime(true)); $this->restartSignalCachePool->save($cacheItem); $io->success('Signal successfully sent to stop any running workers.'); return 0; } }