Passed
Push — master ( 8a09b4...2363c9 )
by Ioannes
01:55
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
                     $this->updateJob($cmd, $job);
@@ -209,12 +209,12 @@  discard block
 block discarded – undo
209 209
                         $returnCode = $command->run($cmdInput, $output);
210 210
                         $execTime = microtime(true) - $timeStart;
211 211
 
212
-                        if(!$returnCode) {
212
+                        if (!$returnCode) {
213 213
 
214 214
                             $job['status'] = self::EXEC_STATUS_SUCCESS;
215 215
 
216 216
                             $msg = sprintf("%s: SUCCESS [%.2f s]", $cmd, $execTime);
217
-                            if($this->logger) {
217
+                            if ($this->logger) {
218 218
                                 $this->logger->alert($msg);
219 219
                             }
220 220
                             $output->writeln(PHP_EOL . $msg);
@@ -225,7 +225,7 @@  discard block
 block discarded – undo
225 225
                             $job['error_code'] = $returnCode;
226 226
 
227 227
                             $msg = sprintf("%s: ERROR [%.2f s]", $cmd, $execTime);
228
-                            if($this->logger) {
228
+                            if ($this->logger) {
229 229
                                 $this->logger->alert($msg);
230 230
                             }
231 231
                             $output->writeln(PHP_EOL . $msg);
@@ -237,14 +237,14 @@  discard block
 block discarded – undo
237 237
                         $job['error'] = $e->getMessage();
238 238
 
239 239
 
240
-                        if($this->logger) {
240
+                        if ($this->logger) {
241 241
                             $this->logger->error($e, ['command' => $cmd]);
242 242
                         }
243 243
                         $output->writeln(PHP_EOL . 'ERROR: ' . $e->getMessage());
244 244
 
245 245
                     } finally {
246 246
 
247
-                        if(!$execTime) {
247
+                        if (!$execTime) {
248 248
                             $execTime = microtime(true) - $timeStart;
249 249
                         }
250 250
                         $job['last_exec'] = time();
@@ -254,7 +254,7 @@  discard block
 block discarded – undo
254 254
                     $this->updateJob($cmd, $job);
255 255
 
256 256
                     $workTime += $execTime;
257
-                    if($workTime * 2 > $allTimeout) {
257
+                    if ($workTime * 2 > $allTimeout) {
258 258
                         break;
259 259
                     }
260 260
                     /*
@@ -268,21 +268,21 @@  discard block
 block discarded – undo
268 268
 
269 269
     protected function isActualJob(&$job) {
270 270
 
271
-        if($job['status'] == self::EXEC_STATUS_WORK) {
271
+        if ($job['status'] == self::EXEC_STATUS_WORK) {
272 272
             return false;
273 273
         }
274 274
 
275 275
         $period = intval($job['period']);
276 276
 
277
-        if($period > 0) {
278
-            if($period < $this->minAgentPeriod) {
277
+        if ($period > 0) {
278
+            if ($period < $this->minAgentPeriod) {
279 279
                 $job['orig_period'] = $period;
280 280
                 $period = $job['period'] = $this->minAgentPeriod;
281 281
             }
282
-            if(time() - $job['last_exec'] >= $period) {
282
+            if (time() - $job['last_exec'] >= $period) {
283 283
                 return true;
284 284
             }
285
-        } else if(!empty($job['times'])) {
285
+        } else if (!empty($job['times'])) {
286 286
             //TODO:
287 287
         }
288 288
 
@@ -292,7 +292,7 @@  discard block
 block discarded – undo
292 292
     public function getCronJobs() {
293 293
 
294 294
         $crontab = $this->getCronTab();
295
-        if($crontab === false) {
295
+        if ($crontab === false) {
296 296
             return false;
297 297
         }
298 298
 
@@ -302,9 +302,9 @@  discard block
 block discarded – undo
302 302
         $commands = $app->all();
303 303
 
304 304
         $selfCommands = [];
305
-        foreach($commands as $command) {
305
+        foreach ($commands as $command) {
306 306
             /** @var BxCommand $command */
307
-            if($command instanceof BxCommand) {
307
+            if ($command instanceof BxCommand) {
308 308
                 $name = $command->getName();
309 309
                 $selfCommands[$name] = [
310 310
                     'object' => $command,
@@ -314,19 +314,19 @@  discard block
 block discarded – undo
314 314
 
315 315
         $agents = [];
316 316
         $reader = new AnnotationReader();
317
-        foreach($selfCommands as $cmd => $selfCommand) {
317
+        foreach ($selfCommands as $cmd => $selfCommand) {
318 318
             $reflectionClass = new \ReflectionClass($selfCommand['object']);
319 319
             $annotations = $reader->getClassAnnotations($reflectionClass);
320 320
 
321
-            foreach($annotations as $annotation) {
322
-                if($annotation instanceof Agent) {
321
+            foreach ($annotations as $annotation) {
322
+                if ($annotation instanceof Agent) {
323 323
                     $agents[$cmd] = $annotation->toArray();
324 324
                 }
325 325
             }
326 326
         }
327 327
 
328
-        foreach($crontab as $cmd => $job) {
329
-            if(is_array($job) && isset($agents[$cmd])) {
328
+        foreach ($crontab as $cmd => $job) {
329
+            if (is_array($job) && isset($agents[$cmd])) {
330 330
                 $agents[$cmd] = array_merge($job, $agents[$cmd]);
331 331
             }
332 332
         }
@@ -345,7 +345,7 @@  discard block
 block discarded – undo
345 345
 
346 346
         $crontab = $this->getCronTab();
347 347
 
348
-        if($crontab === false) {
348
+        if ($crontab === false) {
349 349
             return false;
350 350
         } else {
351 351
             $crontab = array_merge($crontab, $changedAgents);
@@ -363,7 +363,7 @@  discard block
 block discarded – undo
363 363
         $fh = fopen($filename, 'c');
364 364
         if (flock($fh, LOCK_EX)) {
365 365
             ftruncate($fh, 0);
366
-            if(!fwrite($fh, json_encode($agents, JSON_PRETTY_PRINT))) {
366
+            if (!fwrite($fh, json_encode($agents, JSON_PRETTY_PRINT))) {
367 367
                 throw new \Exception('Unable to write BX_CRONTAB : ' . $filename);
368 368
             }
369 369
         } else {
@@ -383,16 +383,16 @@  discard block
 block discarded – undo
383 383
         $filename = EnvHelper::getCrontabFile();
384 384
 
385 385
         $fh = fopen($filename, 'r');
386
-        if(!$fh) {
386
+        if (!$fh) {
387 387
             return false;
388 388
         }
389
-        if(flock($fh, LOCK_SH)) {
389
+        if (flock($fh, LOCK_SH)) {
390 390
             $cronTab = [];
391 391
             clearstatcache();
392 392
             $filesize = (int) filesize($filename);
393
-            if($filesize > 0 && $data = fread($fh, $filesize)) {
393
+            if ($filesize > 0 && $data = fread($fh, $filesize)) {
394 394
                 $decoded = json_decode($data, true);
395
-                if(is_array($decoded)) {
395
+                if (is_array($decoded)) {
396 396
                     $cronTab = $decoded;
397 397
                 } else {
398 398
                     throw new \Exception("Unable to parse cronTab");
@@ -409,14 +409,14 @@  discard block
 block discarded – undo
409 409
 
410 410
     protected function sortCronTab(array &$crontab, $sort = self::SORT_NAME) {
411 411
 
412
-        if($sort == self::SORT_TIME) {
412
+        if ($sort == self::SORT_TIME) {
413 413
             $sorting = [];
414
-            foreach($crontab as $cmd => $data) {
414
+            foreach ($crontab as $cmd => $data) {
415 415
                 $sorting[$cmd] = $data['last_exec'];
416 416
             }
417 417
             arsort($sorting, SORT_NUMERIC);
418 418
             $sorted = [];
419
-            foreach($sorting as $cmd => $time) {
419
+            foreach ($sorting as $cmd => $time) {
420 420
                 $sorted[$cmd] = $crontab[$cmd];
421 421
             }
422 422
             $crontab = $sorted;
Please login to merge, or discard this patch.