1 | <?php |
||
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 | 1 | $oneSecondInterval = new \DateInterval('PT1S'); |
|
48 | |||
49 | 1 | while ($this->isRunning()) { |
|
50 | 1 | $to = DateTime::createFromFormat('U', time(), new \DateTimeZone('UTC')); |
|
51 | 1 | $jobRunner->run($this->getScheduler(), $from, $to, true); |
|
52 | 1 | $from = clone($to); |
|
53 | 1 | $from->add($oneSecondInterval); |
|
54 | 1 | sleep($this->getSeconds($this->interval)); |
|
55 | 1 | $this->iteration++; |
|
56 | } |
||
57 | |||
58 | 1 | return 'Shut down scheduler worker'; |
|
59 | } |
||
60 | |||
61 | /** |
||
62 | * @return JobRunnerInterface |
||
63 | */ |
||
64 | abstract protected function getJobRunner(); |
||
65 | |||
66 | /** |
||
67 | * @return SchedulerInterface |
||
68 | */ |
||
69 | abstract protected function getScheduler(); |
||
70 | |||
71 | /** |
||
72 | * @return bool |
||
73 | */ |
||
74 | 1 | public function isRunning() |
|
88 | |||
89 | /** |
||
90 | * Set marker to shutdown after finishing current iteration |
||
91 | */ |
||
92 | 1 | public function shutdown() |
|
96 | |||
97 | /** |
||
98 | * @param $iterations |
||
99 | * @throws SchedulerException |
||
100 | * @return mixed|void |
||
101 | */ |
||
102 | 2 | public function setMaxIterations($iterations) |
|
109 | |||
110 | /** |
||
111 | * Get amount of seconds in date interval |
||
112 | * |
||
113 | * @param DateInterval $interval |
||
114 | * @return int |
||
115 | * @throws \Exception |
||
116 | */ |
||
117 | 1 | private function getSeconds(DateInterval $interval) |
|
123 | |||
124 | /** |
||
125 | * @param integer $startTime |
||
126 | * @param string $interval |
||
127 | * @throws |
||
128 | */ |
||
129 | 2 | private function init($startTime, $interval) |
|
143 | |||
144 | /** |
||
145 | * Register signal handlers that a worker should respond to. |
||
146 | * |
||
147 | * TERM/INT/QUIT: Shutdown after the current job is finished then exit. |
||
148 | */ |
||
149 | private function registerSigHandlers() |
||
150 | { |
||
151 | if (!function_exists('pcntl_signal')) {function pcntl_signal() {/*do nothing*/}}; |
||
163 | |||
164 | /** |
||
165 | * @return integer |
||
166 | */ |
||
167 | 1 | public function getMaxIterations() |
|
171 | } |
This check looks for type mismatches where the missing type is
false
. This is usually indicative of an error condtion.Consider the follow example
This function either returns a new
DateTime
object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returnedfalse
before passing on the value to another function or method that may not be able to handle afalse
.