GearmanWorkerListCommand.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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\Command;
  13. use Symfony\Component\Console\Input\InputInterface;
  14. use Symfony\Component\Console\Output\OutputInterface;
  15. use Mmoreram\GearmanBundle\Command\Abstracts\AbstractGearmanCommand;
  16. use Mmoreram\GearmanBundle\Service\GearmanClient;
  17. /**
  18. * Gearman Job List Command class
  19. *
  20. * @since 2.3.1
  21. */
  22. class GearmanWorkerListCommand extends AbstractGearmanCommand
  23. {
  24. /**
  25. * @var GearmanClient
  26. *
  27. * Gearman client
  28. */
  29. protected $gearmanClient;
  30. /**
  31. * Set gearman client
  32. *
  33. * @param GearmanClient $gearmanClient Gearman client
  34. *
  35. * @return GearmanJobDescribeCommand self Object
  36. */
  37. public function setGearmanClient(GearmanClient $gearmanClient)
  38. {
  39. $this->gearmanClient = $gearmanClient;
  40. return $this;
  41. }
  42. /**
  43. * Console Command configuration
  44. */
  45. protected function configure()
  46. {
  47. parent::configure();
  48. $this
  49. ->setName('gearman:worker:list')
  50. ->setDescription('List all Gearman Workers and their Jobs');
  51. }
  52. /**
  53. * Executes the current command.
  54. *
  55. * @param InputInterface $input An InputInterface instance
  56. * @param OutputInterface $output An OutputInterface instance
  57. *
  58. * @return integer 0 if everything went fine, or an error code
  59. *
  60. * @throws \LogicException When this abstract class is not implemented
  61. */
  62. protected function execute(InputInterface $input, OutputInterface $output)
  63. {
  64. if ($input->getOption('quiet')) {
  65. return;
  66. }
  67. $workers = $this->gearmanClient->getWorkers();
  68. if (is_array($workers)) {
  69. $it = 1;
  70. foreach ($workers as $worker) {
  71. $output->writeln('<comment>@Worker: </comment><info>' . $worker['className'] . '</info>');
  72. $output->writeln('<comment>callablename: </comment><info>' . $worker['callableName'] . '</info>');
  73. $output->writeln('<comment>Jobs:</comment>');
  74. foreach ($worker['jobs'] as $job) {
  75. $output->writeln('<comment> - #' . $it++ . '</comment>');
  76. $output->writeln('<comment> name: ' . $job['methodName'] . '</comment>');
  77. $output->writeln('<comment> callablename:</comment><info> ' . $job['realCallableNameNoPrefix'] . '</info>');
  78. if (false === is_null($job['jobPrefix'])) {
  79. $output->writeln('<comment> jobPrefix:</comment><info> ' . $job['jobPrefix'] . '</info>');
  80. }
  81. }
  82. }
  83. }
  84. }
  85. }