Passed
Push — master ( 2363c9...402a54 )
by Ioannes
07:53
created
src/Cron.php 1 patch
Spacing   +50 added lines, -50 removed lines patch added patch discarded remove patch
@@ -40,45 +40,45 @@  discard block
 block discarded – undo
40 40
         set_time_limit(EnvHelper::getCrontabTimeout());
41 41
 
42 42
         $logger = EnvHelper::getLogger('bx_cron');
43
-        if($logger) {
43
+        if ($logger) {
44 44
             $this->setLogger($logger);
45 45
         }
46 46
 
47 47
         $showStatus = $input->getOption('status');
48 48
         $byTime = $input->getOption('bytime');
49
-        if($showStatus) {
49
+        if ($showStatus) {
50 50
             $sort = ($byTime ? self::SORT_TIME : self::SORT_NAME);
51 51
             $this->showStatus($output, $sort);
52 52
             return 0;
53 53
         }
54 54
 
55
-        if(EnvHelper::getSwitch('BX_CRONTAB_RUN', EnvHelper::SWITCH_STATE_OFF)) {
56
-            if($this->logger) {
55
+        if (EnvHelper::getSwitch('BX_CRONTAB_RUN', EnvHelper::SWITCH_STATE_OFF)) {
56
+            if ($this->logger) {
57 57
                 $this->logger->alert('BxCron switch off');
58 58
             }
59 59
             return 0;
60 60
         }
61 61
 
62
-        if(!$this->lock()) {
62
+        if (!$this->lock()) {
63 63
             $msg = 'The command is already running in another process.';
64 64
             $output->writeln($msg);
65
-            if($this->logger) {
65
+            if ($this->logger) {
66 66
                 $this->logger->warning($msg);
67 67
             }
68 68
             return 0;
69 69
         }
70 70
 
71
-        if($sleepInterval = EnvHelper::checkSleepInterval()) {
71
+        if ($sleepInterval = EnvHelper::checkSleepInterval()) {
72 72
             $msg = sprintf("Sleep in interval %s", $sleepInterval);
73 73
             $output->writeln($msg);
74
-            if($this->logger) {
74
+            if ($this->logger) {
75 75
                 $this->logger->warning($msg);
76 76
             }
77 77
             return 0;
78 78
         }
79 79
 
80 80
         $clean = $input->getOption('clean');
81
-        if($clean) {
81
+        if ($clean) {
82 82
             $command = $this->getApplication()->find($clean);
83 83
             $this->cleanJob($command->getName());
84 84
             $output->writeln($command->getName() . " will be executed now");
@@ -86,7 +86,7 @@  discard block
 block discarded – undo
86 86
         }
87 87
 
88 88
         $cleanAll = $input->getOption('all');
89
-        if($cleanAll) {
89
+        if ($cleanAll) {
90 90
             $this->cleanJob();
91 91
             $output->writeln("All commands will be executed now");
92 92
             return 0;
@@ -111,10 +111,10 @@  discard block
 block discarded – undo
111 111
         $lastExec = 0;
112 112
         $hasError = false;
113 113
 
114
-        foreach($jobs as $cmd => $job) {
114
+        foreach ($jobs as $cmd => $job) {
115 115
             $execTime = $job['last_exec'];
116
-            if($execTime > $lastExec) $lastExec = $execTime;
117
-            if(!empty($job['error'])) {
116
+            if ($execTime > $lastExec) $lastExec = $execTime;
117
+            if (!empty($job['error'])) {
118 118
                 $hasError = true;
119 119
             }
120 120
         }
@@ -133,7 +133,7 @@  discard block
 block discarded – undo
133 133
             'Status',
134 134
         ];
135 135
 
136
-        if($hasError) {
136
+        if ($hasError) {
137 137
             $header[] = 'Error';
138 138
         }
139 139
 
@@ -143,15 +143,15 @@  discard block
 block discarded – undo
143 143
         ]);
144 144
 
145 145
         $cnt = 1;
146
-        foreach($jobs as $cmd => $job) {
147
-            if($cnt > 1) $table->addRow(new TableSeparator());
146
+        foreach ($jobs as $cmd => $job) {
147
+            if ($cnt > 1) $table->addRow(new TableSeparator());
148 148
             $row = [
149 149
                 $cmd,
150 150
                 $job['period'],
151 151
                 ($job['last_exec'] ? date("d.m.Y H:i:s", $job['last_exec']) : 'NONE'),
152 152
                 $job['status'],
153 153
             ];
154
-            if($hasError) {
154
+            if ($hasError) {
155 155
                 $row[] = $job['error'];
156 156
             }
157 157
             $table->addRow($row);
@@ -165,9 +165,9 @@  discard block
 block discarded – undo
165 165
 
166 166
         $crontab = [];
167 167
 
168
-        if($command) {
168
+        if ($command) {
169 169
             $crontab = $this->getCronTab();
170
-            if($crontab === false) {
170
+            if ($crontab === false) {
171 171
                 return false;
172 172
             }
173 173
             unset($crontab[$command]);
@@ -182,7 +182,7 @@  discard block
 block discarded – undo
182 182
         $allTimeout = EnvHelper::getCrontabTimeout();
183 183
         $workTime = 0;
184 184
 
185
-        if(!empty($jobs)) {
185
+        if (!empty($jobs)) {
186 186
 
187 187
             /*
188 188
              * Минимально допустимый период выполнения одной задачи
@@ -193,9 +193,9 @@  discard block
 block discarded – undo
193 193
             $this->logger->alert($msg);
194 194
             $output->writeln($msg);
195 195
 
196
-            foreach($jobs as $cmd => $job) {
196
+            foreach ($jobs as $cmd => $job) {
197 197
 
198
-                if($this->isActualJob($job)) {
198
+                if ($this->isActualJob($job)) {
199 199
 
200 200
                     $job['status'] = self::EXEC_STATUS_WORK;
201 201
                     $job['start_time'] = time();
@@ -210,12 +210,12 @@  discard block
 block discarded – undo
210 210
                         $returnCode = $command->run($cmdInput, $output);
211 211
                         $execTime = microtime(true) - $timeStart;
212 212
 
213
-                        if(!$returnCode) {
213
+                        if (!$returnCode) {
214 214
 
215 215
                             $job['status'] = self::EXEC_STATUS_SUCCESS;
216 216
 
217 217
                             $msg = sprintf("%s: SUCCESS [%.2f s]", $cmd, $execTime);
218
-                            if($this->logger) {
218
+                            if ($this->logger) {
219 219
                                 $this->logger->alert($msg);
220 220
                             }
221 221
                             $output->writeln(PHP_EOL . $msg);
@@ -226,7 +226,7 @@  discard block
 block discarded – undo
226 226
                             $job['error_code'] = $returnCode;
227 227
 
228 228
                             $msg = sprintf("%s: ERROR [%.2f s]", $cmd, $execTime);
229
-                            if($this->logger) {
229
+                            if ($this->logger) {
230 230
                                 $this->logger->alert($msg);
231 231
                             }
232 232
                             $output->writeln(PHP_EOL . $msg);
@@ -238,14 +238,14 @@  discard block
 block discarded – undo
238 238
                         $job['error'] = $e->getMessage();
239 239
 
240 240
 
241
-                        if($this->logger) {
241
+                        if ($this->logger) {
242 242
                             $this->logger->error($e, ['command' => $cmd]);
243 243
                         }
244 244
                         $output->writeln(PHP_EOL . 'ERROR: ' . $e->getMessage());
245 245
 
246 246
                     } finally {
247 247
 
248
-                        if(!$execTime) {
248
+                        if (!$execTime) {
249 249
                             $execTime = microtime(true) - $timeStart;
250 250
                         }
251 251
                         $job['last_exec'] = time();
@@ -255,7 +255,7 @@  discard block
 block discarded – undo
255 255
                     $this->updateJob($cmd, $job);
256 256
 
257 257
                     $workTime += $execTime;
258
-                    if($workTime * 2 > $allTimeout) {
258
+                    if ($workTime * 2 > $allTimeout) {
259 259
                         break;
260 260
                     }
261 261
                     /*
@@ -269,21 +269,21 @@  discard block
 block discarded – undo
269 269
 
270 270
     protected function isActualJob(&$job) {
271 271
 
272
-        if($job['status'] == self::EXEC_STATUS_WORK) {
272
+        if ($job['status'] == self::EXEC_STATUS_WORK) {
273 273
             return false;
274 274
         }
275 275
 
276 276
         $period = intval($job['period']);
277 277
 
278
-        if($period > 0) {
279
-            if($period < $this->minAgentPeriod) {
278
+        if ($period > 0) {
279
+            if ($period < $this->minAgentPeriod) {
280 280
                 $job['orig_period'] = $period;
281 281
                 $period = $job['period'] = $this->minAgentPeriod;
282 282
             }
283
-            if(time() - $job['last_exec'] >= $period) {
283
+            if (time() - $job['last_exec'] >= $period) {
284 284
                 return true;
285 285
             }
286
-        } else if(!empty($job['times'])) {
286
+        } else if (!empty($job['times'])) {
287 287
             //TODO:
288 288
         }
289 289
 
@@ -293,7 +293,7 @@  discard block
 block discarded – undo
293 293
     public function getCronJobs() {
294 294
 
295 295
         $crontab = $this->getCronTab();
296
-        if($crontab === false) {
296
+        if ($crontab === false) {
297 297
             return false;
298 298
         }
299 299
 
@@ -303,9 +303,9 @@  discard block
 block discarded – undo
303 303
         $commands = $app->all();
304 304
 
305 305
         $selfCommands = [];
306
-        foreach($commands as $command) {
306
+        foreach ($commands as $command) {
307 307
             /** @var BxCommand $command */
308
-            if($command instanceof BxCommand) {
308
+            if ($command instanceof BxCommand) {
309 309
                 $name = $command->getName();
310 310
                 $selfCommands[$name] = [
311 311
                     'object' => $command,
@@ -315,19 +315,19 @@  discard block
 block discarded – undo
315 315
 
316 316
         $agents = [];
317 317
         $reader = new AnnotationReader();
318
-        foreach($selfCommands as $cmd => $selfCommand) {
318
+        foreach ($selfCommands as $cmd => $selfCommand) {
319 319
             $reflectionClass = new \ReflectionClass($selfCommand['object']);
320 320
             $annotations = $reader->getClassAnnotations($reflectionClass);
321 321
 
322
-            foreach($annotations as $annotation) {
323
-                if($annotation instanceof Agent) {
322
+            foreach ($annotations as $annotation) {
323
+                if ($annotation instanceof Agent) {
324 324
                     $agents[$cmd] = $annotation->toArray();
325 325
                 }
326 326
             }
327 327
         }
328 328
 
329
-        foreach($crontab as $cmd => $job) {
330
-            if(is_array($job) && isset($agents[$cmd])) {
329
+        foreach ($crontab as $cmd => $job) {
330
+            if (is_array($job) && isset($agents[$cmd])) {
331 331
                 $agents[$cmd] = array_merge($job, $agents[$cmd]);
332 332
             }
333 333
         }
@@ -346,7 +346,7 @@  discard block
 block discarded – undo
346 346
 
347 347
         $crontab = $this->getCronTab();
348 348
 
349
-        if($crontab === false) {
349
+        if ($crontab === false) {
350 350
             return false;
351 351
         } else {
352 352
             $crontab = array_merge($crontab, $changedAgents);
@@ -364,7 +364,7 @@  discard block
 block discarded – undo
364 364
         $fh = fopen($filename, 'c');
365 365
         if (flock($fh, LOCK_EX)) {
366 366
             ftruncate($fh, 0);
367
-            if(!fwrite($fh, json_encode($agents, JSON_PRETTY_PRINT))) {
367
+            if (!fwrite($fh, json_encode($agents, JSON_PRETTY_PRINT))) {
368 368
                 throw new \Exception('Unable to write BX_CRONTAB : ' . $filename);
369 369
             }
370 370
         } else {
@@ -384,16 +384,16 @@  discard block
 block discarded – undo
384 384
         $filename = EnvHelper::getCrontabFile();
385 385
 
386 386
         $fh = fopen($filename, 'r');
387
-        if(!$fh) {
387
+        if (!$fh) {
388 388
             return false;
389 389
         }
390
-        if(flock($fh, LOCK_SH)) {
390
+        if (flock($fh, LOCK_SH)) {
391 391
             $cronTab = [];
392 392
             clearstatcache();
393 393
             $filesize = (int) filesize($filename);
394
-            if($filesize > 0 && $data = fread($fh, $filesize)) {
394
+            if ($filesize > 0 && $data = fread($fh, $filesize)) {
395 395
                 $decoded = json_decode($data, true);
396
-                if(is_array($decoded)) {
396
+                if (is_array($decoded)) {
397 397
                     $cronTab = $decoded;
398 398
                 } else {
399 399
                     throw new \Exception("Unable to parse cronTab");
@@ -410,14 +410,14 @@  discard block
 block discarded – undo
410 410
 
411 411
     protected function sortCronTab(array &$crontab, $sort = self::SORT_NAME) {
412 412
 
413
-        if($sort == self::SORT_TIME) {
413
+        if ($sort == self::SORT_TIME) {
414 414
             $sorting = [];
415
-            foreach($crontab as $cmd => $data) {
415
+            foreach ($crontab as $cmd => $data) {
416 416
                 $sorting[$cmd] = $data['last_exec'];
417 417
             }
418 418
             arsort($sorting, SORT_NUMERIC);
419 419
             $sorted = [];
420
-            foreach($sorting as $cmd => $time) {
420
+            foreach ($sorting as $cmd => $time) {
421 421
                 $sorted[$cmd] = $crontab[$cmd];
422 422
             }
423 423
             $crontab = $sorted;
Please login to merge, or discard this patch.