/** * {@inheritDoc} */ public function execute(InputInterface $input, OutputInterface $output) { $command = $input->getArgument("threadCommand"); $type = $input->getArgument("type"); $maxRunCount = $input->getArgument("maxRunCount"); $this->runner = $this->runnerFactory->createRunner($type, "", $maxRunCount); $this->runner->execute($command); }
/** * {@inheritDoc} */ public function execute(InputInterface $input, OutputInterface $output) { // save the output for writing to later $this->output = $output; // setup run counting if required $maxRuns = -1; // -1 = run continuously if (!empty($input->getOption("testRuns"))) { $maxRuns = $input->getOption("testRuns"); } $runCount = 0; while ($maxRuns == -1 || $runCount < $maxRuns) { ++$runCount; try { foreach ($this->serviceConfig as $service => $config) { // create a pool for this service if necessary if (!isset($this->pool[$service])) { $this->pool[$service] = []; } foreach ($this->pool[$service] as $index => $instance) { /** @var RunnerInterface $runner */ $runner = $instance["runner"]; if (!$runner->isRunning()) { $output->writeln("<comment>Removing dead instance for {$service}</comment>"); // clean the pid file $pid = $this->pidFactory->create($instance["pidFile"]); $pid->cleanPid(); unset($this->pool[$service][$index]); } } // reindex pool so we don't end up with massive index values $this->pool[$service] = array_values($this->pool[$service]); $count = count($this->pool[$service]); $output->writeln("<info>{$count} instances running for {$service}</info>"); if ($count >= $config["instances"]) { // enough instances of this service for now continue; } // create command as an array $pidFile = $service . "-" . $count . ".pid"; $type = $config["type"]; $maxRunCount = empty($config["maxRunCount"]) ? 0 : (int) $config["maxRunCount"]; // running a command that runs a command $command = [$this->threadCommandName, $config["command"], $type, $maxRunCount]; // create and execute runner, then add to the pool $output->writeln("<comment>Creating new instance for {$service}, PID file: {$pidFile}</comment>"); $runner = $this->runnerFactory->createRunner("console", $pidFile, 1); $runner->execute(implode(" ", $command), false); $this->pool[$service][] = ["runner" => $runner, "pidFile" => $pidFile]; } } catch (\Exception $e) { $output->writeln("<error>{$e->getMessage()}</error>"); } $output->writeln("<info>Completed run #{$runCount} of {$maxRuns}</info>"); // process any signals (to catch "stop" requests) pcntl_signal_dispatch(); // sleep and loop usleep($this->poolRefreshInterval); } }
public function execute(InputInterface $input, OutputInterface $output) { $action = $input->getArgument("action"); switch ($action) { case "restart": case "stop": if ($this->isPoolRunning()) { // KILL, KILL, KILL!! $pid = $this->getPid(); // counter to prevent infinite loops $maxCount = 1000; $interval = $this->waitTimeout / $maxCount; $count = 0; // send the terminate signal $result = posix_kill($pid, SIGUSR1); if ($result === false) { throw new ProcessException("Could not send the terminate command to the pool, {$pid}"); } do { usleep($interval); ++$count; } while ($this->isPoolRunning() && $count < $maxCount); // check if we exited an infinite loop if ($count >= $maxCount) { throw new ProcessException("Could not stop the pool"); } $output->writeln("<info>Pool stopped</info>"); } else { $output->writeln("<info>The pool was not running</info>"); } if ($action == "stop") { break; } // if "restart" then fall through // no break // if "restart" then fall through // no break case "start": // check who we're running as if (posix_getuid() === 0 && !$this->canRunAsRoot) { throw new ProcessException("Cannot run the pool as the root user"); } // start the pool in a new process if ($this->isPoolRunning()) { throw new ProcessException("Pool is already running"); } $runner = $this->runnerFactory->createRunner("console", $this->poolPidFile, 1); $runner->execute($this->poolCommand, false); $output->writeln("<info>Pool started</info>"); break; } }
public function testUndefinedRunnertype() { $factory = new RunnerFactory($this->pidFactory, $this->consolePath); $type = "unknown"; try { $factory->createRunner($type); } catch (RunnerException $e) { $this->assertContains($type, $e->getMessage()); } }