GearmanDescriber.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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\Kernel;
  11. /**
  12. * Implementation of GearmanDescriber
  13. *
  14. * @author Marc Morera <yuhu@mmoreram.com>
  15. */
  16. class GearmanDescriber
  17. {
  18. /**
  19. * @var Kernel
  20. *
  21. * Kernel
  22. */
  23. private $kernel;
  24. /**
  25. * Construct method
  26. *
  27. * @param Kernel $kernel Kernel
  28. */
  29. public function __construct(Kernel $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. /**
  55. * Also a complete and clean execution path is given , for supervisord
  56. */
  57. $output->writeln('<info> @job\supervisord : </info><comment>/usr/bin/php ' . $script.' ' . $job['realCallableName'] . ' --no-interaction</comment>');
  58. $output->writeln('<info> @job\iterations : ' . $job['iterations'] . '</info>');
  59. $output->writeln('<info> @job\defaultMethod : ' . $job['defaultMethod'] . '</info>');
  60. /**
  61. * Printed every server is defined for current job
  62. */
  63. $output->writeln('');
  64. $output->writeln('<info> @job\servers :</info>');
  65. $output->writeln('');
  66. foreach ($job['servers'] as $name => $server) {
  67. $output->writeln('<comment> ' . $name . ' - ' . $server['host'] . ':' . $server['port'] . '</comment>');
  68. }
  69. /**
  70. * Description
  71. */
  72. $output->writeln('');
  73. $output->writeln('<info> @job\description :</info>');
  74. $output->writeln('');
  75. $output->writeln('<comment> #' . $job['description'] . '</comment>');
  76. $output->writeln('');
  77. }
  78. /**
  79. * Describe Worker.
  80. *
  81. * Given a output object and a Worker, dscribe it.
  82. *
  83. * @param OutputInterface $output Output object
  84. * @param array $worker Worker array with Job to describe
  85. * @param Boolean $tinyJobDescription If true also print job list
  86. */
  87. public function describeWorker(OutputInterface $output, array $worker, $tinyJobDescription = false)
  88. {
  89. /**
  90. * Commandline
  91. */
  92. $script = $this->kernel->getRootDir() . '/console gearman:worker:execute';
  93. $output->writeln('');
  94. $output->writeln('<info> @Worker\className : ' . $worker['className'] . '</info>');
  95. $output->writeln('<info> @Worker\fileName : ' . $worker['fileName'] . '</info>');
  96. $output->writeln('<info> @Worker\nameSpace : ' . $worker['namespace'] . '</info>');
  97. $output->writeln('<info> @Worker\callableName: ' . $worker['callableName'] . '</info>');
  98. /**
  99. * Also a complete and clean execution path is given , for supervisord
  100. */
  101. $output->writeln('<info> @Worker\supervisord : </info><comment>/usr/bin/php ' . $script.' ' . $worker['callableName'] . ' --no-interaction</comment>');
  102. /**
  103. * Service value is only explained if defined. Not mandatory
  104. */
  105. if (null !== $worker['service']) {
  106. $output->writeln('<info> @Worker\service : ' . $worker['service'] . '</info>');
  107. }
  108. $output->writeln('<info> @worker\iterations : ' . $worker['iterations'] . '</info>');
  109. $output->writeln('<info> @Worker\#jobs : '.count($worker['jobs']).'</info>');
  110. if ($tinyJobDescription) {
  111. $output->writeln('<info> @Worker\jobs</info>');
  112. $output->writeln('');
  113. foreach ($worker['jobs'] as $job) {
  114. $output->writeln('<comment> # ' . $job['realCallableName'] . '</comment>');
  115. }
  116. }
  117. /**
  118. * Printed every server is defined for current job
  119. */
  120. $output->writeln('');
  121. $output->writeln('<info> @worker\servers :</info>');
  122. $output->writeln('');
  123. foreach ($worker['servers'] as $name => $server) {
  124. $output->writeln('<comment> #' . $name . ' - ' . $server['host'] . ':' . $server['port'] . '</comment>');
  125. }
  126. /**
  127. * Description
  128. */
  129. $output->writeln('');
  130. $output->writeln('<info> @Worker\description :</info>');
  131. $output->writeln('');
  132. $output->writeln('<comment> ' . $worker['description'] . '</comment>');
  133. $output->writeln('');
  134. }
  135. }