CommandLoop.php 1.7 KB

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