PageRenderTime 53ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://github.com/Exercise/zf2
PHP | 318 lines | 136 code | 40 blank | 142 comment | 17 complexity | 1d352841bc5ba1dc8d32a2b581ec2057 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-2010 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. /**
  23. * @namespace
  24. */
  25. namespace Zend\Queue\Adapter;
  26. use Zend\Queue;
  27. use Zend\Queue\Message;
  28. /**
  29. * Zend Platform JobQueue adapter
  30. *
  31. * @uses \ZendAPI_Queue
  32. * @uses \ZendAPI_Job
  33. * @uses \Zend\Queue\Adapter\AdapterAbstract
  34. * @uses \Zend\Queue\Queue
  35. * @uses \Zend\Queue\Exception
  36. * @uses \Zend\Queue\Message\Message
  37. * @category Zend
  38. * @package Zend_Queue
  39. * @subpackage Adapter
  40. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  41. * @license http://framework.zend.com/license/new-bsd New BSD License
  42. */
  43. class PlatformJobQueue extends AbstractAdapter
  44. {
  45. /**
  46. * @var \ZendAPI_Queue
  47. */
  48. protected $_zendQueue;
  49. /**
  50. * Constructor
  51. *
  52. * @param array|\Zend\Config\Config $options
  53. * @param \Zend\Queue\Queue|null $queue
  54. * @return void
  55. */
  56. public function __construct($options, Queue\Queue $queue = null)
  57. {
  58. parent::__construct($options, $queue);
  59. if (!extension_loaded("jobqueue_client")) {
  60. throw new Queue\Exception('Platform Job Queue extension does not appear to be loaded');
  61. }
  62. if (! isset($this->_options['daemonOptions'])) {
  63. throw new Queue\Exception('Job Queue host and password should be provided');
  64. }
  65. $options = $this->_options['daemonOptions'];
  66. if (!array_key_exists('host', $options)) {
  67. throw new Queue\Exception('Platform Job Queue host should be provided');
  68. }
  69. if (!array_key_exists('password', $options)) {
  70. throw new Queue\Exception('Platform Job Queue password should be provided');
  71. }
  72. $this->_zendQueue = new \ZendAPI_Queue($options['host']);
  73. if (!$this->_zendQueue) {
  74. throw new Queue\Exception('Platform Job Queue connection failed');
  75. }
  76. if (!$this->_zendQueue->login($options['password'])) {
  77. throw new Queue\Exception('Job Queue login failed');
  78. }
  79. if ($this->_queue) {
  80. $this->_queue->setMessageClass('\Zend\Queue\Message\PlatformJob');
  81. }
  82. }
  83. /********************************************************************
  84. * Queue management functions
  85. ********************************************************************/
  86. /**
  87. * Does a queue already exist?
  88. *
  89. * @param string $name
  90. * @return boolean
  91. * @throws \Zend\Queue\Exception (not supported)
  92. */
  93. public function isExists($name)
  94. {
  95. throw new Queue\Exception('isExists() is not supported in this adapter');
  96. }
  97. /**
  98. * Create a new queue
  99. *
  100. * @param string $name queue name
  101. * @param integer $timeout default visibility timeout
  102. * @return void
  103. * @throws \Zend\Queue\Exception
  104. */
  105. public function create($name, $timeout=null)
  106. {
  107. throw new Queue\Exception('create() is not supported in ' . get_class($this));
  108. }
  109. /**
  110. * Delete a queue and all of its messages
  111. *
  112. * @param string $name queue name
  113. * @return void
  114. * @throws \Zend\Queue\Exception
  115. */
  116. public function delete($name)
  117. {
  118. throw new Queue\Exception('delete() is not supported in ' . get_class($this));
  119. }
  120. /**
  121. * Get an array of all available queues
  122. *
  123. * @return void
  124. * @throws \Zend\Queue\Exception
  125. */
  126. public function getQueues()
  127. {
  128. throw new Queue\Exception('getQueues() is not supported in this adapter');
  129. }
  130. /**
  131. * Return the approximate number of messages in the queue
  132. *
  133. * @param \Zend\Queue\Queue|null $queue
  134. * @return integer
  135. */
  136. public function count(Queue\Queue $queue = null)
  137. {
  138. if ($queue !== null) {
  139. throw new Queue\Exception('Queue parameter is not supported');
  140. }
  141. return $this->_zendQueue->getNumOfJobsInQueue();
  142. }
  143. /********************************************************************
  144. * Messsage management functions
  145. ********************************************************************/
  146. /**
  147. * Send a message to the queue
  148. *
  149. * @param array|\ZendAPI_Job $message Message to send to the active queue
  150. * @param \Zend\Queue\Queue $queue Not supported
  151. * @return \Zend\Queue\Message\Message
  152. * @throws \Zend\Queue\Exception
  153. */
  154. public function send($message, Queue\Queue $queue = null)
  155. {
  156. if ($queue !== null) {
  157. throw new Queue\Exception('Queue parameter is not supported');
  158. }
  159. // This adapter can work only for this message type
  160. $classname = $this->_queue->getMessageClass();
  161. if ($message instanceof \ZendAPI_Job) {
  162. $message = array('data' => $message);
  163. }
  164. $zendApiJob = new $classname($message);
  165. // Unfortunately, the Platform JQ API is PHP4-style...
  166. $platformJob = $zendApiJob->getJob();
  167. $jobId = $this->_zendQueue->addJob($platformJob);
  168. if (!$jobId) {
  169. throw new Queue\Exception('Failed to add a job to queue: '
  170. . $this->_zendQueue->getLastError());
  171. }
  172. $zendApiJob->setJobId($jobId);
  173. return $zendApiJob;
  174. }
  175. /**
  176. * Get messages in the queue
  177. *
  178. * @param integer $maxMessages Maximum number of messages to return
  179. * @param integer $timeout Ignored
  180. * @param \Zend\Queue\Queue $queue Not supported
  181. * @throws \Zend\Queue\Exception
  182. * @return ArrayIterator
  183. */
  184. public function receive($maxMessages = null, $timeout = null, Queue\Queue $queue = null)
  185. {
  186. if ($maxMessages === null) {
  187. $maxMessages = 1;
  188. }
  189. if ($queue !== null) {
  190. throw new Queue\Exception('Queue shouldn\'t be set');
  191. }
  192. $jobs = $this->_zendQueue->getJobsInQueue(null, $maxMessages, true);
  193. $options = array(
  194. 'queue' => $this->_queue,
  195. 'data' => $jobs,
  196. 'messageClass' => $this->_queue->getMessageClass(),
  197. );
  198. $classname = $this->_queue->getMessageSetClass();
  199. return new $classname($options);
  200. }
  201. /**
  202. * Delete a message from the queue
  203. *
  204. * Returns true if the message is deleted, false if the deletion is
  205. * unsuccessful.
  206. *
  207. * @param \Zend\Queue\Message\Message $message
  208. * @return boolean
  209. * @throws \Zend\Queue\Exception
  210. */
  211. public function deleteMessage(Message\Message $message)
  212. {
  213. if (get_class($message) != $this->_queue->getMessageClass()) {
  214. throw new Queue\Exception(
  215. 'Failed to remove job from the queue; only messages of type '
  216. . '\Zend\Queue\Message\PlatformJob may be used'
  217. );
  218. }
  219. return $this->_zendQueue->removeJob($message->getJobId());
  220. }
  221. public function isJobIdExist($id)
  222. {
  223. return (($this->_zendQueue->getJob($id))? true : false);
  224. }
  225. /********************************************************************
  226. * Supporting functions
  227. ********************************************************************/
  228. /**
  229. * Return a list of queue capabilities functions
  230. *
  231. * $array['function name'] = true or false
  232. * true is supported, false is not supported.
  233. *
  234. * @param string $name
  235. * @return array
  236. */
  237. public function getCapabilities()
  238. {
  239. return array(
  240. 'create' => false,
  241. 'delete' => false,
  242. 'getQueues' => false,
  243. 'isExists' => false,
  244. 'count' => true,
  245. 'send' => true,
  246. 'receive' => true,
  247. 'deleteMessage' => true,
  248. );
  249. }
  250. /********************************************************************
  251. * Functions that are not part of the \Zend\Queue\Adapter\AdapterAbstract
  252. ********************************************************************/
  253. /**
  254. * Serialize
  255. *
  256. * @return array
  257. */
  258. public function __sleep()
  259. {
  260. return array('_options');
  261. }
  262. /**
  263. * Unserialize
  264. *
  265. * @return void
  266. */
  267. public function __wakeup()
  268. {
  269. $options = $this->_options['daemonOptions'];
  270. $this->_zendQueue = new \ZendAPI_Queue($options['host']);
  271. if (!$this->_zendQueue) {
  272. throw new Queue\Exception('Platform Job Queue connection failed');
  273. }
  274. if (!$this->_zendQueue->login($options['password'])) {
  275. throw new Queue\Exception('Job Queue login failed');
  276. }
  277. }
  278. }