PageRenderTime 44ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://github.com/Doap/iCms---intelligent-Content-management-system
PHP | 343 lines | 164 code | 43 blank | 136 comment | 20 complexity | 6f399fe3080dca525c2da02433181efa 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: PlatformJobQueue.php 20096 2010-01-06 02:05:09Z bkarwin $
  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-2010 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. * @return void
  47. */
  48. public function __construct($options, Zend_Queue $queue = null)
  49. {
  50. parent::__construct($options, $queue);
  51. if (!extension_loaded("jobqueue_client")) {
  52. require_once 'Zend/Queue/Exception.php';
  53. throw new Zend_Queue_Exception('Platform Job Queue extension does not appear to be loaded');
  54. }
  55. if (! isset($this->_options['daemonOptions'])) {
  56. require_once 'Zend/Queue/Exception.php';
  57. throw new Zend_Queue_Exception('Job Queue host and password should be provided');
  58. }
  59. $options = $this->_options['daemonOptions'];
  60. if (!array_key_exists('host', $options)) {
  61. require_once 'Zend/Queue/Exception.php';
  62. throw new Zend_Queue_Exception('Platform Job Queue host should be provided');
  63. }
  64. if (!array_key_exists('password', $options)) {
  65. require_once 'Zend/Queue/Exception.php';
  66. throw new Zend_Queue_Exception('Platform Job Queue password should be provided');
  67. }
  68. $this->_zendQueue = new ZendApi_Queue($options['host']);
  69. if (!$this->_zendQueue) {
  70. require_once 'Zend/Queue/Exception.php';
  71. throw new Zend_Queue_Exception('Platform Job Queue connection failed');
  72. }
  73. if (!$this->_zendQueue->login($options['password'])) {
  74. require_once 'Zend/Queue/Exception.php';
  75. throw new Zend_Queue_Exception('Job Queue login failed');
  76. }
  77. if ($this->_queue) {
  78. $this->_queue->setMessageClass('Zend_Queue_Message_PlatformJob');
  79. }
  80. }
  81. /********************************************************************
  82. * Queue management functions
  83. ********************************************************************/
  84. /**
  85. * Does a queue already exist?
  86. *
  87. * @param string $name
  88. * @return boolean
  89. * @throws Zend_Queue_Exception (not supported)
  90. */
  91. public function isExists($name)
  92. {
  93. require_once 'Zend/Queue/Exception.php';
  94. throw new Zend_Queue_Exception('isExists() is not supported in this adapter');
  95. }
  96. /**
  97. * Create a new queue
  98. *
  99. * @param string $name queue name
  100. * @param integer $timeout default visibility timeout
  101. * @return void
  102. * @throws Zend_Queue_Exception
  103. */
  104. public function create($name, $timeout=null)
  105. {
  106. require_once 'Zend/Queue/Exception.php';
  107. throw new Zend_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. require_once 'Zend/Queue/Exception.php';
  119. throw new Zend_Queue_Exception('delete() is not supported in ' . get_class($this));
  120. }
  121. /**
  122. * Get an array of all available queues
  123. *
  124. * @return void
  125. * @throws Zend_Queue_Exception
  126. */
  127. public function getQueues()
  128. {
  129. require_once 'Zend/Queue/Exception.php';
  130. throw new Zend_Queue_Exception('getQueues() is not supported in this adapter');
  131. }
  132. /**
  133. * Return the approximate number of messages in the queue
  134. *
  135. * @param Zend_Queue|null $queue
  136. * @return integer
  137. */
  138. public function count(Zend_Queue $queue = null)
  139. {
  140. if ($queue !== null) {
  141. require_once 'Zend/Queue/Exception.php';
  142. throw new Zend_Queue_Exception('Queue parameter is not supported');
  143. }
  144. return $this->_zendQueue->getNumOfJobsInQueue();
  145. }
  146. /********************************************************************
  147. * Messsage management functions
  148. ********************************************************************/
  149. /**
  150. * Send a message to the queue
  151. *
  152. * @param array | ZendAPI_job $message Message to send to the active queue
  153. * @param Zend_Queue $queue Not supported
  154. * @return Zend_Queue_Message
  155. * @throws Zend_Queue_Exception
  156. */
  157. public function send($message, Zend_Queue $queue = null)
  158. {
  159. if ($queue !== null) {
  160. require_once 'Zend/Queue/Exception.php';
  161. throw new Zend_Queue_Exception('Queue parameter is not supported');
  162. }
  163. // This adapter can work only for this message type
  164. $classname = $this->_queue->getMessageClass();
  165. if (!class_exists($classname)) {
  166. require_once 'Zend/Loader.php';
  167. Zend_Loader::loadClass($classname);
  168. }
  169. if ($message instanceof ZendAPI_Job) {
  170. $message = array('data' => $message);
  171. }
  172. $zendApiJob = new $classname($message);
  173. // Unfortunately, the Platform JQ API is PHP4-style...
  174. $platformJob = $zendApiJob->getJob();
  175. $jobId = $this->_zendQueue->addJob($platformJob);
  176. if (!$jobId) {
  177. require_once 'Zend/Queue/Exception.php';
  178. throw new Zend_Queue_Exception('Failed to add a job to queue: '
  179. . $this->_zendQueue->getLastError());
  180. }
  181. $zendApiJob->setJobId($jobId);
  182. return $zendApiJob;
  183. }
  184. /**
  185. * Get messages in the queue
  186. *
  187. * @param integer $maxMessages Maximum number of messages to return
  188. * @param integer $timeout Ignored
  189. * @param Zend_Queue $queue Not supported
  190. * @throws Zend_Queue_Exception
  191. * @return ArrayIterator
  192. */
  193. public function receive($maxMessages = null, $timeout = null, Zend_Queue $queue = null)
  194. {
  195. if ($maxMessages === null) {
  196. $maxMessages = 1;
  197. }
  198. if ($queue !== null) {
  199. require_once 'Zend/Queue/Exception.php';
  200. throw new Zend_Queue_Exception('Queue shouldn\'t be set');
  201. }
  202. $jobs = $this->_zendQueue->getJobsInQueue(null, $maxMessages, true);
  203. $classname = $this->_queue->getMessageClass();
  204. if (!class_exists($classname)) {
  205. require_once 'Zend/Loader.php';
  206. Zend_Loader::loadClass($classname);
  207. }
  208. $options = array(
  209. 'queue' => $this->_queue,
  210. 'data' => $jobs,
  211. 'messageClass' => $this->_queue->getMessageClass(),
  212. );
  213. $classname = $this->_queue->getMessageSetClass();
  214. if (!class_exists($classname)) {
  215. require_once 'Zend/Loader.php';
  216. Zend_Loader::loadClass($classname);
  217. }
  218. return new $classname($options);
  219. }
  220. /**
  221. * Delete a message from the queue
  222. *
  223. * Returns true if the message is deleted, false if the deletion is
  224. * unsuccessful.
  225. *
  226. * @param Zend_Queue_Message $message
  227. * @return boolean
  228. * @throws Zend_Queue_Exception
  229. */
  230. public function deleteMessage(Zend_Queue_Message $message)
  231. {
  232. if (get_class($message) != $this->_queue->getMessageClass()) {
  233. require_once 'Zend/Queue/Exception.php';
  234. throw new Zend_Queue_Exception(
  235. 'Failed to remove job from the queue; only messages of type '
  236. . 'Zend_Queue_Message_PlatformJob may be used'
  237. );
  238. }
  239. return $this->_zendQueue->removeJob($message->getJobId());
  240. }
  241. public function isJobIdExist($id)
  242. {
  243. return (($this->_zendQueue->getJob($id))? true : false);
  244. }
  245. /********************************************************************
  246. * Supporting functions
  247. ********************************************************************/
  248. /**
  249. * Return a list of queue capabilities functions
  250. *
  251. * $array['function name'] = true or false
  252. * true is supported, false is not supported.
  253. *
  254. * @param string $name
  255. * @return array
  256. */
  257. public function getCapabilities()
  258. {
  259. return array(
  260. 'create' => false,
  261. 'delete' => false,
  262. 'getQueues' => false,
  263. 'isExists' => false,
  264. 'count' => true,
  265. 'send' => true,
  266. 'receive' => true,
  267. 'deleteMessage' => true,
  268. );
  269. }
  270. /********************************************************************
  271. * Functions that are not part of the Zend_Queue_Adapter_AdapterAbstract
  272. ********************************************************************/
  273. /**
  274. * Serialize
  275. *
  276. * @return array
  277. */
  278. public function __sleep()
  279. {
  280. return array('_options');
  281. }
  282. /**
  283. * Unserialize
  284. *
  285. * @return void
  286. */
  287. public function __wakeup()
  288. {
  289. $options = $this->_options['daemonOptions'];
  290. $this->_zendQueue = new ZendApi_Queue($options['host']);
  291. if (!$this->_zendQueue) {
  292. require_once 'Zend/Queue/Exception.php';
  293. throw new Zend_Queue_Exception('Platform Job Queue connection failed');
  294. }
  295. if (!$this->_zendQueue->login($options['password'])) {
  296. require_once 'Zend/Queue/Exception.php';
  297. throw new Zend_Queue_Exception('Job Queue login failed');
  298. }
  299. }
  300. }