|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Scheduler\Worker; |
|
4
|
|
|
|
|
5
|
|
|
use DateInterval; |
|
6
|
|
|
use DateTime; |
|
7
|
|
|
use Scheduler\Exception\SchedulerException; |
|
8
|
|
|
use Scheduler\JobRunner\JobRunnerInterface; |
|
9
|
|
|
use Scheduler\SchedulerInterface; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Class AbstractWorker |
|
13
|
|
|
* @package Scheduler\Worker |
|
14
|
|
|
* @author Aleh Hutnikau, <[email protected]> |
|
15
|
|
|
*/ |
|
16
|
|
|
abstract class AbstractWorker implements WorkerInterface |
|
17
|
|
|
{ |
|
18
|
|
|
const DEFAULT_ITERATION_INTERVAL = 'PT60S'; |
|
19
|
|
|
|
|
20
|
|
|
/** @var DateTime */ |
|
21
|
|
|
private $from; |
|
22
|
|
|
|
|
23
|
|
|
/** @var DateInterval */ |
|
24
|
|
|
private $interval; |
|
25
|
|
|
|
|
26
|
|
|
/** @var bool */ |
|
27
|
|
|
private $shutdown = false; |
|
28
|
|
|
|
|
29
|
|
|
/** @var integer */ |
|
30
|
|
|
private $iteration = 0; |
|
31
|
|
|
|
|
32
|
|
|
/** @var integer */ |
|
33
|
|
|
private $maxIterations = 1000; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @param integer $startTime |
|
37
|
|
|
* @param string $interval |
|
38
|
|
|
* @return mixed|string |
|
39
|
|
|
* @throws SchedulerException |
|
40
|
|
|
*/ |
|
41
|
2 |
|
public function run($startTime, $interval) |
|
42
|
|
|
{ |
|
43
|
2 |
|
$this->init([$startTime, $interval]); |
|
44
|
1 |
|
$jobRunner = $this->getJobRunner(); |
|
45
|
|
|
|
|
46
|
1 |
|
$from = clone($this->from); |
|
47
|
|
|
|
|
48
|
1 |
|
while ($this->isRunning()) { |
|
49
|
1 |
|
$to = new DateTime('now', new \DateTimeZone('UTC')); |
|
50
|
1 |
|
$jobRunner->run($this->getScheduler(), $from, $to, true); |
|
51
|
1 |
|
$from = clone($to); |
|
52
|
1 |
|
sleep($this->getSeconds($this->interval)); |
|
53
|
1 |
|
$this->iteration++; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
1 |
|
return 'Shut down scheduler worker'; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @return JobRunnerInterface |
|
61
|
|
|
*/ |
|
62
|
|
|
abstract protected function getJobRunner(); |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @return SchedulerInterface |
|
66
|
|
|
*/ |
|
67
|
|
|
abstract protected function getScheduler(); |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @return bool |
|
71
|
|
|
*/ |
|
72
|
1 |
|
public function isRunning() |
|
73
|
|
|
{ |
|
74
|
1 |
|
pcntl_signal_dispatch(); |
|
75
|
|
|
|
|
76
|
1 |
|
if ($this->iteration >= $this->getMaxIterations()) { |
|
77
|
1 |
|
$this->shutdown(); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
1 |
|
if ($this->shutdown) { |
|
81
|
1 |
|
return false; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
1 |
|
return true; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Set marker to shutdown after finishing current iteration |
|
89
|
|
|
*/ |
|
90
|
1 |
|
public function shutdown() |
|
91
|
|
|
{ |
|
92
|
1 |
|
$this->shutdown = true; |
|
93
|
1 |
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* @param $iterations |
|
97
|
|
|
* @throws SchedulerException |
|
98
|
|
|
* @return mixed|void |
|
99
|
|
|
*/ |
|
100
|
2 |
|
public function setMaxIterations($iterations) |
|
101
|
|
|
{ |
|
102
|
2 |
|
if (!is_integer($iterations)) { |
|
103
|
1 |
|
throw new SchedulerException('$iterations parameter must be integer'); |
|
104
|
|
|
} |
|
105
|
1 |
|
$this->maxIterations = $iterations; |
|
106
|
1 |
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Get amount of seconds in date interval |
|
110
|
|
|
* |
|
111
|
|
|
* @param DateInterval $interval |
|
112
|
|
|
* @return int |
|
113
|
|
|
* @throws \Exception |
|
114
|
|
|
*/ |
|
115
|
1 |
|
private function getSeconds(DateInterval $interval) |
|
116
|
|
|
{ |
|
117
|
1 |
|
$date = new \DateTimeImmutable('now', new \DateTimeZone('UTC')); |
|
118
|
1 |
|
$date2 = $date->add($interval); |
|
119
|
1 |
|
return $date2->getTimestamp() - $date->getTimestamp(); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* @param array $params |
|
124
|
|
|
* @throws |
|
125
|
|
|
*/ |
|
126
|
2 |
|
private function init($params = []) |
|
127
|
|
|
{ |
|
128
|
2 |
|
if (!isset($params[0]) || !is_numeric($params[0])) { |
|
129
|
1 |
|
throw new SchedulerException('Start time parameter must be numeric'); |
|
130
|
|
|
} |
|
131
|
1 |
|
$this->from = new DateTime('@' . $params[0], new \DateTimeZone('UTC')); |
|
132
|
|
|
|
|
133
|
1 |
|
$this->interval = new \DateInterval(self::DEFAULT_ITERATION_INTERVAL); |
|
134
|
1 |
|
if (isset($params[1])) { |
|
135
|
1 |
|
$this->interval = new \DateInterval($params[1]); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
1 |
|
$this->registerSigHandlers(); |
|
139
|
1 |
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* Register signal handlers that a worker should respond to. |
|
143
|
|
|
* |
|
144
|
|
|
* TERM/INT/QUIT: Shutdown after the current job is finished then exit. |
|
145
|
|
|
*/ |
|
146
|
1 |
|
private function registerSigHandlers() |
|
147
|
|
|
{ |
|
148
|
1 |
|
if (!function_exists('pcntl_signal')) { |
|
149
|
|
|
//worker ran without pcntl |
|
150
|
|
|
function pcntl_signal() { |
|
151
|
|
|
//do nothing |
|
152
|
|
|
} |
|
153
|
|
|
define('SIGTERM', 15); |
|
154
|
|
|
define('SIGINT', 2); |
|
155
|
|
|
define('SIGQUIT', 3); |
|
156
|
|
|
} |
|
157
|
1 |
|
if (!function_exists('pcntl_signal_dispatch')) { |
|
158
|
|
|
function pcntl_signal_dispatch() { |
|
|
|
|
|
|
159
|
|
|
//do nothing |
|
160
|
|
|
}; |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
declare(ticks = 1); |
|
164
|
|
|
|
|
165
|
1 |
|
pcntl_signal(SIGTERM, [$this, 'shutdown']); |
|
166
|
1 |
|
pcntl_signal(SIGINT, [$this, 'shutdown']); |
|
167
|
1 |
|
pcntl_signal(SIGQUIT, [$this, 'shutdown']); |
|
168
|
1 |
|
} |
|
169
|
|
|
|
|
170
|
|
|
/** |
|
171
|
|
|
* @return integer |
|
172
|
|
|
*/ |
|
173
|
1 |
|
public function getMaxIterations() |
|
174
|
|
|
{ |
|
175
|
1 |
|
return $this->maxIterations; |
|
176
|
|
|
} |
|
177
|
|
|
} |
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.