Exemplo n.º 1
0
 /**
  * {@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);
 }
Exemplo n.º 2
0
 public function testExecuteCommand()
 {
     foreach ($this->argList as $arg => $value) {
         $this->input->shouldReceive("getArgument")->withArgs([$arg])->once()->andReturn($value);
     }
     $this->runnerFactory->shouldReceive("createRunner")->withArgs([$this->argList["type"], "", $this->argList["maxRunCount"]])->once();
     $this->runner->shouldReceive("execute")->withArgs([$this->argList["threadCommand"]])->once();
     $command = new ThreadCommand("thread:command", $this->runnerFactory);
     $command->execute($this->input, $this->output);
 }
Exemplo n.º 3
0
 public function setup()
 {
     $this->runner = \Mockery::mock("Silktide\\Teamster\\Pool\\Runner\\RunnerInterface");
     $this->runnerFactory = \Mockery::mock("Silktide\\Teamster\\Pool\\Runner\\RunnerFactory");
     $this->runnerFactory->shouldReceive("createRunner")->andReturn($this->runner);
     $this->pid = \Mockery::mock("Silktide\\Teamster\\Pool\\Pid\\PidInterface")->shouldIgnoreMissing(true);
     $this->pidFactory = \Mockery::mock("Silktide\\Teamster\\Pool\\Pid\\PidFactoryInterface");
     $this->pidFactory->shouldReceive("create")->andReturn($this->pid);
     $this->inputDefinition = \Mockery::mock("Symfony\\Component\\Console\\Input\\InputDefinition");
     $this->input = \Mockery::mock("Symfony\\Component\\Console\\Input\\InputInterface");
     $this->output = \Mockery::mock("Symfony\\Component\\Console\\Output\\OutputInterface")->shouldIgnoreMissing();
     vfsStreamWrapper::register();
     vfsStreamWrapper::setRoot(new vfsStreamDirectory($this->testDir));
 }
Exemplo n.º 4
0
 public function setup()
 {
     $this->runner = \Mockery::mock("Silktide\\Teamster\\Pool\\Runner\\RunnerInterface")->shouldIgnoreMissing();
     $this->runner->shouldReceive("isRunning")->andReturn(true);
     $this->completedRunner = \Mockery::mock("Silktide\\Teamster\\Pool\\Runner\\RunnerInterface")->shouldIgnoreMissing();
     $this->completedRunner->shouldReceive("isRunning")->andReturn(false);
     $this->runnerFactory = \Mockery::mock("Silktide\\Teamster\\Pool\\Runner\\RunnerFactory");
     $this->runnerFactory->shouldReceive("createRunner")->withArgs(["console", "/^(?!completed-).*/", \Mockery::type("int")])->andReturn($this->runner);
     $this->runnerFactory->shouldReceive("createRunner")->withArgs(["console", "/^completed-/", \Mockery::type("int")])->andReturn($this->completedRunner);
     $this->pid = \Mockery::mock("Silktide\\Teamster\\Pool\\Pid\\PidInterface")->shouldIgnoreMissing(true);
     $this->pidFactory = \Mockery::mock("Silktide\\Teamster\\Pool\\Pid\\PidFactoryInterface");
     $this->pidFactory->shouldReceive("create")->andReturn($this->pid);
     $this->inputDefinition = \Mockery::mock("Symfony\\Component\\Console\\Input\\InputDefinition");
     $this->input = \Mockery::mock("Symfony\\Component\\Console\\Input\\InputInterface");
     $this->output = \Mockery::mock("Symfony\\Component\\Console\\Output\\OutputInterface")->shouldIgnoreMissing();
 }
Exemplo n.º 5
0
 /**
  * {@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);
     }
 }
Exemplo n.º 6
0
 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;
     }
 }
Exemplo n.º 7
0
 public function testUndefinedRunnertype()
 {
     $factory = new RunnerFactory($this->pidFactory, $this->consolePath);
     $type = "unknown";
     try {
         $factory->createRunner($type);
     } catch (RunnerException $e) {
         $this->assertContains($type, $e->getMessage());
     }
 }