CommandLoop.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. namespace ModulesGarden\ProxmoxAddon\Core\CommandLine;
  3. use Symfony\Component\Console\Input\InputInterface;
  4. use Symfony\Component\Console\Output\OutputInterface;
  5. use Symfony\Component\Console\Style\SymfonyStyle;
  6. class CommandLoop extends AbstractCommand
  7. {
  8. /**
  9. * Loop interval in seconds
  10. * @var int
  11. */
  12. protected $loopInterval = 5;
  13. /**
  14. * Loop counter
  15. * @var int
  16. * @TODO remove me
  17. */
  18. private $loopCounter = 0;
  19. /**
  20. *
  21. */
  22. final protected function configure()
  23. {
  24. parent::configure();
  25. }
  26. /**
  27. * @param InputInterface $input
  28. * @param OutputInterface $output
  29. * @return int|null|void
  30. * @throws \Exception
  31. */
  32. final protected function execute(InputInterface $input, OutputInterface $output)
  33. {
  34. $hypervisor = new Hypervisor($this->getName(), $input->getOptions());
  35. /**
  36. * Lock command in database. We want to run only one command in the same time
  37. */
  38. $hypervisor->lock();
  39. do
  40. {
  41. /**
  42. * Let's do something funny!
  43. */
  44. parent::execute($input, $output);
  45. /**
  46. * Ping... Pong... Ping...
  47. */
  48. $hypervisor->ping();
  49. /**
  50. * Time to sleep!
  51. */
  52. $hypervisor->sleep($this->loopInterval);
  53. /**
  54. * @TODO remove me
  55. */
  56. $this->loopCounter++;
  57. }
  58. while (!$hypervisor->shouldBeStopped());
  59. /**
  60. * Unlock command in database
  61. */
  62. $hypervisor->unlock();
  63. }
  64. /**
  65. * @return int
  66. */
  67. final protected function getLoopCounter()
  68. {
  69. return $this->loopCounter;
  70. }
  71. }
  72. //