GearmanDescriber.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. /**
  3. * Gearman Bundle for Symfony2
  4. *
  5. * For the full copyright and license information, please view the LICENSE
  6. * file that was distributed with this source code.
  7. *
  8. * Feel free to edit as you please, and have fun.
  9. *
  10. * @author Marc Morera <yuhu@mmoreram.com>
  11. */
  12. namespace Mmoreram\GearmanBundle\Service;
  13. use Symfony\Component\Console\Output\OutputInterface;
  14. use Symfony\Component\HttpKernel\KernelInterface;
  15. /**
  16. * Implementation of GearmanDescriber
  17. *
  18. * @since 2.3.1
  19. */
  20. class GearmanDescriber
  21. {
  22. /**
  23. * @var KernelInterface
  24. *
  25. * Kernel
  26. */
  27. private $kernel;
  28. /**
  29. * Construct method
  30. *
  31. * @param KernelInterface $kernel Kernel
  32. */
  33. public function __construct(KernelInterface $kernel)
  34. {
  35. $this->kernel = $kernel;
  36. }
  37. /**
  38. * Describe Job.
  39. *
  40. * Given a output object and a Job, dscribe it.
  41. *
  42. * @param OutputInterface $output Output object
  43. * @param array $worker Worker array with Job to describe
  44. */
  45. public function describeJob(OutputInterface $output, array $worker)
  46. {
  47. /**
  48. * Commandline
  49. */
  50. $script = $this->kernel->getRootDir() . '/console gearman:job:execute';
  51. /**
  52. * A job descriptions contains its worker description
  53. */
  54. $this->describeWorker($output, $worker);
  55. $job = $worker['job'];
  56. $output->writeln('<info>@job\methodName : ' . $job['methodName'] . '</info>');
  57. $output->writeln('<info>@job\callableName : ' . $job['realCallableName'] . '</info>');
  58. if ($job['jobPrefix']) {
  59. $output->writeln('<info>@job\jobPrefix : ' . $job['jobPrefix'] . '</info>');
  60. }
  61. /**
  62. * Also a complete and clean execution path is given , for supervisord
  63. */
  64. $output->writeln('<info>@job\supervisord : </info><comment>/usr/bin/php ' . $script.' ' . $job['realCallableName'] . ' --no-interaction</comment>');
  65. $output->writeln('<info>@job\iterations : ' . $job['iterations'] . '</info>');
  66. $output->writeln('<info>@job\defaultMethod : ' . $job['defaultMethod'] . '</info>');
  67. /**
  68. * Printed every server is defined for current job
  69. */
  70. $output->writeln('');
  71. $output->writeln('<info>@job\servers :</info>');
  72. $output->writeln('');
  73. foreach ($job['servers'] as $name => $server) {
  74. $output->writeln('<comment> ' . $name . ' - ' . $server['host'] . ':' . $server['port'] . '</comment>');
  75. }
  76. /**
  77. * Description
  78. */
  79. $output->writeln('');
  80. $output->writeln('<info>@job\description :</info>');
  81. $output->writeln('');
  82. $output->writeln('<comment> #' . $job['description'] . '</comment>');
  83. $output->writeln('');
  84. }
  85. /**
  86. * Describe Worker.
  87. *
  88. * Given a output object and a Worker, dscribe it.
  89. *
  90. * @param OutputInterface $output Output object
  91. * @param array $worker Worker array with Job to describe
  92. * @param Boolean $tinyJobDescription If true also print job list
  93. */
  94. public function describeWorker(OutputInterface $output, array $worker, $tinyJobDescription = false)
  95. {
  96. /**
  97. * Commandline
  98. */
  99. $script = $this->kernel->getRootDir() . '/console gearman:worker:execute';
  100. $output->writeln('');
  101. $output->writeln('<info>@Worker\className : ' . $worker['className'] . '</info>');
  102. $output->writeln('<info>@Worker\fileName : ' . $worker['fileName'] . '</info>');
  103. $output->writeln('<info>@Worker\nameSpace : ' . $worker['namespace'] . '</info>');
  104. $output->writeln('<info>@Worker\callableName: ' . $worker['callableName'] . '</info>');
  105. /**
  106. * Also a complete and clean execution path is given , for supervisord
  107. */
  108. $output->writeln('<info>@Worker\supervisord : </info><comment>/usr/bin/php ' . $script.' ' . $worker['callableName'] . ' --no-interaction</comment>');
  109. /**
  110. * Service value is only explained if defined. Not mandatory
  111. */
  112. if (null !== $worker['service']) {
  113. $output->writeln('<info>@Worker\service : ' . $worker['service'] . '</info>');
  114. }
  115. $output->writeln('<info>@worker\iterations : ' . $worker['iterations'] . '</info>');
  116. $output->writeln('<info>@Worker\#jobs : ' . count($worker['jobs']) . '</info>');
  117. if ($tinyJobDescription) {
  118. $output->writeln('<info>@Worker\jobs</info>');
  119. $output->writeln('');
  120. foreach ($worker['jobs'] as $job) {
  121. if ($job['jobPrefix']) {
  122. $output->writeln('<comment> # ' . $job['realCallableNameNoPrefix'] . ' with jobPrefix: ' . $job['jobPrefix'] . '</comment>');
  123. } else {
  124. $output->writeln('<comment> # ' . $job['realCallableNameNoPrefix'] . ' </comment>');
  125. }
  126. }
  127. }
  128. /**
  129. * Printed every server is defined for current job
  130. */
  131. $output->writeln('');
  132. $output->writeln('<info>@worker\servers :</info>');
  133. $output->writeln('');
  134. foreach ($worker['servers'] as $name => $server) {
  135. $output->writeln('<comment> #' . $name . ' - ' . $server['host'] . ':' . $server['port'] . '</comment>');
  136. }
  137. /**
  138. * Description
  139. */
  140. $output->writeln('');
  141. $output->writeln('<info>@Worker\description :</info>');
  142. $output->writeln('');
  143. $output->writeln('<comment> ' . $worker['description'] . '</comment>');
  144. $output->writeln('');
  145. }
  146. }