RunParallel.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. <?php
  2. namespace FD3;
  3. use Symfony\Component\Console\Command\Command;
  4. use Symfony\Component\Console\Input\InputInterface;
  5. use Symfony\Component\Console\Input\InputArgument;
  6. use Symfony\Component\Console\Input\InputOption;
  7. use Symfony\Component\Console\Output\OutputInterface;
  8. use Symfony\Component\Process\Process;
  9. use Symfony\Component\Process\Exception\ProcessFailedException;
  10. class RunParallel extends Command
  11. {
  12. protected function configure()
  13. {
  14. $this
  15. ->setName('run:parallel')
  16. ->setDescription('Run in parallel from the directory of the repo using a ini file.')
  17. ->setHelp('This command allows you to tag and change the required code on the composer.json file...')
  18. ->addArgument('ini_file', InputArgument::REQUIRED, 'The ini file from where to get the source code config.')
  19. ->addArgument('cmd', InputArgument::REQUIRED, 'the command to run in parallel');
  20. }
  21. protected function execute(InputInterface $input, OutputInterface $output)
  22. {
  23. $file = $input->getArgument("ini_file");
  24. $initial_dir = getcwd();
  25. $realpath = realpath($file);
  26. $dirname = dirname($realpath);
  27. if (!chdir($dirname)) {
  28. throw new \Exception("Can't change working directory to " . $dirname);
  29. }
  30. $content = parse_ini_file($realpath, true);
  31. $command = $input->getArgument("cmd");
  32. $p_list = array();
  33. foreach ($content as $sec => $conf) {
  34. chdir($dirname . "/" . $sec);
  35. $output->writeln($sec . "-> " . $command);
  36. $process = new Process($command);
  37. //$process->setTimeout(600);
  38. //$process->setIdleTimeout(6);
  39. $p_list[$sec] = $process;
  40. $process->start();
  41. }
  42. foreach ($content as $sec => $conf) {
  43. $process = $p_list[$sec];
  44. $process->wait();
  45. $output->writeln("<info>" . $sec . "</info>");
  46. $output->write($process->getOutput() . "\n");
  47. }
  48. chdir($initial_dir);
  49. }
  50. }