GearmanExecute.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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\NullOutput;
  14. use Symfony\Component\Console\Output\OutputInterface;
  15. use Symfony\Component\DependencyInjection\ContainerInterface;
  16. use Symfony\Component\DependencyInjection\ContainerAwareInterface;
  17. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  18. use Mmoreram\GearmanBundle\Command\Util\GearmanOutputAwareInterface;
  19. use Mmoreram\GearmanBundle\Service\Abstracts\AbstractGearmanService;
  20. use Mmoreram\GearmanBundle\Event\GearmanWorkExecutedEvent;
  21. use Mmoreram\GearmanBundle\GearmanEvents;
  22. /**
  23. * Gearman execute methods. All Worker methods
  24. *
  25. * @since 2.3.1
  26. */
  27. class GearmanExecute extends AbstractGearmanService
  28. {
  29. /**
  30. * @var ContainerInterface
  31. *
  32. * Container instance
  33. */
  34. private $container;
  35. /**
  36. * @var EventDispatcherInterface
  37. *
  38. * EventDispatcher instance
  39. */
  40. protected $eventDispatcher;
  41. /**
  42. * @var OutputInterface
  43. *
  44. * Output instance
  45. */
  46. protected $output;
  47. /**
  48. * Set container
  49. *
  50. * @param ContainerInterface $container Container
  51. *
  52. * @return GearmanExecute self Object
  53. */
  54. public function setContainer(ContainerInterface $container)
  55. {
  56. $this->container = $container;
  57. return $this;
  58. }
  59. /**
  60. * Set event dispatcher
  61. *
  62. * @param EventDispatcherInterface $eventDispatcher
  63. *
  64. * @return GearmanExecute self Object
  65. */
  66. public function setEventDispatcher(EventDispatcherInterface $eventDispatcher)
  67. {
  68. $this->eventDispatcher = $eventDispatcher;
  69. return $this;
  70. }
  71. /**
  72. * Set output
  73. *
  74. * @param OutputInterface $output
  75. *
  76. * @return GearmanExecute self Object
  77. */
  78. public function setOutput(OutputInterface $output)
  79. {
  80. $this->output = $output;
  81. return $this;
  82. }
  83. /**
  84. * Executes a job given a jobName and given settings and annotations of job
  85. *
  86. * @param string $jobName Name of job to be executed
  87. */
  88. public function executeJob($jobName)
  89. {
  90. $worker = $this->getJob($jobName);
  91. if (false !== $worker) {
  92. $this->callJob($worker);
  93. }
  94. }
  95. /**
  96. * Given a worker, execute GearmanWorker function defined by job.
  97. *
  98. * @param array $worker Worker definition
  99. *
  100. * @return GearmanExecute self Object
  101. */
  102. private function callJob(Array $worker)
  103. {
  104. $gearmanWorker = new \GearmanWorker;
  105. if (isset($worker['job'])) {
  106. $jobs = array($worker['job']);
  107. $iterations = $worker['job']['iterations'];
  108. $this->addServers($gearmanWorker, $worker['job']['servers']);
  109. } else {
  110. $jobs = $worker['jobs'];
  111. $iterations = $worker['iterations'];
  112. $this->addServers($gearmanWorker, $worker['servers']);
  113. }
  114. $objInstance = $this->createJob($worker);
  115. $this->runJob($gearmanWorker, $objInstance, $jobs, $iterations);
  116. return $this;
  117. }
  118. /**
  119. * Given a worker settings, return Job instance
  120. *
  121. * @param array $worker Worker settings
  122. *
  123. * @return Object Job instance
  124. */
  125. private function createJob(array $worker)
  126. {
  127. /**
  128. * If service is defined, we must retrieve this class with dependency injection
  129. *
  130. * Otherwise we just create it with a simple new()
  131. */
  132. if ($worker['service']) {
  133. $objInstance = $this->container->get($worker['service']);
  134. } else {
  135. $objInstance = new $worker['className'];
  136. /**
  137. * If instance of given object is instanceof
  138. * ContainerAwareInterface, we inject full container by calling
  139. * container setter.
  140. *
  141. * @see https://github.com/mmoreram/gearman-bundle/pull/12
  142. */
  143. if ($objInstance instanceof ContainerAwareInterface) {
  144. $objInstance->setContainer($this->container);
  145. }
  146. }
  147. return $objInstance;
  148. }
  149. /**
  150. * Given a GearmanWorker and an instance of Job, run it
  151. *
  152. * @param \GearmanWorker $gearmanWorker Gearman Worker
  153. * @param Object $objInstance Job instance
  154. * @param array $jobs Array of jobs to subscribe
  155. * @param integer $iterations Number of iterations
  156. *
  157. * @return GearmanExecute self Object
  158. */
  159. private function runJob(\GearmanWorker $gearmanWorker, $objInstance, array $jobs, $iterations)
  160. {
  161. /**
  162. * Set the output of this instance, this should allow workers to use the console output.
  163. */
  164. if ($objInstance instanceof GearmanOutputAwareInterface) {
  165. $objInstance->setOutput($this->output ? : new NullOutput());
  166. }
  167. /**
  168. * Every job defined in worker is added into GearmanWorker
  169. */
  170. foreach ($jobs as $job) {
  171. $gearmanWorker->addFunction($job['realCallableName'], array($objInstance, $job['methodName']));
  172. }
  173. /**
  174. * If iterations value is 0, is like worker will never die
  175. */
  176. $alive = (0 == $iterations);
  177. /**
  178. * Executes GearmanWorker with all jobs defined
  179. */
  180. while ($gearmanWorker->work()) {
  181. $iterations--;
  182. $event = new GearmanWorkExecutedEvent($jobs, $iterations, $gearmanWorker->returnCode());
  183. $this->eventDispatcher->dispatch(GearmanEvents::GEARMAN_WORK_EXECUTED, $event);
  184. if ($gearmanWorker->returnCode() != GEARMAN_SUCCESS) {
  185. break;
  186. }
  187. /**
  188. * Only finishes its execution if alive is false and iterations
  189. * arrives to 0
  190. */
  191. if (!$alive && $iterations <= 0) {
  192. break;
  193. }
  194. }
  195. }
  196. /**
  197. * Adds into worker all defined Servers.
  198. * If any is defined, performs default method
  199. *
  200. * @param \GearmanWorker $gmworker Worker to perform configuration
  201. * @param array $servers Servers array
  202. */
  203. private function addServers(\GearmanWorker $gmworker, Array $servers)
  204. {
  205. if (!empty($servers)) {
  206. foreach ($servers as $server) {
  207. $gmworker->addServer($server['host'], $server['port']);
  208. }
  209. } else {
  210. $gmworker->addServer();
  211. }
  212. }
  213. /**
  214. * Executes a worker given a workerName subscribing all his jobs inside and
  215. * given settings and annotations of worker and jobs
  216. *
  217. * @param string $workerName Name of worker to be executed
  218. */
  219. public function executeWorker($workerName)
  220. {
  221. $worker = $this->getWorker($workerName);
  222. if (false !== $worker) {
  223. $this->callJob($worker);
  224. }
  225. }
  226. }