GearmanDescriber.php 5.5 KB

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