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