GearmanDescriber.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. $script = $this->kernel->getRootDir() . '/console gearman:job:execute';
  44. $this->describeWorker($output, $worker);
  45. $job = $worker['job'];
  46. $output->writeln('<info> @job\methodName : ' . $job['methodName'] . '</info>');
  47. $output->writeln('<info> @job\callableName : ' . $job['realCallableName'] . '</info>');
  48. $output->writeln('<info> @job\supervisord : </info><comment>/usr/bin/php ' . $script.' ' . $job['realCallableName'] . ' --no-interaction</comment>');
  49. $output->writeln('<info> @job\iterations : ' . $job['iterations'] . '</info>');
  50. $output->writeln('<info> @job\defaultMethod : ' . $job['defaultMethod'] . '</info>');
  51. $output->writeln('<info> @job\servers :</info>');
  52. $output->writeln('');
  53. foreach ($job['servers'] as $name => $server) {
  54. $output->writeln('<comment> ' . $name . ' - ' . $server['host'] . ':' . $server['port'] . '</comment>');
  55. }
  56. $output->writeln('');
  57. $output->writeln('<info> @job\description :</info>');
  58. $output->writeln('');
  59. $output->writeln('<comment> #' . $job['description'] . '</comment>');
  60. $output->writeln('');
  61. }
  62. /**
  63. * Describe Worker.
  64. *
  65. * Given a output object and a Worker, dscribe it.
  66. *
  67. * @param OutputInterface $output Output object
  68. * @param array $worker Worker array with Job to describe
  69. * @param Boolean $tinyJobDescription If true also print job list
  70. */
  71. public function describeWorker(OutputInterface $output, array $worker, $tinyJobDescription = false)
  72. {
  73. $script = $this->kernel->getRootDir() . '/console gearman:worker:execute';
  74. $output->writeln('');
  75. $output->writeln('<info> @Worker\className : ' . $worker['className'] . '</info>');
  76. $output->writeln('<info> @Worker\fileName : ' . $worker['fileName'] . '</info>');
  77. $output->writeln('<info> @Worker\nameSpace : ' . $worker['namespace'] . '</info>');
  78. $output->writeln('<info> @Worker\callableName: ' . $worker['callableName'] . '</info>');
  79. $output->writeln('<info> @Worker\supervisord : </info><comment>/usr/bin/php ' . $script.' ' . $worker['callableName'] . ' --no-interaction</comment>');
  80. if (null !== $worker['service']) {
  81. $output->writeln('<info> @Worker\service : ' . $worker['service'] . '</info>');
  82. }
  83. $output->writeln('<info> @worker\iterations : ' . $worker['iterations'] . '</info>');
  84. $output->writeln('<info> @Worker\#jobs : '.count($worker['jobs']).'</info>');
  85. if ($tinyJobDescription) {
  86. $output->writeln('<info> @Worker\jobs</info>');
  87. $output->writeln('');
  88. foreach ($worker['jobs'] as $job) {
  89. $output->writeln('<comment> # ' . $job['realCallableName'] . '</comment>');
  90. }
  91. }
  92. $output->writeln('');
  93. $output->writeln('<info> @worker\servers :</info>');
  94. $output->writeln('');
  95. foreach ($worker['servers'] as $name => $server) {
  96. $output->writeln('<comment> #' . $name . ' - ' . $server['host'] . ':' . $server['port'] . '</comment>');
  97. }
  98. $output->writeln('');
  99. $output->writeln('<info> @Worker\description :</info>');
  100. $output->writeln('');
  101. $output->writeln('<comment> ' . $worker['description'] . '</comment>');
  102. $output->writeln('');
  103. }
  104. }