This repository has been archived by the owner on Jan 1, 2018. It is now read-only.
forked from othlu/GearmanManager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pecl-manager.php
executable file
·229 lines (168 loc) · 6.79 KB
/
pecl-manager.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
#!/usr/bin/env php
<?php
/**
* Implements the worker portions of the pecl/gearman library
*
* @author Brian Moon <[email protected]>
* @copyright 1997-Present Brian Moon
* @package GearmanManager
*
*/
declare(ticks = 1);
require dirname(__FILE__)."/GearmanManager.php";
/**
* Implements the worker portions of the pecl/gearman library
*/
class GearmanPeclManager extends GearmanManager {
/**
* Starts a worker for the PECL library
*
* @param array $worker_list List of worker functions to add
* @param array $timeouts list of worker timeouts to pass to server
* @return void
*
*/
protected function start_lib_worker($worker_list, $timeouts = array()) {
$thisWorker = new GearmanWorker();
$thisWorker->addOptions(GEARMAN_WORKER_NON_BLOCKING);
$thisWorker->setTimeout(5000);
foreach($this->servers as $s){
$this->log("Adding server $s", GearmanManager::LOG_LEVEL_WORKER_INFO);
$thisWorker->addServers($s);
}
foreach($worker_list as $w){
$timeout = (isset($timeouts[$w]) ? $timeouts[$w] : null);
$this->log("Adding job $w ; timeout: " . $timeout, GearmanManager::LOG_LEVEL_WORKER_INFO);
$thisWorker->addFunction($w, array($this, "do_job"), $this, $timeout);
}
$start = time();
while(!$this->stop_work){
if($thisWorker->work() ||
$thisWorker->returnCode() == GEARMAN_IO_WAIT ||
$thisWorker->returnCode() == GEARMAN_NO_JOBS) {
if ($thisWorker->returnCode() == GEARMAN_SUCCESS) continue;
if (!$thisWorker->wait()){
if ($thisWorker->returnCode() == GEARMAN_NO_ACTIVE_FDS){
sleep(5);
}
}
}
/**
* Check the running time of the current child. If it has
* been too long, stop working.
*/
if($this->max_run_time > 0 && time() - $start > $this->max_run_time) {
$this->log("Been running too long, exiting", GearmanManager::LOG_LEVEL_WORKER_INFO);
$this->stop_work = true;
}
if(!empty($this->config["max_runs_per_worker"]) && $this->job_execution_count >= $this->config["max_runs_per_worker"]) {
$this->log("Ran $this->job_execution_count jobs which is over the maximum({$this->config['max_runs_per_worker']}), exiting", GearmanManager::LOG_LEVEL_WORKER_INFO);
$this->stop_work = true;
}
}
$thisWorker->unregisterAll();
}
/**
* Wrapper function handler for all registered functions
* This allows us to do some nice logging when jobs are started/finished
*/
public function do_job($job) {
static $objects;
if($objects===null) $objects = array();
$w = $job->workload();
$h = $job->handle();
$job_name = $job->functionName();
if($this->prefix){
$func = $this->prefix.$job_name;
} else {
$func = $job_name;
}
if (!JobFailProtection::pushJob($job->unique())) {
$this->log("($h) Abort Processing Job - loop detected: ".$job->unique(), GearmanManager::LOG_LEVEL_DEBUG);
return false;
}
if(empty($objects[$job_name]) && !function_exists($func) && !class_exists($func, false)){
if(!isset($this->functions[$job_name])){
$this->log("Function $func is not a registered job name");
return;
}
require_once $this->functions[$job_name]["path"];
if(class_exists($func) && method_exists($func, "run")){
$this->log("Creating a $func object", GearmanManager::LOG_LEVEL_WORKER_INFO);
$objects[$job_name] = new $func();
} elseif(!function_exists($func)) {
$this->log("Function $func not found");
return;
}
}
$this->log("($h) Starting Job: $job_name (unique: ".$job->unique().")", GearmanManager::LOG_LEVEL_WORKER_INFO);
$this->log("($h) Workload: $w", GearmanManager::LOG_LEVEL_DEBUG);
$log = array();
/**
* Run the real function here
*/
if(isset($objects[$job_name])){
$this->log("($h) Calling object for $job_name.", GearmanManager::LOG_LEVEL_DEBUG);
$result = $objects[$job_name]->run($job, $log);
} elseif(function_exists($func)) {
$this->log("($h) Calling function for $job_name.", GearmanManager::LOG_LEVEL_DEBUG);
$result = $func($job, $log);
} else {
$this->log("($h) FAILED to find a function or class for $job_name.", GearmanManager::LOG_LEVEL_INFO);
}
if(!empty($log)){
foreach($log as $l){
if(!is_scalar($l)){
$l = explode("\n", trim(print_r($l, true)));
} elseif(strlen($l) > 256){
$l = substr($l, 0, 256)."...(truncated)";
}
if(is_array($l)){
foreach($l as $ln){
$this->log("($h) $ln", GearmanManager::LOG_LEVEL_WORKER_INFO);
}
} else {
$this->log("($h) $l", GearmanManager::LOG_LEVEL_WORKER_INFO);
}
}
}
$result_log = $result;
if(!is_scalar($result_log)){
$result_log = explode("\n", trim(print_r($result_log, true)));
} elseif(strlen($result_log) > 256){
$result_log = substr($result_log, 0, 256)."...(truncated)";
}
if(is_array($result_log)){
foreach($result_log as $ln){
$this->log("($h) $ln", GearmanManager::LOG_LEVEL_DEBUG);
}
} else {
$this->log("($h) $result_log", GearmanManager::LOG_LEVEL_DEBUG);
}
/**
* Workaround for PECL bug #17114
* http://pecl.php.net/bugs/bug.php?id=17114
*/
$type = gettype($result);
settype($result, $type);
$this->job_execution_count++;
return $result;
}
/**
* Validates the PECL compatible worker files/functions
*/
protected function validate_lib_workers() {
foreach($this->functions as $func => $props){
require_once $props["path"];
$real_func = $this->prefix.$func;
if(!function_exists($real_func) &&
(!class_exists($real_func) || !method_exists($real_func, "run"))){
$this->log("Function $real_func not found in ".$props["path"]);
posix_kill($this->pid, SIGUSR2);
exit();
}
}
}
}
$mgr = new GearmanPeclManager();
?>