/
MongoQueue.php
221 lines (174 loc) · 5.57 KB
/
MongoQueue.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
<?php
abstract class MongoQueue
{
public static $database = null;
public static $connection = null;
public static $environment = null;
public static $context = null;
public static $collectionName = 'mongo_queue';
protected static $environmentLoaded = false;
public static function push($className, $methodName, $parameters, $when, $batch = false, $priority = null)
{
if (!$batch)
{
$collection = self::getCollection();
$collection->save(array(
'object_class' => $className,
'object_method' => $methodName,
'parameters' => $parameters,
'when' => $when,
'priority' => $priority,
'locked' => null,
'locked_at' => null,
'batch' => 1));
}
else
{
$db = self::getDatabase(array('object_class' => $className, 'object_method' => $methodName, 'parameters' => $parameters));
$collection = $db->selectCollection(self::$collectionName);
$job = $db->command(
array(
'findandmodify' => self::$collectionName,
'query' => array('object_class' => $className, 'object_method' => $methodName, 'parameters' => $parameters, 'locked' => null),
'update' => array('$inc' => array('batch' => 1)),
'upsert' => true,
'new' => true
));
if ($job['ok'])
{
$job = $job['value'];
$touched = false;
// take the lower 'when'
if (!isset($job['when']) || $job['when'] > $when)
{
$job['when'] = $when;
$touched = true;
}
// take the higher 'priority'
if (!isset($job['priority']) || ($priority !== null && $job['priority'] < $priority))
{
$job['priority'] = $priority;
$touched = true;
}
if ($touched)
$collection->save($job);
}
}
}
public static function hasRunnable($class_name = null, $method_name = null)
{
$collection = self::getCollection();
$query = array('$or' => array(array('when' => array('$lte' => time())), array('when' => null)), 'locked' => null);
$fields = array('_id' => 0, 'when' => 1);
if ($class_name)
$query['object_class'] = $class_name;
if ($method_name)
$query['object_method'] = $method_name;
return ($collection->findOne($query, $fields) != null);
}
public static function count()
{
$collection = self::getCollection();
$query = array('$or' => array(array('when' => array('$lte' => time())), array('when' => null)), 'locked' => null);
return $collection->count($query);
}
public static function run($class_name = null, $method_name = null, $prioritize = true)
{
$db = self::getDatabase();
$environment = self::initializeEnvironment();
$query = array('$or' => array(array('when' => array('$lte' => time())), array('when' => null)), 'locked' => null);
if ($class_name)
$query['object_class'] = $class_name;
if ($method_name)
$query['object_method'] = $method_name;
$sort = array('when' => 1);
if ($prioritize)
$sort = array('priority' => -1, 'when' => 1);
$job = $db->command(
array(
"findandmodify" => self::$collectionName,
"query" => $query,
"sort" => $sort,
"update" => array('$set' => array('locked' => true, 'locked_at' => time()))
));
if ($job['ok'])
{
$jobRecord = $job['value'];
$jobID = $jobRecord['_id'];
// run the job
if (isset($jobRecord['object_class']))
{
$className = $jobRecord['object_class'];
$method = isset($jobRecord['object_method']) ? $jobRecord['object_method'] : 'perform';
$parameters = isset($jobRecord['parameters']) ? $jobRecord['parameters'] : array();
if (self::$context)
{
foreach (self::$context as $key => $value)
{
if (property_exists($className, $key))
$className::$$key = $value;
}
}
call_user_func_array(array(new $className, $method), $parameters);
}
// remove the job from the queue
$db->selectCollection(self::$collectionName)->remove(array('_id' => $jobID));
return true;
}
return false;
}
protected static function getConnection($hint = null)
{
if (is_array(self::$connection))
{
$count = count(self::$connection);
if (!$hint)
$hint = md5(rand());
// convert the hint into an index
$hint = abs(crc32(serialize($hint)) % $count);
return self::$connection[$hint];
}
else
{
return self::$connection;
}
}
protected static function getDatabase($hint = null)
{
$collection_name = self::$collectionName;
$connection = self::getConnection($hint);
if (self::$database == null)
throw new Exception("BaseMongoRecord::database must be initialized to a proper database string");
if ($connection == null)
throw new Exception("BaseMongoRecord::connection must be initialized to a valid Mongo object");
if (!$connection->connected)
$connection->connect();
return $connection->selectDB(self::$database);
}
protected static function getCollection($hint = null)
{
$collection_name = self::$collectionName;
$connection = self::getConnection($hint);
if (self::$database == null)
throw new Exception("BaseMongoRecord::database must be initialized to a proper database string");
if ($connection == null)
throw new Exception("BaseMongoRecord::connection must be initialized to a valid Mongo object");
if (!$connection->connected)
$connection->connect();
return $connection->selectCollection(self::$database, $collection_name);
}
protected static function initializeEnvironment()
{
if (self::$environment && !self::$environmentLoaded)
{
$environment = self::$environment;
spl_autoload_register(
function ($className) use ($environment)
{
require_once($environment . '/' . $className . '.php');
}
);
self::$environmentLoaded = true;
}
}
}