|
1
|
|
|
<?php |
|
2
|
|
|
namespace App\BxConsole; |
|
3
|
|
|
|
|
4
|
|
|
use Doctrine\Common\Annotations\AnnotationReader; |
|
5
|
|
|
use Symfony\Component\Console\Helper\Table; |
|
6
|
|
|
use Symfony\Component\Console\Helper\TableCell; |
|
7
|
|
|
use Symfony\Component\Console\Helper\TableSeparator; |
|
8
|
|
|
use Symfony\Component\Console\Input\ArrayInput; |
|
9
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
10
|
|
|
use Symfony\Component\Console\Input\InputOption; |
|
11
|
|
|
use Symfony\Component\Console\Output\ConsoleOutputInterface; |
|
12
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
13
|
|
|
use App\BxConsole\Annotations\Agent; |
|
14
|
|
|
|
|
15
|
|
|
class Cron extends BxCommand { |
|
16
|
|
|
|
|
17
|
|
|
use LockableTrait; |
|
18
|
|
|
|
|
19
|
|
|
const EXEC_STATUS_SUCCESS = 'SUCCESS'; |
|
20
|
|
|
const EXEC_STATUS_ERROR = 'ERROR'; |
|
21
|
|
|
const EXEC_STATUS_WORK = 'WORK'; |
|
22
|
|
|
|
|
23
|
|
|
const SORT_NAME = 'name'; |
|
24
|
|
|
const SORT_TIME = 'time'; |
|
25
|
|
|
|
|
26
|
|
|
private $minAgentPeriod; |
|
27
|
|
|
|
|
28
|
|
|
protected function configure() { |
|
29
|
|
|
|
|
30
|
|
|
$this->setName('system:cron') |
|
31
|
|
|
->setDescription('Job sheduler for application comands') |
|
32
|
|
|
->addOption('status', 's', InputOption::VALUE_NONE, 'Show BX_CRONTAB status table') |
|
33
|
|
|
->addOption('bytime', 't', InputOption::VALUE_NONE, 'Sort status table by exec time desc') |
|
34
|
|
|
->addOption('clean', 'c', InputOption::VALUE_REQUIRED, 'Command to be clean crontab data (status, last exec)') |
|
35
|
|
|
->addOption('all', 'a', InputOption::VALUE_NONE, 'Command to be clean all crontab data (status, last exec)'); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
protected function execute(InputInterface $input, OutputInterface $output): int |
|
39
|
|
|
{ |
|
40
|
|
|
set_time_limit(EnvHelper::getCrontabTimeout()); |
|
41
|
|
|
|
|
42
|
|
|
$logger = EnvHelper::getLogger('bx_cron'); |
|
43
|
|
|
if($logger) { |
|
44
|
|
|
$this->setLogger($logger); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
$showStatus = $input->getOption('status'); |
|
48
|
|
|
$byTime = $input->getOption('bytime'); |
|
49
|
|
|
if($showStatus) { |
|
50
|
|
|
$sort = ($byTime ? self::SORT_TIME : self::SORT_NAME); |
|
51
|
|
|
$this->showStatus($output, $sort); |
|
52
|
|
|
return 0; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
if(EnvHelper::getSwitch('BX_CRONTAB_RUN', EnvHelper::SWITCH_STATE_OFF)) { |
|
56
|
|
|
if($this->logger) { |
|
57
|
|
|
$this->logger->alert('BxCron switch off'); |
|
58
|
|
|
} |
|
59
|
|
|
return 0; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
if(!$this->lock()) { |
|
63
|
|
|
$msg = 'The command is already running in another process.'; |
|
64
|
|
|
$output->writeln($msg); |
|
65
|
|
|
if($this->logger) { |
|
66
|
|
|
$this->logger->warning($msg); |
|
67
|
|
|
} |
|
68
|
|
|
return 0; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
if($sleepInterval = EnvHelper::checkSleepInterval()) { |
|
72
|
|
|
$msg = sprintf("Sleep in interval %s", $sleepInterval); |
|
73
|
|
|
$output->writeln($msg); |
|
74
|
|
|
if($this->logger) { |
|
75
|
|
|
$this->logger->warning($msg); |
|
76
|
|
|
} |
|
77
|
|
|
return 0; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
$clean = $input->getOption('clean'); |
|
81
|
|
|
if($clean) { |
|
82
|
|
|
$command = $this->getApplication()->find($clean); |
|
83
|
|
|
$this->cleanJob($command->getName()); |
|
84
|
|
|
$output->writeln($command->getName() . " will be executed now"); |
|
85
|
|
|
return 0; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
$cleanAll = $input->getOption('all'); |
|
89
|
|
|
if($cleanAll) { |
|
90
|
|
|
$this->cleanJob(); |
|
91
|
|
|
$output->writeln("All commands will be executed now"); |
|
92
|
|
|
return 0; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
$this->executeJobs($output); |
|
96
|
|
|
|
|
97
|
|
|
$this->release(); |
|
98
|
|
|
|
|
99
|
|
|
return 0; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
protected function showStatus(OutputInterface $output, $sort) { |
|
103
|
|
|
|
|
104
|
|
|
$table = new Table($output); |
|
105
|
|
|
$table->setStyle('box-double'); |
|
106
|
|
|
|
|
107
|
|
|
$isSwitchOff = EnvHelper::getSwitch('BX_CRONTAB_RUN', EnvHelper::SWITCH_STATE_OFF); |
|
108
|
|
|
|
|
109
|
|
|
$jobs = $this->getCronJobs(); |
|
110
|
|
|
$this->sortCronTab($jobs, $sort); |
|
|
|
|
|
|
111
|
|
|
$lastExec = 0; |
|
112
|
|
|
$hasError = false; |
|
113
|
|
|
|
|
114
|
|
|
foreach($jobs as $cmd => $job) { |
|
115
|
|
|
$execTime = $job['last_exec']; |
|
116
|
|
|
if($execTime > $lastExec) $lastExec = $execTime; |
|
117
|
|
|
if(!empty($job['error'])) { |
|
118
|
|
|
$hasError = true; |
|
119
|
|
|
} |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
$headStr = sprintf( |
|
123
|
|
|
"BX_CRONTAB_RUN: %s; LAST_EXEC: %s; AGENTS_COUNT: %d", |
|
124
|
|
|
($isSwitchOff ? 'OFF' : 'ON'), |
|
125
|
|
|
($lastExec ? date("d.m.Y H:i:s", $lastExec) : 'NONE'), |
|
126
|
|
|
count($jobs), |
|
127
|
|
|
); |
|
128
|
|
|
|
|
129
|
|
|
$header = [ |
|
130
|
|
|
'Command', |
|
131
|
|
|
'Period', |
|
132
|
|
|
'Last Exec', |
|
133
|
|
|
'Status', |
|
134
|
|
|
]; |
|
135
|
|
|
|
|
136
|
|
|
if($hasError) { |
|
137
|
|
|
$header[] = 'Error'; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
$table->setHeaders([ |
|
141
|
|
|
[new TableCell($headStr, ['colspan' => ($hasError ? 5 : 4)])], |
|
142
|
|
|
$header, |
|
143
|
|
|
]); |
|
144
|
|
|
|
|
145
|
|
|
$cnt = 1; |
|
146
|
|
|
foreach($jobs as $cmd => $job) { |
|
147
|
|
|
if($cnt > 1) $table->addRow(new TableSeparator()); |
|
148
|
|
|
$row = [ |
|
149
|
|
|
$cmd, |
|
150
|
|
|
$job['period'], |
|
151
|
|
|
($job['last_exec'] ? date("d.m.Y H:i:s", $job['last_exec']) : 'NONE'), |
|
152
|
|
|
$job['status'], |
|
153
|
|
|
]; |
|
154
|
|
|
if($hasError) { |
|
155
|
|
|
$row[] = $job['error']; |
|
156
|
|
|
} |
|
157
|
|
|
$table->addRow($row); |
|
158
|
|
|
$cnt++; |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
$table->render(); |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
protected function cleanJob($command = false) { |
|
165
|
|
|
|
|
166
|
|
|
$crontab = []; |
|
167
|
|
|
|
|
168
|
|
|
if($command) { |
|
169
|
|
|
$crontab = $this->getCronTab(); |
|
170
|
|
|
if($crontab === false) { |
|
|
|
|
|
|
171
|
|
|
return false; |
|
172
|
|
|
} |
|
173
|
|
|
unset($crontab[$command]); |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
$this->setCronTab($crontab); |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
protected function executeJobs(OutputInterface $output) { |
|
180
|
|
|
|
|
181
|
|
|
$jobs = $this->getCronJobs(); |
|
182
|
|
|
$allTimeout = EnvHelper::getCrontabTimeout(); |
|
183
|
|
|
$workTime = 0; |
|
184
|
|
|
|
|
185
|
|
|
if(is_array($jobs) && !empty($jobs)) { |
|
|
|
|
|
|
186
|
|
|
|
|
187
|
|
|
/* |
|
188
|
|
|
* Минимально допустимый период выполнения одной задачи |
|
189
|
|
|
* при котором гарантируется выполнение всех задач |
|
190
|
|
|
*/ |
|
191
|
|
|
$this->minAgentPeriod = (count($jobs) + 1) * EnvHelper::getBxCrontabPeriod(); |
|
192
|
|
|
$this->logger->alert(sprintf("Minimal agent period: %d", $this->minAgentPeriod)); |
|
193
|
|
|
|
|
194
|
|
|
foreach($jobs as $cmd => $job) { |
|
195
|
|
|
|
|
196
|
|
|
if($this->isActualJob($job)) { |
|
197
|
|
|
|
|
198
|
|
|
$job['status'] = self::EXEC_STATUS_WORK; |
|
199
|
|
|
$this->updateJob($cmd, $job); |
|
200
|
|
|
|
|
201
|
|
|
$command = $this->getApplication()->find($cmd); |
|
202
|
|
|
$cmdInput = new ArrayInput(['command' => $cmd]); |
|
203
|
|
|
$timeStart = microtime(true); |
|
204
|
|
|
$execTime = 0; |
|
205
|
|
|
try { |
|
206
|
|
|
|
|
207
|
|
|
$returnCode = $command->run($cmdInput, $output); |
|
208
|
|
|
$execTime = microtime(true) - $timeStart; |
|
209
|
|
|
|
|
210
|
|
|
if(!$returnCode) { |
|
211
|
|
|
|
|
212
|
|
|
$job['status'] = self::EXEC_STATUS_SUCCESS; |
|
213
|
|
|
|
|
214
|
|
|
$msg = sprintf("%s: SUCCESS [%.2f s]", $cmd, $execTime); |
|
215
|
|
|
if($this->logger) { |
|
216
|
|
|
$this->logger->alert($msg); |
|
217
|
|
|
} |
|
218
|
|
|
$output->writeln(PHP_EOL . $msg); |
|
219
|
|
|
|
|
220
|
|
|
} else { |
|
221
|
|
|
|
|
222
|
|
|
$job['status'] = self::EXEC_STATUS_ERROR; |
|
223
|
|
|
$job['error_code'] = $returnCode; |
|
224
|
|
|
|
|
225
|
|
|
$msg = sprintf("%s: ERROR [%.2f s]", $cmd, $execTime); |
|
226
|
|
|
if($this->logger) { |
|
227
|
|
|
$this->logger->alert($msg); |
|
228
|
|
|
} |
|
229
|
|
|
$output->writeln(PHP_EOL . $msg); |
|
230
|
|
|
} |
|
231
|
|
|
|
|
232
|
|
|
} catch (\Exception $e) { |
|
233
|
|
|
|
|
234
|
|
|
$job['status'] = self::EXEC_STATUS_ERROR; |
|
235
|
|
|
$job['error'] = $e->getMessage(); |
|
236
|
|
|
|
|
237
|
|
|
|
|
238
|
|
|
if($this->logger) { |
|
239
|
|
|
$this->logger->error($e, ['command' => $cmd]); |
|
240
|
|
|
} |
|
241
|
|
|
$output->writeln(PHP_EOL . 'ERROR: ' . $e->getMessage()); |
|
242
|
|
|
|
|
243
|
|
|
} finally { |
|
244
|
|
|
|
|
245
|
|
|
if(!$execTime) { |
|
246
|
|
|
$execTime = microtime(true) - $timeStart; |
|
247
|
|
|
} |
|
248
|
|
|
$job['last_exec'] = time(); |
|
249
|
|
|
$job['exec_time'] = round($execTime, 1); |
|
250
|
|
|
} |
|
251
|
|
|
|
|
252
|
|
|
$this->updateJob($cmd, $job); |
|
253
|
|
|
|
|
254
|
|
|
$workTime += $execTime; |
|
255
|
|
|
if($workTime * 2 > $allTimeout) { |
|
256
|
|
|
break; |
|
257
|
|
|
} |
|
258
|
|
|
/* |
|
259
|
|
|
* Let's do just one task |
|
260
|
|
|
*/ |
|
261
|
|
|
//break; |
|
262
|
|
|
} |
|
263
|
|
|
} // foreach($jobs as $cmd => $job) |
|
264
|
|
|
} // if(!empty($jobs)) |
|
265
|
|
|
} |
|
266
|
|
|
|
|
267
|
|
|
protected function isActualJob(&$job) { |
|
268
|
|
|
|
|
269
|
|
|
if($job['status'] == self::EXEC_STATUS_WORK) { |
|
270
|
|
|
return false; |
|
271
|
|
|
} |
|
272
|
|
|
|
|
273
|
|
|
$period = intval($job['period']); |
|
274
|
|
|
|
|
275
|
|
|
if($period > 0) { |
|
276
|
|
|
if($period < $this->minAgentPeriod) { |
|
277
|
|
|
$job['orig_period'] = $period; |
|
278
|
|
|
$period = $job['period'] = $this->minAgentPeriod; |
|
279
|
|
|
} |
|
280
|
|
|
if(time() - $job['last_exec'] >= $period) { |
|
281
|
|
|
return true; |
|
282
|
|
|
} |
|
283
|
|
|
} else if(!empty($job['times'])) { |
|
284
|
|
|
//TODO: |
|
285
|
|
|
} |
|
286
|
|
|
|
|
287
|
|
|
return false; |
|
288
|
|
|
} |
|
289
|
|
|
|
|
290
|
|
|
public function getCronJobs() { |
|
291
|
|
|
|
|
292
|
|
|
$crontab = $this->getCronTab(); |
|
293
|
|
|
if($crontab === false) { |
|
|
|
|
|
|
294
|
|
|
return false; |
|
295
|
|
|
} |
|
296
|
|
|
|
|
297
|
|
|
/** @var Application $app */ |
|
298
|
|
|
$app = $this->getApplication(); |
|
299
|
|
|
|
|
300
|
|
|
$commands = $app->all(); |
|
301
|
|
|
|
|
302
|
|
|
$selfCommands = []; |
|
303
|
|
|
foreach($commands as $command) { |
|
304
|
|
|
/** @var BxCommand $command */ |
|
305
|
|
|
if($command instanceof BxCommand) { |
|
306
|
|
|
$name = $command->getName(); |
|
307
|
|
|
$selfCommands[$name] = [ |
|
308
|
|
|
'object' => $command, |
|
309
|
|
|
]; |
|
310
|
|
|
} |
|
311
|
|
|
} |
|
312
|
|
|
|
|
313
|
|
|
$agents = []; |
|
314
|
|
|
$reader = new AnnotationReader(); |
|
315
|
|
|
foreach($selfCommands as $cmd => $selfCommand) { |
|
316
|
|
|
$reflectionClass = new \ReflectionClass($selfCommand['object']); |
|
317
|
|
|
$annotations = $reader->getClassAnnotations($reflectionClass); |
|
318
|
|
|
|
|
319
|
|
|
foreach($annotations as $annotation) { |
|
320
|
|
|
if($annotation instanceof Agent) { |
|
321
|
|
|
$agents[$cmd] = $annotation->toArray(); |
|
322
|
|
|
} |
|
323
|
|
|
} |
|
324
|
|
|
} |
|
325
|
|
|
|
|
326
|
|
|
foreach($crontab as $cmd => $job) { |
|
327
|
|
|
if(is_array($job) && isset($agents[$cmd])) { |
|
328
|
|
|
$agents[$cmd] = array_merge($job, $agents[$cmd]); |
|
329
|
|
|
} |
|
330
|
|
|
} |
|
331
|
|
|
|
|
332
|
|
|
$this->setCronTab($agents); |
|
333
|
|
|
|
|
334
|
|
|
return $agents; |
|
335
|
|
|
} |
|
336
|
|
|
|
|
337
|
|
|
protected function updateJob($cmd, $job) { |
|
338
|
|
|
|
|
339
|
|
|
return $this->updateCronTab([$cmd => $job]); |
|
340
|
|
|
} |
|
341
|
|
|
|
|
342
|
|
|
protected function updateCronTab(array $changedAgents) { |
|
343
|
|
|
|
|
344
|
|
|
$crontab = $this->getCronTab(); |
|
345
|
|
|
|
|
346
|
|
|
if($crontab === false) { |
|
|
|
|
|
|
347
|
|
|
return false; |
|
348
|
|
|
} else { |
|
349
|
|
|
$crontab = array_merge($crontab, $changedAgents); |
|
350
|
|
|
return $this->setCronTab($crontab); |
|
351
|
|
|
} |
|
352
|
|
|
} |
|
353
|
|
|
|
|
354
|
|
|
protected function setCronTab(array $agents) { |
|
355
|
|
|
|
|
356
|
|
|
$isSuccess = true; |
|
357
|
|
|
$this->sortCronTab($agents); |
|
358
|
|
|
|
|
359
|
|
|
$filename = EnvHelper::getCrontabFile(); |
|
360
|
|
|
|
|
361
|
|
|
$fh = fopen($filename, 'c'); |
|
362
|
|
|
if (flock($fh, LOCK_EX)) { |
|
363
|
|
|
ftruncate($fh, 0); |
|
364
|
|
|
if(!fwrite($fh, json_encode($agents, JSON_PRETTY_PRINT))) { |
|
365
|
|
|
throw new \Exception('Unable to write BX_CRONTAB : ' . $filename); |
|
366
|
|
|
} |
|
367
|
|
|
} else { |
|
368
|
|
|
$isSuccess = false; |
|
369
|
|
|
} |
|
370
|
|
|
flock($fh, LOCK_UN); |
|
371
|
|
|
fclose($fh); |
|
372
|
|
|
|
|
373
|
|
|
return $isSuccess; |
|
374
|
|
|
} |
|
375
|
|
|
|
|
376
|
|
|
/** |
|
377
|
|
|
* @return array|false|mixed |
|
378
|
|
|
*/ |
|
379
|
|
|
protected function getCronTab() { |
|
380
|
|
|
|
|
381
|
|
|
$filename = EnvHelper::getCrontabFile(); |
|
382
|
|
|
|
|
383
|
|
|
$fh = fopen($filename, 'r'); |
|
384
|
|
|
if(!$fh) { |
|
|
|
|
|
|
385
|
|
|
return false; |
|
386
|
|
|
} |
|
387
|
|
|
if(flock($fh, LOCK_SH)) { |
|
388
|
|
|
$cronTab = []; |
|
389
|
|
|
$filesize = (int) filesize($filename); |
|
390
|
|
|
if($filesize > 0 && $data = fread($fh, $filesize)) { |
|
391
|
|
|
$decoded = json_decode($data, true); |
|
392
|
|
|
if(is_array($decoded)) { |
|
393
|
|
|
$cronTab = $decoded; |
|
394
|
|
|
} |
|
395
|
|
|
} |
|
396
|
|
|
} else { |
|
397
|
|
|
$cronTab = false; |
|
398
|
|
|
} |
|
399
|
|
|
flock($fh, LOCK_UN); |
|
400
|
|
|
fclose($fh); |
|
401
|
|
|
|
|
402
|
|
|
return $cronTab; |
|
403
|
|
|
} |
|
404
|
|
|
|
|
405
|
|
|
protected function sortCronTab(array &$crontab, $sort = self::SORT_NAME) { |
|
406
|
|
|
|
|
407
|
|
|
if($sort == self::SORT_TIME) { |
|
408
|
|
|
$sorting = []; |
|
409
|
|
|
foreach($crontab as $cmd => $data) { |
|
410
|
|
|
$sorting[$cmd] = $data['last_exec']; |
|
411
|
|
|
} |
|
412
|
|
|
arsort($sorting, SORT_NUMERIC); |
|
413
|
|
|
$sorted = []; |
|
414
|
|
|
foreach($sorting as $cmd => $time) { |
|
415
|
|
|
$sorted[$cmd] = $crontab[$cmd]; |
|
416
|
|
|
} |
|
417
|
|
|
$crontab = $sorted; |
|
418
|
|
|
} else { |
|
419
|
|
|
ksort($crontab, SORT_STRING); |
|
420
|
|
|
} |
|
421
|
|
|
} |
|
422
|
|
|
} |