forked from brianlmoon/GearmanManager
-
Notifications
You must be signed in to change notification settings - Fork 1
/
pear-manager.php
executable file
·181 lines (137 loc) · 4.95 KB
/
pear-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
#!/usr/bin/env php
<?php
/**
* Implements the worker portions of the PEAR Net_Gearman library
*
* @author Brian Moon <brian@moonspot.net>
* @copyright 1997-Present Brian Moon
* @package GearmanManager
*
*/
declare(ticks = 1);
require dirname(__FILE__)."/GearmanManager.php";
/**
* Implements the worker portions of the PEAR Net_Gearman library
*/
class GearmanPearManager extends GearmanManager {
public static $LOG = array();
private $start_time;
/**
* Starts a worker for the PEAR library
*
* @param array $worker_list List of worker functions to add
* @return void
*
*/
protected function start_lib_worker($worker_list) {
/**
* Require PEAR Net_Gearman libs
*/
define('NET_GEARMAN_JOB_PATH', $this->worker_dir);
require "Net/Gearman/Worker.php";
$worker = new Net_Gearman_Worker($this->servers);
foreach($worker_list as $w){
$this->log("Adding job $w", GearmanManager::LOG_LEVEL_WORKER_INFO);
$worker->addAbility($w);
}
$worker->attachCallback(array($this, 'job_start'), Net_Gearman_Worker::JOB_START);
$worker->attachCallback(array($this, 'job_complete'), Net_Gearman_Worker::JOB_COMPLETE);
$worker->attachCallback(array($this, 'job_fail'), Net_Gearman_Worker::JOB_FAIL);
$this->start_time = time();
$worker->beginWork(array($this, "monitor"));
}
/**
* Monitor call back for worker. Return false to stop worker
*
* @param bool $idle If true the worker was idle
* @param int $lastJob The time the last job was run
* @return bool
*
*/
public function monitor($idle, $lastJob) {
if($this->max_run_time > 0 && time() - $this->start_time > $this->max_run_time) {
$this->log("Been running too long, exiting", GearmanManager::LOG_LEVEL_WORKER_INFO);
$this->stop_work = true;
}
$time = time() - $lastJob;
$this->log("Worker's last job $time seconds ago", GearmanManager::LOG_LEVEL_CRAZY);
return $this->stop_work;
}
/**
* Call back for when jobs are started
*/
public function job_start($handle, $job, $args) {
$this->log("($handle) Starting Job: $job", GearmanManager::LOG_LEVEL_WORKER_INFO);
$this->log("($handle) Workload: ".json_encode($args), GearmanManager::LOG_LEVEL_DEBUG);
self::$LOG = array();
}
/**
* Call back for when jobs are completed
*/
public function job_complete($handle, $job, $result) {
$this->log("($handle) Completed Job: $job", GearmanManager::LOG_LEVEL_WORKER_INFO);
$this->log_result($handle, $result);
}
/**
* Call back for when jobs fail
*/
public function job_fail($handle, $job, $result) {
$this->log("($handle) Failed Job: $job", GearmanManager::LOG_LEVEL_WORKER_INFO);
$this->log_result($handle, $result);
}
/**
* Logs the result of complete/failed jobs
*
* @param mixed $result Result returned from worker
* @return void
*
*/
private function log_result($handle, $result) {
if(!empty(self::$LOG)){
foreach(self::$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("($handle) $ln", GearmanManager::LOG_LEVEL_WORKER_INFO);
}
} else {
$this->log("($handle) $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("($handle) $ln", GearmanManager::LOG_LEVEL_DEBUG);
}
} else {
$this->log("($handle) $result_log", GearmanManager::LOG_LEVEL_DEBUG);
}
}
/**
* Validates the PECL compatible worker files/functions
*/
protected function validate_lib_workers($worker_files) {
require "Net/Gearman/Job/Common.php";
foreach($worker_files as $file){
$class = "Net_Gearman_Job_".substr(basename($file), 0, -4);
include $file;
if(!class_exists($class) && !method_exists($class, "run")) {
$this->log("Class $class not found in $file or run method not present");
posix_kill($this->pid, SIGUSR2);
exit();
}
}
}
}
$mgr = new GearmanPearManager();
?>