/library/Zend/Queue/Adapter/PlatformJobQueue.php

https://bitbucket.org/Ebozavrik/test-application · PHP · 354 lines · 164 code · 45 blank · 145 comment · 20 complexity · 8b468d8b3dccae147ce80088e9399cd7 MD5 · raw file

  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Queue
  17. * @subpackage Adapter
  18. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id: PlatformJobQueue.php 24593 2012-01-05 20:35:02Z matthew $
  21. */
  22. /**
  23. * @see Zend_Queue_Adapter_AdapterAbstract
  24. */
  25. require_once 'Zend/Queue/Adapter/AdapterAbstract.php';
  26. /**
  27. * Zend Platform JobQueue adapter
  28. *
  29. * @category Zend
  30. * @package Zend_Queue
  31. * @subpackage Adapter
  32. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  33. * @license http://framework.zend.com/license/new-bsd New BSD License
  34. */
  35. class Zend_Queue_Adapter_PlatformJobQueue extends Zend_Queue_Adapter_AdapterAbstract
  36. {
  37. /**
  38. * @var ZendApi_JobQueue
  39. */
  40. protected $_zendQueue;
  41. /**
  42. * Constructor
  43. *
  44. * @param array|Zend_Config $options
  45. * @param Zend_Queue|null $queue
  46. *
  47. * @return void
  48. */
  49. public function __construct ($options, Zend_Queue $queue = null)
  50. {
  51. parent::__construct($options, $queue);
  52. if (!extension_loaded("jobqueue_client")) {
  53. require_once 'Zend/Queue/Exception.php';
  54. throw new Zend_Queue_Exception( 'Platform Job Queue extension does not appear to be loaded' );
  55. }
  56. if (!isset( $this->_options['daemonOptions'] )) {
  57. require_once 'Zend/Queue/Exception.php';
  58. throw new Zend_Queue_Exception( 'Job Queue host and password should be provided' );
  59. }
  60. $options = $this->_options['daemonOptions'];
  61. if (!array_key_exists('host', $options)) {
  62. require_once 'Zend/Queue/Exception.php';
  63. throw new Zend_Queue_Exception( 'Platform Job Queue host should be provided' );
  64. }
  65. if (!array_key_exists('password', $options)) {
  66. require_once 'Zend/Queue/Exception.php';
  67. throw new Zend_Queue_Exception( 'Platform Job Queue password should be provided' );
  68. }
  69. $this->_zendQueue = new ZendApi_Queue( $options['host'] );
  70. if (!$this->_zendQueue) {
  71. require_once 'Zend/Queue/Exception.php';
  72. throw new Zend_Queue_Exception( 'Platform Job Queue connection failed' );
  73. }
  74. if (!$this->_zendQueue->login($options['password'])) {
  75. require_once 'Zend/Queue/Exception.php';
  76. throw new Zend_Queue_Exception( 'Job Queue login failed' );
  77. }
  78. if ($this->_queue) {
  79. $this->_queue->setMessageClass('Zend_Queue_Message_PlatformJob');
  80. }
  81. }
  82. /********************************************************************
  83. * Queue management functions
  84. ********************************************************************/
  85. /**
  86. * Does a queue already exist?
  87. *
  88. * @param string $name
  89. *
  90. * @return boolean
  91. * @throws Zend_Queue_Exception (not supported)
  92. */
  93. public function isExists ($name)
  94. {
  95. require_once 'Zend/Queue/Exception.php';
  96. throw new Zend_Queue_Exception( 'isExists() is not supported in this adapter' );
  97. }
  98. /**
  99. * Create a new queue
  100. *
  101. * @param string $name queue name
  102. * @param integer $timeout default visibility timeout
  103. *
  104. * @return void
  105. * @throws Zend_Queue_Exception
  106. */
  107. public function create ($name, $timeout = null)
  108. {
  109. require_once 'Zend/Queue/Exception.php';
  110. throw new Zend_Queue_Exception( 'create() is not supported in ' . get_class($this) );
  111. }
  112. /**
  113. * Delete a queue and all of its messages
  114. *
  115. * @param string $name queue name
  116. *
  117. * @return void
  118. * @throws Zend_Queue_Exception
  119. */
  120. public function delete ($name)
  121. {
  122. require_once 'Zend/Queue/Exception.php';
  123. throw new Zend_Queue_Exception( 'delete() is not supported in ' . get_class($this) );
  124. }
  125. /**
  126. * Get an array of all available queues
  127. *
  128. * @return void
  129. * @throws Zend_Queue_Exception
  130. */
  131. public function getQueues ()
  132. {
  133. require_once 'Zend/Queue/Exception.php';
  134. throw new Zend_Queue_Exception( 'getQueues() is not supported in this adapter' );
  135. }
  136. /**
  137. * Return the approximate number of messages in the queue
  138. *
  139. * @param Zend_Queue|null $queue
  140. *
  141. * @return integer
  142. */
  143. public function count (Zend_Queue $queue = null)
  144. {
  145. if ($queue !== null) {
  146. require_once 'Zend/Queue/Exception.php';
  147. throw new Zend_Queue_Exception( 'Queue parameter is not supported' );
  148. }
  149. return $this->_zendQueue->getNumOfJobsInQueue();
  150. }
  151. /********************************************************************
  152. * Messsage management functions
  153. ********************************************************************/
  154. /**
  155. * Send a message to the queue
  156. *
  157. * @param array | ZendAPI_job $message Message to send to the active queue
  158. * @param Zend_Queue $queue Not supported
  159. *
  160. * @return Zend_Queue_Message
  161. * @throws Zend_Queue_Exception
  162. */
  163. public function send ($message, Zend_Queue $queue = null)
  164. {
  165. if ($queue !== null) {
  166. require_once 'Zend/Queue/Exception.php';
  167. throw new Zend_Queue_Exception( 'Queue parameter is not supported' );
  168. }
  169. // This adapter can work only for this message type
  170. $classname = $this->_queue->getMessageClass();
  171. if (!class_exists($classname)) {
  172. require_once 'Zend/Loader.php';
  173. Zend_Loader::loadClass($classname);
  174. }
  175. if ($message instanceof ZendAPI_Job) {
  176. $message = array( 'data' => $message );
  177. }
  178. $zendApiJob = new $classname( $message );
  179. // Unfortunately, the Platform JQ API is PHP4-style...
  180. $platformJob = $zendApiJob->getJob();
  181. $jobId = $this->_zendQueue->addJob($platformJob);
  182. if (!$jobId) {
  183. require_once 'Zend/Queue/Exception.php';
  184. throw new Zend_Queue_Exception( 'Failed to add a job to queue: '
  185. . $this->_zendQueue->getLastError() );
  186. }
  187. $zendApiJob->setJobId($jobId);
  188. return $zendApiJob;
  189. }
  190. /**
  191. * Get messages in the queue
  192. *
  193. * @param integer $maxMessages Maximum number of messages to return
  194. * @param integer $timeout Ignored
  195. * @param Zend_Queue $queue Not supported
  196. *
  197. * @throws Zend_Queue_Exception
  198. * @return ArrayIterator
  199. */
  200. public function receive ($maxMessages = null, $timeout = null, Zend_Queue $queue = null)
  201. {
  202. if ($maxMessages === null) {
  203. $maxMessages = 1;
  204. }
  205. if ($queue !== null) {
  206. require_once 'Zend/Queue/Exception.php';
  207. throw new Zend_Queue_Exception( 'Queue shouldn\'t be set' );
  208. }
  209. $jobs = $this->_zendQueue->getJobsInQueue(null, $maxMessages, true);
  210. $classname = $this->_queue->getMessageClass();
  211. if (!class_exists($classname)) {
  212. require_once 'Zend/Loader.php';
  213. Zend_Loader::loadClass($classname);
  214. }
  215. $options = array(
  216. 'queue' => $this->_queue,
  217. 'data' => $jobs,
  218. 'messageClass' => $this->_queue->getMessageClass(),
  219. );
  220. $classname = $this->_queue->getMessageSetClass();
  221. if (!class_exists($classname)) {
  222. require_once 'Zend/Loader.php';
  223. Zend_Loader::loadClass($classname);
  224. }
  225. return new $classname( $options );
  226. }
  227. /**
  228. * Delete a message from the queue
  229. *
  230. * Returns true if the message is deleted, false if the deletion is
  231. * unsuccessful.
  232. *
  233. * @param Zend_Queue_Message $message
  234. *
  235. * @return boolean
  236. * @throws Zend_Queue_Exception
  237. */
  238. public function deleteMessage (Zend_Queue_Message $message)
  239. {
  240. if (get_class($message) != $this->_queue->getMessageClass()) {
  241. require_once 'Zend/Queue/Exception.php';
  242. throw new Zend_Queue_Exception(
  243. 'Failed to remove job from the queue; only messages of type '
  244. . 'Zend_Queue_Message_PlatformJob may be used'
  245. );
  246. }
  247. return $this->_zendQueue->removeJob($message->getJobId());
  248. }
  249. public function isJobIdExist ($id)
  250. {
  251. return ( ( $this->_zendQueue->getJob($id) ) ? true : false );
  252. }
  253. /********************************************************************
  254. * Supporting functions
  255. ********************************************************************/
  256. /**
  257. * Return a list of queue capabilities functions
  258. *
  259. * $array['function name'] = true or false
  260. * true is supported, false is not supported.
  261. *
  262. * @param string $name
  263. *
  264. * @return array
  265. */
  266. public function getCapabilities ()
  267. {
  268. return array(
  269. 'create' => false,
  270. 'delete' => false,
  271. 'getQueues' => false,
  272. 'isExists' => false,
  273. 'count' => true,
  274. 'send' => true,
  275. 'receive' => true,
  276. 'deleteMessage' => true,
  277. );
  278. }
  279. /********************************************************************
  280. * Functions that are not part of the Zend_Queue_Adapter_AdapterAbstract
  281. ********************************************************************/
  282. /**
  283. * Serialize
  284. *
  285. * @return array
  286. */
  287. public function __sleep ()
  288. {
  289. return array( '_options' );
  290. }
  291. /**
  292. * Unserialize
  293. *
  294. * @return void
  295. */
  296. public function __wakeup ()
  297. {
  298. $options = $this->_options['daemonOptions'];
  299. $this->_zendQueue = new ZendApi_Queue( $options['host'] );
  300. if (!$this->_zendQueue) {
  301. require_once 'Zend/Queue/Exception.php';
  302. throw new Zend_Queue_Exception( 'Platform Job Queue connection failed' );
  303. }
  304. if (!$this->_zendQueue->login($options['password'])) {
  305. require_once 'Zend/Queue/Exception.php';
  306. throw new Zend_Queue_Exception( 'Job Queue login failed' );
  307. }
  308. }
  309. }