GearmanClient.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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
  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->getJob($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. * The GearmanClient::do() method is deprecated as of pecl/gearman 1.0.0. Use GearmanClient::doNormal().
  55. *
  56. * @param string $name A GearmanBundle registered function the worker is to execute
  57. * @param Mixed $params Parameters to send to job
  58. *
  59. * @return string A string representing the results of running a task.
  60. * @depracated
  61. */
  62. public function doJob($name, $params = array())
  63. {
  64. return $this->enqueue($name, $params, 'do');
  65. }
  66. /**
  67. * Runs a single task and returns a string representation of the result.
  68. * It is up to the GearmanClient and GearmanWorker to agree on the format of the result.
  69. *
  70. * @param string $name A GearmanBundle registered function the worker is to execute
  71. * @param Mixed $params Parameters to send to job
  72. *
  73. * @return string A string representing the results of running a task.
  74. */
  75. public function doNormalJob($name, $params = array())
  76. {
  77. return $this->enqueue($name, $params, 'doNormal');
  78. }
  79. /**
  80. * Runs a task in the background, returning a job handle which
  81. * can be used to get the status of the running task.
  82. *
  83. * @param string $name A GearmanBundle registered function the worker is to execute
  84. * @param Mixed $params Parameters to send to job
  85. *
  86. * @return string Job handle for the submitted task.
  87. */
  88. public function doBackgroundJob($name, $params = array())
  89. {
  90. return $this->enqueue($name, $params, 'doBackground');
  91. }
  92. /**
  93. * Runs a single high priority task and returns a string representation of the result.
  94. * It is up to the GearmanClient and GearmanWorker to agree on the format of the result.
  95. * High priority tasks will get precedence over normal and low priority tasks in the job queue.
  96. *
  97. * @param string $name A GearmanBundle registered function the worker is to execute
  98. * @param Mixed $params Parameters to send to job
  99. *
  100. * @return string A string representing the results of running a task.
  101. */
  102. public function doHighJob($name, $params = array())
  103. {
  104. return $this->enqueue($name, $params, 'doHigh');
  105. }
  106. /**
  107. * Runs a high priority task in the background, returning a job handle which can be used to get the status of the running task.
  108. * High priority tasks take precedence over normal and low priority tasks in the job queue.
  109. *
  110. * @param string $name A GearmanBundle registered function the worker is to execute
  111. * @param Mixed $params Parameters to send to job
  112. *
  113. * @return string The job handle for the submitted task.
  114. */
  115. public function doHighBackgroundJob($name, $params = array())
  116. {
  117. return $this->enqueue($name, $params, 'doHighBackground');
  118. }
  119. /**
  120. * Runs a single low priority task and returns a string representation of the result.
  121. * It is up to the GearmanClient and GearmanWorker to agree on the format of the result.
  122. * Normal and high priority tasks will get precedence over low priority tasks in the job queue.
  123. *
  124. * @param string $name A GearmanBundle registered function the worker is to execute
  125. * @param Mixed $params Parameters to send to job
  126. *
  127. * @return string A string representing the results of running a task.
  128. */
  129. public function doLowJob($name, $params = array())
  130. {
  131. return $this->enqueue($name, $params, 'doLow');
  132. }
  133. /**
  134. * Runs a low priority task in the background, returning a job handle which can be used to get the status of the running task.
  135. * Normal and high priority tasks will get precedence over low priority tasks in the job queue.
  136. *
  137. * @param string $name A GearmanBundle registered function the worker is to execute
  138. * @param Mixed $params Parameters to send to job
  139. *
  140. * @return string The job handle for the submitted task.
  141. */
  142. public function doLowBackgroundJob($name, $params = array())
  143. {
  144. return $this->enqueue($name, $params, 'doLowBackground');
  145. }
  146. /**
  147. * Get real worker from job name and enqueues the action given one
  148. * method.
  149. *
  150. * @param string $jobName A GearmanBundle registered function the worker is to execute
  151. * @param mixed $params Parameters to send to job
  152. * @param string $method Method to execute
  153. *
  154. * @return mixed Return result of the call
  155. */
  156. private function enqueue($jobName, $params, $method)
  157. {
  158. $worker = $this->getJob($jobName);
  159. if (false !== $worker) {
  160. return $this->doEnqueue($worker, $params, $method);
  161. }
  162. return false;
  163. }
  164. /**
  165. * Execute a GearmanClient call given a worker, params and a method.
  166. * If any method is given, it performs a "do" call
  167. *
  168. * If he GarmanClient call is asyncronous, result value will be a handler.
  169. * Otherwise, will return job result.
  170. *
  171. * @param array $worker Worker definition
  172. * @param mixed $params Parameters to send to job
  173. * @param string $method Method to execute
  174. *
  175. * @return mixed Return result of the GearmanClient call
  176. */
  177. private function doEnqueue(Array $worker, $params='', $method='do')
  178. {
  179. $gmclient= new \GearmanClient();
  180. if (null === $this->server || !is_array($this->server)) {
  181. $gmclient->addServer();
  182. } else {
  183. $gmclient->addServer($this->server[0], $this->server[1]);
  184. }
  185. return $gmclient->$method($worker['job']['realCallableName'], serialize($params));
  186. }
  187. /**
  188. * Set server of gearman
  189. *
  190. * @param type $servername Server name (must be ip)
  191. * @param type $port Port of server. By default 4730
  192. *
  193. * @return Gearman Returns self object
  194. */
  195. public function setServer($servername, $port = 4730)
  196. {
  197. $this->server = array($servername, $port);
  198. return $this;
  199. }
  200. }