forked from CoderKungfu/php-queue
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRunner.php
More file actions
165 lines (148 loc) · 4.79 KB
/
Copy pathRunner.php
File metadata and controls
165 lines (148 loc) · 4.79 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
<?php
namespace PHPQueue;
use PHPQueue\Exception\Exception;
abstract class Runner
{
const RUN_USLEEP = 1000000;
/**
* @var \PHPQueue\JobQueue
*/
private $queue;
/**
* @var \PHPQueue\Logger
*/
public $logger;
public $queue_name;
public $log_path;
public $log_level;
protected $current_date;
protected $current_log_check = 0;
protected $max_check_interval = 360;
public function __construct($queue='', $options=array())
{
if (!empty($queue)) {
$this->queue_name = $queue;
}
if (
!empty($options['logPath'])
&& is_writable($options['logPath'])
)
{
$this->log_path = $options['logPath'];
}
if ( !empty($options['logLevel']) ) {
$this->log_level = $options['logLevel'];
} else {
$this->log_level = Logger::INFO;
}
return $this;
}
public function run()
{
$this->setup();
$this->beforeLoop();
$this->loop();
}
public function setup()
{
if (empty($this->log_path)) {
$baseFolder = dirname(dirname(__DIR__));
$this->log_path = sprintf(
'%s/demo/runners/logs/'
, $baseFolder
);
}
$this->createLogger();
}
protected function beforeLoop()
{
if (empty($this->queue_name)) {
throw new Exception('Queue name is invalid');
}
$this->queue = Base::getQueue($this->queue_name);
}
protected function loop()
{
while (true) {
$this->checkAndCycleLog();
$this->workJob();
}
}
protected function checkAndCycleLog()
{
$this->current_log_check++;
if ($this->current_log_check > $this->max_check_interval) {
if ($this->current_date != date('Ymd')) {
$this->logger->addAlert("Cycling log file now.");
$this->logger = Logger::cycleLog(
$this->queue_name
, $this->log_level
, $this->getFullLogPath()
);
}
$this->current_log_check = 0;
}
}
public function workJob()
{
$sleepTime = self::RUN_USLEEP;
$newJob = null;
try {
$newJob = Base::getJob($this->queue);
} catch (\Exception $ex) {
$this->logger->addError($ex->getMessage());
$sleepTime = self::RUN_USLEEP * 5;
}
if (empty($newJob)) {
$this->logger->addNotice("No Job found.");
$sleepTime = self::RUN_USLEEP * 10;
} else {
try {
if (empty($newJob->worker)) {
throw new Exception("No worker declared.");
}
if (is_string($newJob->worker)) {
$result_data = $this->processWorker($newJob->worker, $newJob);
} elseif (is_array($newJob->worker)) {
$this->logger->addInfo(sprintf("Running chained new job (%s) with workers", $new_job->job_id), $newJob->worker);
foreach ($newJob->worker as $worker_name) {
$result_data = $this->processWorker($worker_name, $newJob);
$newJob->data = $result_data;
}
}
return Base::updateJob($this->queue, $newJob->job_id, $result_data);
} catch (\Exception $ex) {
$this->logger->addError($ex->getMessage());
$this->logger->addInfo(sprintf('Releasing job (%s).', $newJob->job_id));
$this->queue->releaseJob($newJob->job_id);
$sleepTime = self::RUN_USLEEP * 5;
}
}
$this->logger->addInfo('Sleeping ' . ceil($sleepTime / 1000000) . ' seconds.');
usleep($sleepTime);
}
protected function processWorker($worker_name, $new_job)
{
$this->logger->addInfo(sprintf("Running new job (%s) with worker: %s", $new_job->job_id, $worker_name));
$worker = Base::getWorker($worker_name);
Base::workJob($worker, $new_job);
$this->logger->addInfo(sprintf('Worker is done. Updating job (%s). Result:', $new_job->job_id), $worker->result_data);
return $worker->result_data;
}
protected function createLogger()
{
$this->logger = Logger::createLogger(
$this->queue_name
, $this->log_level
, $this->getFullLogPath()
);
}
/**
* @return string
*/
protected function getFullLogPath()
{
$this->current_date = date('Ymd');
return sprintf('%s/%s-%s.log', $this->log_path, $this->queue_name, $this->current_date);
}
}