-
Notifications
You must be signed in to change notification settings - Fork 10
/
Beanstalk.php
234 lines (199 loc) · 6.25 KB
/
Beanstalk.php
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
<?php
/**
* li3_queue: queue plugin for the lithium framework
*
* @copyright Copyright 2012, Olivier Louvignes for Union of RAD (http://union-of-rad.org)
* @copyright Inspired by David Persson's Queue plugin for CakePHP.
* @license http://opensource.org/licenses/bsd-license.php The BSD License
*
*/
namespace li3_queue\extensions\adapter\queue;
use lithium\core\NetworkException;
use li3_queue\extensions\adapter\net\socket\Beanstalk as BeanstalkSocket;
class Beanstalk extends \li3_queue\extensions\adapter\Queue {
protected $_autoConfig = array('classes' => 'merge');
protected $_classes = array(
'message' => 'li3_queue\storage\queue\Message',
'service' => '\li3_queue\net\beanstalk\Service'
);
/**
* The `Socket` instance used to send `Service` calls.
*
* @var lithium\net\Socket
*/
public $connection = null;
/**
* Stores the status of this object's connection. Updated when `connect()` or `disconnect()` are
* called, or if an error occurs that closes the object's connection.
*
* @var boolean
*/
protected $_isConnected = false;
/**
* Adds config values to the public properties when a new object is created.
*
* @param array $config Configuration options : default value
* - `'host'` _string_: '127.0.0.1'
* - `'port'` _interger_: 11300
* - `'timeout'` _interger_: 60
* - `'tube'` _string_: 'default'
* - `'kickBound'` _interger_: 100
* - `'persistent'` _boolean_: true
* - `'autoConfirm'` _boolean_: false
* - `'autoConnect'` _boolean_: true
*/
public function __construct(array $config = array()) {
$defaults = array(
'host' => '127.0.0.1',
'port' => 11300,
'timeout' => 60,
'tube' => 'default',
'kickBound' => 100,
'persistent' => true,
'autoConfirm' => false,
'autoConnect' => true
);
parent::__construct($config + $defaults);
}
/* Connection Protocol */
/**
* Connect to the Beanstalk server.
*
* @see lithium\data\source\MongoDb::__construct()
* @link http://php.net/manual/en/mongo.construct.php PHP Manual: Mongo::__construct()
* @return boolean Returns `true` the connection attempt was successful, otherwise `false`.
*/
public function connect() {
$config = &$this->_config;
if(!$this->connection) {
$this->connection = $this->invokeMethod('_instance', array('service', $this->_config));
if($this->connection->connect()) {
$this->_isConnected = true;
$this->connection->choose($config['tube']);
$this->connection->watch($config['tube']);
}
}
return $this->_isConnected;
}
/**
* Checks the connection status of this data source. If the `'autoConnect'` option is set to
* true and the source connection is not currently active, a connection attempt will be made
* before returning the result of the connection status.
*
* @param array $options The options available for this method:
* - 'autoConnect': If true, and the connection is not currently active, calls
* `connect()` on this object. Defaults to `false`.
* @return boolean Returns the current value of `$_isConnected`, indicating whether or not
* the object's connection is currently active. This value may not always be accurate,
* as the connection could have timed out or otherwise been dropped by the remote
* resource during the course of the request.
*/
public function isConnected(array $options = array()) {
return $this->_isConnected;
}
/**
* Disconnect from the Beanstalk server.
*
* @return boolean True on successful disconnect, false otherwise.
*/
public function disconnect() {
if($this->isConnected()) {
return $this->connection->disconnect();
}
}
/* Queue Protocol */
/**
* Write method.
* Sends `put` command to write a message to the queue.
* Defaults are the following:
* - `priority`=0 Messages are processed by priority, lowest first
* - `delay`=0 Number of seconds before moving the message to the ready queue
* - `timeout`=30 Number of seconds a worker can process a message
*
* @param string $data
* @param array $options
* @return boolean
*/
public function write($data, array $options = array()) {
$defaults = array(
'priority' => 0,
'delay' => 0,
'timeout' => 30
);
$options += $defaults;
extract($options, EXTR_OVERWRITE);
$response = $this->connection->put($data, $priority, $delay, $timeout);
if($response->status == 'INSERTED') {
return true;
}
return false;
}
public function read(array $options = array()) {
$defaults = array(
'timeout' => 0
);
$options += $defaults;
extract($options, EXTR_OVERWRITE);
$response = $this->connection->reserve($options);
if(is_object($response) && $response->status == 'RESERVED') {
return $this->_message($response, $options);
}
return null;
}
public function confirm($message, array $options = array()) {
$response = $this->connection->delete($message->id());
if(is_object($response) && $response->status == 'DELETED') {
return true;
}
return false;
}
public function requeue($message, array $options = array()) {
$defaults = array(
'priority' => 0,
'delay' => 0
);
$options += $defaults;
extract($options, EXTR_OVERWRITE);
$response = $this->connection->release($message->id(), $priority, $delay);
if(is_object($response) && $response->status == 'RELEASED') {
return true;
}
return false;
}
public function consume($callback, array $options = array()) {
while($response = $this->connection->reserve()) {
if($response->id){
$message = $this->_message($response);
if($callback($message) === false) {
break;
}
}
}
return false;
}
public function purge() {
do {
$response = $this->connection->reserve(0);
if($response->id) {
$this->connection->delete($response->id);
}
} while ($response->status == 'RESERVED');
return true;
}
protected function _message($response, array $options = array()) {
$defaults = array('class' => 'message');
$options += $defaults;
$class = $options['class'];
$params = array(
'id' => $response->id,
'queue' => $this,
'data' => trim($response->data)
);
$message = $this->invokeMethod('_instance', array($class, $params));
if($this->_config['autoConfirm']) {
$message->confirm();
}
return $message;
}
}
?>