GearmanClient.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. <?php
  2. namespace Mmoreramerino\GearmanBundle\Service;
  3. use Mmoreramerino\GearmanBundle\Service\GearmanService;
  4. use Mmoreramerino\GearmanBundle\Service\GearmanInterface;
  5. use Mmoreramerino\GearmanBundle\Exceptions\NoCallableGearmanMethodException;
  6. /**
  7. * Implementation of GearmanInterface
  8. *
  9. * @author Marc Morera <marc@ulabox.com>
  10. */
  11. class GearmanClient extends GearmanService implements GearmanInterface
  12. {
  13. /**
  14. * Server variable to define in what server must connect to
  15. *
  16. * @var array
  17. */
  18. public $server = null;
  19. /**
  20. * If workers are not loaded, they're loaded and returned.
  21. * Otherwise, they are simply returned
  22. *
  23. * @return Array Workers array getted from cache and saved
  24. */
  25. public function getWorkers()
  26. {
  27. /**
  28. * Always will be an Array
  29. */
  30. return $this->setWorkers();
  31. }
  32. /**
  33. * Runs a single task and returns some result, depending of method called.
  34. * Method called depends of default callable method setted on gearman settings
  35. * or overwritted on work or job annotations
  36. *
  37. * @param string $name A GearmanBundle registered function the worker is to execute
  38. * @param Mixed $params Parameters to send to job
  39. *
  40. * @return mixed result depending of method called.
  41. */
  42. public function callJob($name, $params = array())
  43. {
  44. $worker = $this->getWorker($name);
  45. $methodCallable = $worker['job']['defaultMethod'] . 'Job';
  46. if (!method_exists($this, $methodCallable)) {
  47. throw new NoCallableGearmanMethodException($methodCallable);
  48. }
  49. return $this->$methodCallable($name, $params);
  50. }
  51. /**
  52. * Runs a single task and returns a string representation of the result.
  53. * It is up to the GearmanClient and GearmanWorker to agree on the format of the result.
  54. *
  55. * @param string $name A GearmanBundle registered function the worker is to execute
  56. * @param Mixed $params Parameters to send to job
  57. *
  58. * @return string A string representing the results of running a task.
  59. */
  60. public function doJob($name, $params = array())
  61. {
  62. return $this->enqueue($name, $params, 'do');
  63. }
  64. /**
  65. * Runs a task in the background, returning a job handle which
  66. * can be used to get the status of the running task.
  67. *
  68. * @param string $name A GearmanBundle registered function the worker is to execute
  69. * @param Mixed $params Parameters to send to job
  70. *
  71. * @return string Job handle for the submitted task.
  72. */
  73. public function doBackgroundJob($name, $params = array())
  74. {
  75. return $this->enqueue($name, $params, 'doBackground');
  76. }
  77. /**
  78. * Runs a single high priority task and returns a string representation of the result.
  79. * It is up to the GearmanClient and GearmanWorker to agree on the format of the result.
  80. * High priority tasks will get precedence over normal and low priority tasks in the job queue.
  81. *
  82. * @param string $name A GearmanBundle registered function the worker is to execute
  83. * @param Mixed $params Parameters to send to job
  84. *
  85. * @return string A string representing the results of running a task.
  86. */
  87. public function doHighJob($name, $params = array())
  88. {
  89. return $this->enqueue($name, $params, 'doHigh');
  90. }
  91. /**
  92. * Runs a high priority task in the background, returning a job handle which can be used to get the status of the running task.
  93. * High priority tasks take precedence over normal and low priority tasks in the job queue.
  94. *
  95. * @param string $name A GearmanBundle registered function the worker is to execute
  96. * @param Mixed $params Parameters to send to job
  97. *
  98. * @return string The job handle for the submitted task.
  99. */
  100. public function doHighBackgroundJob($name, $params = array())
  101. {
  102. return $this->enqueue($name, $params, 'doHighBackground');
  103. }
  104. /**
  105. * Runs a single low priority task and returns a string representation of the result.
  106. * It is up to the GearmanClient and GearmanWorker to agree on the format of the result.
  107. * Normal and high priority tasks will get precedence over low priority tasks in the job queue.
  108. *
  109. * @param string $name A GearmanBundle registered function the worker is to execute
  110. * @param Mixed $params Parameters to send to job
  111. *
  112. * @return string A string representing the results of running a task.
  113. */
  114. public function doLowJob($name, $params = array())
  115. {
  116. return $this->enqueue($name, $params, 'doLow');
  117. }
  118. /**
  119. * Runs a low priority task in the background, returning a job handle which can be used to get the status of the running task.
  120. * Normal and high priority tasks will get precedence over low priority tasks in the job queue.
  121. *
  122. * @param string $name A GearmanBundle registered function the worker is to execute
  123. * @param Mixed $params Parameters to send to job
  124. *
  125. * @return string The job handle for the submitted task.
  126. */
  127. public function doLowBackgroundJob($name, $params = array())
  128. {
  129. return $this->enqueue($name, $params, 'doLowBackground');
  130. }
  131. /**
  132. * Get real worker from job name and enqueues the action given one
  133. * method.
  134. *
  135. * @param string $jobName A GearmanBundle registered function the worker is to execute
  136. * @param mixed $params Parameters to send to job
  137. * @param string $method Method to execute
  138. *
  139. * @return mixed Return result of the call
  140. */
  141. private function enqueue($jobName, $params, $method)
  142. {
  143. $worker = $this->getWorker($jobName);
  144. if (false !== $worker) {
  145. return $this->doEnqueue($worker, $params, $method);
  146. }
  147. return false;
  148. }
  149. /**
  150. * Execute a GearmanClient call given a worker, params and a method.
  151. * If any method is given, it performs a "do" call
  152. *
  153. * If he GarmanClient call is asyncronous, result value will be a handler.
  154. * Otherwise, will return job result.
  155. *
  156. * @param array $worker Worker definition
  157. * @param mixed $params Parameters to send to job
  158. * @param string $method Method to execute
  159. *
  160. * @return mixed Return result of the GearmanClient call
  161. */
  162. private function doEnqueue(Array $worker, $params='', $method='do')
  163. {
  164. $gmclient= new \GearmanClient();
  165. if (null === $this->server || !is_array($this->server)) {
  166. $gmclient->addServer();
  167. } else {
  168. $gmclient->addServer($this->server[0], $this->server[1]);
  169. }
  170. return $gmclient->$method($worker['job']['realCallableName'], serialize($params));
  171. }
  172. /**
  173. * Set server of gearman
  174. *
  175. * @param type $servername Server name (must be ip)
  176. * @param type $port Port of server. By default 4730
  177. *
  178. * @return Gearman Returns self object
  179. */
  180. public function setServer($servername, $port = 4730)
  181. {
  182. $this->server = array($servername, $port);
  183. return $this;
  184. }
  185. }