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 | 1 | $reports = []; |
|
49 | 1 | while ($this->isRunning()) { |
|
50 | 1 | $to = new DateTime(); |
|
51 | 1 | $to->setTimestamp(time()); |
|
52 | 1 | $to->setTimezone(new \DateTimeZone('UTC')); |
|
53 | 1 | $reports = $jobRunner->run($this->getScheduler(), $from, $to, true); |
|
54 | 1 | $from = clone($to); |
|
55 | 1 | $from->add($oneSecondInterval); |
|
56 | 1 | sleep($this->getSeconds($this->interval)); |
|
57 | 1 | $this->iteration++; |
|
58 | 1 | } |
|
59 | |||
60 | 1 | return $reports; |
|
61 | } |
||
62 | |||
63 | /** |
||
64 | * @return JobRunnerInterface |
||
65 | */ |
||
66 | abstract protected function getJobRunner(); |
||
67 | |||
68 | /** |
||
69 | * @return SchedulerInterface |
||
70 | */ |
||
71 | abstract protected function getScheduler(); |
||
72 | |||
73 | /** |
||
74 | * @return bool |
||
75 | */ |
||
76 | 1 | public function isRunning() |
|
77 | { |
||
78 | 1 | if (function_exists('pcntl_signal_dispatch')) {pcntl_signal_dispatch();}; |
|
79 | |||
80 | 1 | if ($this->iteration >= $this->getMaxIterations()) { |
|
81 | 1 | $this->shutdown(); |
|
82 | 1 | } |
|
83 | |||
84 | 1 | if ($this->shutdown) { |
|
85 | 1 | return false; |
|
86 | } |
||
87 | |||
88 | 1 | return true; |
|
89 | } |
||
90 | |||
91 | /** |
||
92 | * Set marker to shutdown after finishing current iteration |
||
93 | */ |
||
94 | 1 | public function shutdown() |
|
98 | |||
99 | /** |
||
100 | * @param $iterations |
||
101 | * @throws SchedulerException |
||
102 | * @return mixed|void |
||
103 | */ |
||
104 | 2 | public function setMaxIterations($iterations) |
|
111 | |||
112 | /** |
||
113 | * Get amount of seconds in date interval |
||
114 | * |
||
115 | * @param DateInterval $interval |
||
116 | * @return int |
||
117 | * @throws \Exception |
||
118 | */ |
||
119 | 1 | private function getSeconds(DateInterval $interval) |
|
125 | |||
126 | /** |
||
127 | * @param integer $startTime |
||
128 | * @param string $interval |
||
129 | * @throws |
||
130 | */ |
||
131 | 2 | private function init($startTime, $interval) |
|
132 | { |
||
133 | 2 | if (!is_numeric($startTime)) { |
|
134 | 1 | throw new SchedulerException('Start time parameter must be numeric'); |
|
135 | } |
||
136 | 1 | $this->from = new DateTime(); |
|
137 | 1 | $this->from->setTimestamp($startTime); |
|
138 | 1 | $this->from->setTimezone(new \DateTimeZone('UTC')); |
|
139 | |||
140 | 1 | $this->interval = new \DateInterval(self::DEFAULT_ITERATION_INTERVAL); |
|
141 | 1 | if ($interval !== null) { |
|
142 | 1 | $this->interval = new \DateInterval($interval); |
|
143 | 1 | } |
|
144 | |||
145 | 1 | $this->registerSigHandlers(); |
|
146 | 1 | } |
|
147 | |||
148 | /** |
||
149 | * Register signal handlers that a worker should respond to. |
||
150 | * |
||
151 | * TERM/INT/QUIT: Shutdown after the current job is finished then exit. |
||
152 | */ |
||
153 | 1 | private function registerSigHandlers() |
|
160 | |||
161 | /** |
||
162 | * @return integer |
||
163 | */ |
||
164 | 1 | public function getMaxIterations() |
|
168 | } |