|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Webdevvie\PheanstalkTaskQueueBundle\Command; |
| 4 | + |
| 5 | +use Webdevvie\PheanstalkTaskQueueBundle\Command\AbstractWorker; |
| 6 | +use Symfony\Component\Console\Input\InputInterface; |
| 7 | +use Symfony\Component\Console\Input\InputOption; |
| 8 | +use Symfony\Component\Console\Output\OutputInterface; |
| 9 | +use Webdevvie\PheanstalkTaskQueueBundle\Entity\Task; |
| 10 | + |
| 11 | +/** |
| 12 | + * Class StopWorkerTenderCommand |
| 13 | + * Sends a signal to the worker tender to stop |
| 14 | + * |
| 15 | + * @package Webdevvie\PheanstalkTaskQueueBundle\Command |
| 16 | + * @author John Bakker <[email protected]> |
| 17 | + */ |
| 18 | +class StopWorkerTenderCommand extends AbstractWorker |
| 19 | +{ |
| 20 | + /** |
| 21 | + * {@inheritDoc} |
| 22 | + * |
| 23 | + * @return void |
| 24 | + */ |
| 25 | + protected function configure() |
| 26 | + { |
| 27 | + $this->setName('taskqueue:stop-worker-tender') |
| 28 | + ->setDescription('Signals the worker tender to stop and waits for it to stop'); |
| 29 | + $this->addDefaultConfiguration(); |
| 30 | + } |
| 31 | + |
| 32 | + /** |
| 33 | + * {@inheritDoc} |
| 34 | + * |
| 35 | + * @param InputInterface $input |
| 36 | + * @param OutputInterface $output |
| 37 | + * @return void |
| 38 | + * @throws \InvalidArgumentException |
| 39 | + */ |
| 40 | + protected function execute(InputInterface $input, OutputInterface $output) |
| 41 | + { |
| 42 | + $this->initialiseWorker($input, $output); |
| 43 | + $processes = $this->findWorkerTenderProcessesForTube($this->tube); |
| 44 | + if (count($processes) == 0) { |
| 45 | + $output->writeln("<info>Found no processes</info>"); |
| 46 | + return; |
| 47 | + } |
| 48 | + $output->writeln('<info>Found ' . count($processes) . ' process(es)'); |
| 49 | + foreach ($processes as $process) { |
| 50 | + $output->writeln('<info>sending kill to:</info> ' . $process); |
| 51 | + exec("kill $process"); |
| 52 | + } |
| 53 | + $output->write('<info>Waiting on processes to stop</info> '); |
| 54 | + while (count($processes) > 0) { |
| 55 | + $processes = $this->findWorkerTenderProcessesForTube($this->tube); |
| 56 | + $output->write("."); |
| 57 | + sleep(1); |
| 58 | + |
| 59 | + } |
| 60 | + $output->writeln("Done!"); |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Returns a list of processes that match the worker-tender |
| 65 | + * |
| 66 | + * @param string $tube |
| 67 | + * @return array |
| 68 | + */ |
| 69 | + private function findWorkerTenderProcessesForTube($tube) |
| 70 | + { |
| 71 | + $processes = array(); |
| 72 | + $command = "ps ax "; |
| 73 | + $command .= "|grep php "; |
| 74 | + $command .= "|grep -v stop "; |
| 75 | + $command .= "|grep worker-tender "; |
| 76 | + if ($tube !== '') { |
| 77 | + $command .= "|grep " . escapeshellarg('use-tube=' . $tube); |
| 78 | + } |
| 79 | + $data = exec($command); |
| 80 | + $lines = explode("\n", $data); |
| 81 | + foreach ($lines as $line) { |
| 82 | + $line = trim($line); |
| 83 | + $parts = explode(" ", $line); |
| 84 | + if ($parts[0] === '') { |
| 85 | + continue; |
| 86 | + } |
| 87 | + $processes[] = intval($parts[0]); |
| 88 | + } |
| 89 | + |
| 90 | + return $processes; |
| 91 | + |
| 92 | + } |
| 93 | +} |
0 commit comments