GearmanExecute.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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\ContainerAwareInterface;
  16. use Symfony\Component\DependencyInjection\ContainerInterface;
  17. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  18. use Mmoreram\GearmanBundle\Command\Util\GearmanOutputAwareInterface;
  19. use Mmoreram\GearmanBundle\Event\GearmanWorkExecutedEvent;
  20. use Mmoreram\GearmanBundle\GearmanEvents;
  21. use Mmoreram\GearmanBundle\Service\Abstracts\AbstractGearmanService;
  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(
  172. $job['realCallableName'],
  173. array($this, 'handleJob'),
  174. array(
  175. 'job_object_instance' => $objInstance,
  176. 'job_method' => $job['methodName'],
  177. )
  178. );
  179. }
  180. /**
  181. * If iterations value is 0, is like worker will never die
  182. */
  183. $alive = (0 == $iterations);
  184. /**
  185. * Executes GearmanWorker with all jobs defined
  186. */
  187. while ($gearmanWorker->work()) {
  188. $iterations--;
  189. $event = new GearmanWorkExecutedEvent($jobs, $iterations, $gearmanWorker->returnCode());
  190. $this->eventDispatcher->dispatch(GearmanEvents::GEARMAN_WORK_EXECUTED, $event);
  191. if ($gearmanWorker->returnCode() != GEARMAN_SUCCESS) {
  192. break;
  193. }
  194. /**
  195. * Only finishes its execution if alive is false and iterations
  196. * arrives to 0
  197. */
  198. if (!$alive && $iterations <= 0) {
  199. break;
  200. }
  201. }
  202. }
  203. /**
  204. * Adds into worker all defined Servers.
  205. * If any is defined, performs default method
  206. *
  207. * @param \GearmanWorker $gmworker Worker to perform configuration
  208. * @param array $servers Servers array
  209. */
  210. private function addServers(\GearmanWorker $gmworker, Array $servers)
  211. {
  212. if (!empty($servers)) {
  213. foreach ($servers as $server) {
  214. $gmworker->addServer($server['host'], $server['port']);
  215. }
  216. } else {
  217. $gmworker->addServer();
  218. }
  219. }
  220. /**
  221. * Executes a worker given a workerName subscribing all his jobs inside and
  222. * given settings and annotations of worker and jobs
  223. *
  224. * @param string $workerName Name of worker to be executed
  225. */
  226. public function executeWorker($workerName)
  227. {
  228. $worker = $this->getWorker($workerName);
  229. if (false !== $worker) {
  230. $this->callJob($worker);
  231. }
  232. }
  233. /**
  234. * Wrapper function handler for all registered functions
  235. * This allows us to do some nice logging when jobs are started/finished
  236. *
  237. * @see https://github.com/brianlmoon/GearmanManager/blob/ffc828dac2547aff76cb4962bb3fcc4f454ec8a2/GearmanPeclManager.php#L95-206
  238. *
  239. * @param \GearmanJob $job
  240. * @param mixed $context
  241. *
  242. * @return mixed
  243. */
  244. public function handleJob(\GearmanJob $job, $context)
  245. {
  246. if (
  247. !is_array($context)
  248. || !array_key_exists('job_object_instance', $context)
  249. || !array_key_exists('job_method', $context)
  250. ) {
  251. }
  252. $result = call_user_func_array(
  253. array($context['job_object_instance'], $context['job_method']),
  254. array($job, $context)
  255. );
  256. /**
  257. * Workaround for PECL bug #17114
  258. * http://pecl.php.net/bugs/bug.php?id=17114
  259. */
  260. $type = gettype($result);
  261. settype($result, $type);
  262. return $result;
  263. }
  264. }