Passed
Push — master ( 6457fe...f42879 )
by Ioannes
07:49
created
src/Cron.php 1 patch
Spacing   +48 added lines, -48 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]);
@@ -180,7 +180,7 @@  discard block
 block discarded – undo
180 180
 
181 181
         $jobs = $this->getCronJobs();
182 182
 
183
-        if(is_array($jobs) && !empty($jobs)) {
183
+        if (is_array($jobs) && !empty($jobs)) {
184 184
 
185 185
             /*
186 186
              * Минимально допустимый период выполнения одной задачи
@@ -188,9 +188,9 @@  discard block
 block discarded – undo
188 188
              */
189 189
             $this->minAgentPeriod = (count($jobs) + 1) * EnvHelper::getBxCrontabPeriod();
190 190
 
191
-            foreach($jobs as $cmd => $job) {
191
+            foreach ($jobs as $cmd => $job) {
192 192
 
193
-                if($this->isActualJob($job)) {
193
+                if ($this->isActualJob($job)) {
194 194
 
195 195
                     $job['status'] = self::EXEC_STATUS_WORK;
196 196
                     $this->updaateJob($cmd, $job);
@@ -202,12 +202,12 @@  discard block
 block discarded – undo
202 202
                         $timeStart = microtime(true);
203 203
                         $returnCode = $command->run($cmdInput, $output);
204 204
 
205
-                        if(!$returnCode) {
205
+                        if (!$returnCode) {
206 206
 
207 207
                             $job['status'] = self::EXEC_STATUS_SUCCESS;
208 208
 
209 209
                             $msg = sprintf("%s: SUCCESS [%.2f s]", $cmd, microtime(true) - $timeStart);
210
-                            if($this->logger) {
210
+                            if ($this->logger) {
211 211
                                 $this->logger->alert($msg);
212 212
                             }
213 213
                             $output->writeln(PHP_EOL . $msg);
@@ -218,7 +218,7 @@  discard block
 block discarded – undo
218 218
                             $job['error_code'] = $returnCode;
219 219
 
220 220
                             $msg = sprintf("%s: ERROR [%.2f s]", $cmd, microtime(true) - $timeStart);
221
-                            if($this->logger) {
221
+                            if ($this->logger) {
222 222
                                 $this->logger->alert($msg);
223 223
                             }
224 224
                             $output->writeln(PHP_EOL . $msg);
@@ -230,7 +230,7 @@  discard block
 block discarded – undo
230 230
                         $job['error'] = $e->getMessage();
231 231
 
232 232
 
233
-                        if($this->logger) {
233
+                        if ($this->logger) {
234 234
                             $this->logger->error($e, ['command' => $cmd]);
235 235
                         }
236 236
                         $output->writeln(PHP_EOL . 'ERROR: ' . $e->getMessage());
@@ -252,21 +252,21 @@  discard block
 block discarded – undo
252 252
 
253 253
     protected function isActualJob(&$job) {
254 254
 
255
-        if($job['status'] == self::EXEC_STATUS_WORK) {
255
+        if ($job['status'] == self::EXEC_STATUS_WORK) {
256 256
             return false;
257 257
         }
258 258
 
259 259
         $period = intval($job['period']);
260 260
 
261
-        if($period > 0) {
262
-            if($period < $this->minAgentPeriod) {
261
+        if ($period > 0) {
262
+            if ($period < $this->minAgentPeriod) {
263 263
                 $job['orig_period'] = $period;
264 264
                 $period = $job['period'] = $this->minAgentPeriod;
265 265
             }
266
-            if(time() - $job['last_exec'] >= $period) {
266
+            if (time() - $job['last_exec'] >= $period) {
267 267
                 return true;
268 268
             }
269
-        } else if(!empty($job['times'])) {
269
+        } else if (!empty($job['times'])) {
270 270
             //TODO:
271 271
         }
272 272
 
@@ -276,7 +276,7 @@  discard block
 block discarded – undo
276 276
     public function getCronJobs() {
277 277
 
278 278
         $crontab = $this->getCronTab();
279
-        if($crontab === false) {
279
+        if ($crontab === false) {
280 280
             return false;
281 281
         }
282 282
 
@@ -286,9 +286,9 @@  discard block
 block discarded – undo
286 286
         $commands = $app->all();
287 287
 
288 288
         $selfCommands = [];
289
-        foreach($commands as $command) {
289
+        foreach ($commands as $command) {
290 290
             /** @var BxCommand $command */
291
-            if($command instanceof BxCommand) {
291
+            if ($command instanceof BxCommand) {
292 292
                 $name = $command->getName();
293 293
                 $selfCommands[$name] = [
294 294
                     'object' => $command,
@@ -298,19 +298,19 @@  discard block
 block discarded – undo
298 298
 
299 299
         $agents = [];
300 300
         $reader = new AnnotationReader();
301
-        foreach($selfCommands as $cmd => $selfCommand) {
301
+        foreach ($selfCommands as $cmd => $selfCommand) {
302 302
             $reflectionClass = new \ReflectionClass($selfCommand['object']);
303 303
             $annotations = $reader->getClassAnnotations($reflectionClass);
304 304
 
305
-            foreach($annotations as $annotation) {
306
-                if($annotation instanceof Agent) {
305
+            foreach ($annotations as $annotation) {
306
+                if ($annotation instanceof Agent) {
307 307
                     $agents[$cmd] = $annotation->toArray();
308 308
                 }
309 309
             }
310 310
         }
311 311
 
312
-        foreach($crontab as $cmd => $job) {
313
-            if(is_array($job) && isset($agents[$cmd])) {
312
+        foreach ($crontab as $cmd => $job) {
313
+            if (is_array($job) && isset($agents[$cmd])) {
314 314
                 $agents[$cmd] = array_merge($job, $agents[$cmd]);
315 315
             }
316 316
         }
@@ -329,7 +329,7 @@  discard block
 block discarded – undo
329 329
 
330 330
         $crontab = $this->getCronTab();
331 331
 
332
-        if($crontab === false) {
332
+        if ($crontab === false) {
333 333
             return false;
334 334
         } else {
335 335
             $crontab = array_merge($crontab, $changedAgents);
@@ -347,7 +347,7 @@  discard block
 block discarded – undo
347 347
         $fh = fopen($filename, 'c');
348 348
         if (flock($fh, LOCK_EX)) {
349 349
             ftruncate($fh, 0);
350
-            if(!fwrite($fh, json_encode($agents, JSON_PRETTY_PRINT))) {
350
+            if (!fwrite($fh, json_encode($agents, JSON_PRETTY_PRINT))) {
351 351
                 throw new \Exception('Unable to write BX_CRONTAB : ' . $filename);
352 352
             }
353 353
         } else {
@@ -367,14 +367,14 @@  discard block
 block discarded – undo
367 367
         $filename = EnvHelper::getCrontabFile();
368 368
 
369 369
         $fh = fopen($filename, 'r');
370
-        if(!$fh) {
370
+        if (!$fh) {
371 371
             return false;
372 372
         }
373
-        if(flock($fh, LOCK_SH)) {
373
+        if (flock($fh, LOCK_SH)) {
374 374
             $cronTab = [];
375
-            if($data = fread($fh, filesize($filename))) {
375
+            if ($data = fread($fh, filesize($filename))) {
376 376
                 $decoded = json_decode($data, true);
377
-                if(is_array($decoded)) {
377
+                if (is_array($decoded)) {
378 378
                     $cronTab = $decoded;
379 379
                 }
380 380
             }
@@ -389,14 +389,14 @@  discard block
 block discarded – undo
389 389
 
390 390
     protected function sortCronTab(array &$crontab, $sort = self::SORT_NAME) {
391 391
 
392
-        if($sort == self::SORT_TIME) {
392
+        if ($sort == self::SORT_TIME) {
393 393
             $sorting = [];
394
-            foreach($crontab as $cmd => $data) {
394
+            foreach ($crontab as $cmd => $data) {
395 395
                 $sorting[$cmd] = $data['last_exec'];
396 396
             }
397 397
             arsort($sorting, SORT_NUMERIC);
398 398
             $sorted = [];
399
-            foreach($sorting as $cmd => $time) {
399
+            foreach ($sorting as $cmd => $time) {
400 400
                 $sorted[$cmd] = $crontab[$cmd];
401 401
             }
402 402
             $crontab = $sorted;
Please login to merge, or discard this patch.