forked from CoderKungfu/php-queue
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBase.php
More file actions
258 lines (249 loc) · 6.94 KB
/
Copy pathBase.php
File metadata and controls
258 lines (249 loc) · 6.94 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
<?php
namespace PHPQueue;
class Base
{
static public $queue_path = null;
static public $worker_path = null;
static public $queue_namespace = null;
static public $worker_namespace = null;
static private $all_queues = array();
static private $all_workers = array();
/**
* @param string $queue
* @param array $options
* @return \PHPQueue\JobQueue
*/
static public function getQueue($queue=null)
{
if (empty($queue))
{
throw new \PHPQueue\Exception("Queue name is empty");
}
if ( empty(self::$all_queues[$queue]) )
{
$className = self::loadAndGetQueueClassName($queue);
try
{
self::$all_queues[$queue] = new $className();
}
catch (Exception $ex)
{
throw new \PHPQueue\Exception($ex->getMessage(), $ex->getCode());
}
}
return self::$all_queues[$queue];
}
static protected function loadAndGetQueueClassName($queue_name=null)
{
$class_name = '';
if (!is_null(self::$queue_path))
{
$classFile = self::$queue_path . '/' . $queue_name . 'Queue.php';
if (file_exists($classFile))
{
require_once $classFile;
}
else
{
throw new \PHPQueue\Exception("Queue file does not exist: $classFile");
}
$class_name = "\\" . $queue_name . 'Queue';
}
if (!is_null(self::$queue_namespace))
{
if (!(strpos(self::$queue_namespace, "\\") === 0))
{
$class_name = "\\";
}
$class_name .= self::$queue_namespace . "\\" . $queue_name;
}
return $class_name;
}
/**
* @param \PHPQueue\JobQueue $queue
* @param array $newJob
* @return boolean
*/
static public function addJob($queue, $newJob=array())
{
if (!is_a($queue, '\\PHPQueue\\JobQueue'))
{
throw new \PHPQueue\Exception("Invalid queue object.");
}
if (empty($newJob))
{
throw new \PHPQueue\Exception("Invalid job data.");
}
$status = false;
try
{
$queue->beforeAdd($newJob);
$status = $queue->addJob($newJob);
$queue->afterAdd();
}
catch (Exception $ex)
{
$queue->onError($ex);
throw $ex;
}
return $status;
}
/**
* @param \PHPQueue\JobQueue $queue
* @param string $jobId
* @return \PHPQueue\Job
*/
static public function getJob($queue, $jobId=null)
{
if (!is_a($queue, '\\PHPQueue\\JobQueue'))
{
throw new \PHPQueue\Exception("Invalid queue object.");
}
$job = null;
try
{
$queue->beforeGet();
$job = $queue->getJob($jobId);
$queue->afterGet();
}
catch (Exception $ex)
{
$queue->onError($ex);
throw $ex;
}
return $job;
}
/**
* @param \PHPQueue\JobQueue $queue
* @param string $jobId
* @param mixed $resultData
* @return boolean
*/
static public function updateJob($queue, $jobId=null, $resultData=null)
{
if (!is_a($queue, '\\PHPQueue\\JobQueue'))
{
throw new \PHPQueue\Exception("Invalid queue object.");
}
$status = false;
try
{
$queue->beforeUpdate();
$queue->updateJob($jobId, $resultData);
$status = $queue->clearJob($jobId);
$queue->afterUpdate();
}
catch (Exception $ex)
{
$queue->onError($ex);
$queue->releaseJob($jobId);
throw $ex;
}
return $status;
}
/**
* @param string $worker_name
* @param array $options
* @return \PHPQueue\Worker
* @throws \PHPQueue\Exception
*/
static public function getWorker($worker_name=null)
{
if (empty($worker_name))
{
throw new \PHPQueue\Exception("Worker name is empty");
}
if ( empty(self::$all_workers[$worker_name]) )
{
$className = self::loadAndGetWorkerClassName($worker_name);
try
{
self::$all_workers[$worker_name] = new $className();
}
catch (Exception $ex)
{
throw new \PHPQueue\Exception($ex->getMessage(), $ex->getCode());
}
}
return self::$all_workers[$worker_name];
}
static protected function loadAndGetWorkerClassName($worker_name=null)
{
$class_name = '';
if (!is_null(self::$worker_path))
{
$classFile = self::$worker_path . '/' . $worker_name . 'Worker.php';
if (file_exists($classFile))
{
require_once $classFile;
}
else
{
throw new \PHPQueue\Exception("Worker file does not exist: $classFile");
}
$class_name = "\\" . $worker_name . 'Worker';
}
if (!is_null(self::$worker_namespace))
{
if (!(strpos(self::$worker_namespace, "\\") === 0))
{
$class_name = "\\";
}
$class_name .= self::$worker_namespace . "\\" . $worker_name;
}
return $class_name;
}
/**
* @param \PHPQueue\Worker $worker
* @param \PHPQueue\Job $job
* @return \PHPQueue\Worker
* @throws \PHPQueue\Exception
*/
static public function workJob($worker, $job)
{
if (!is_a($worker, '\\PHPQueue\\Worker'))
{
throw new \PHPQueue\Exception("Invalid worker object.");
}
if (!is_a($job, '\\PHPQueue\\Job'))
{
throw new \PHPQueue\Exception("Invalid job object.");
}
try
{
$worker->beforeJob($job->data);
$worker->runJob($job);
$job->onSuccessful();
$worker->afterJob();
$worker->onSuccess();
}
catch (Exception $ex)
{
$worker->onError($ex);
$job->onError();
throw $ex;
}
return $worker;
}
/**
* Factory method to instantiate a copy of a backend class.
* @param string $type Case-sensitive class name.
* @param array $options Constuctor options.
* @return \PHPQueue\backend_classname
* @throws \PHPQueue\Exception
*/
static public function backendFactory($type=null, $options=array())
{
$backend_classname = '\\PHPQueue\\Backend\\' . $type;
$obj = new $backend_classname($options);
if (is_a($obj, $backend_classname))
{
return $obj;
}
else
{
throw new \PHPQueue\Exception("Invalid Backend object.");
}
}
}
?>