|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Tkotosz\CommandScheduler\Model; |
|
4
|
|
|
|
|
5
|
|
|
use Tkotosz\CommandScheduler\Api\CommandScheduleRepositoryInterface; |
|
6
|
|
|
use Tkotosz\CommandScheduler\Model\CommandProvider; |
|
7
|
|
|
use Psr\Log\LoggerInterface; |
|
8
|
|
|
use Symfony\Component\Console\Command\Command; |
|
9
|
|
|
use Symfony\Component\Console\Input\ArrayInput; |
|
10
|
|
|
use Symfony\Component\Console\Input\StringInput; |
|
11
|
|
|
use Symfony\Component\Console\Output\BufferedOutput; |
|
12
|
|
|
|
|
13
|
|
|
class CommandScheduleProcessor |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @var CommandScheduleRepositoryInterface |
|
17
|
|
|
*/ |
|
18
|
|
|
private $commandScheduleRepository; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var LoggerInterface |
|
22
|
|
|
*/ |
|
23
|
|
|
private $logger; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var CommandProvider |
|
27
|
|
|
*/ |
|
28
|
|
|
private $commandProvider; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @param CommandScheduleRepositoryInterface $commandScheduleRepository |
|
32
|
|
|
* @param LoggerInterface $logger |
|
33
|
|
|
* @param CommandProvider $commandProvider |
|
34
|
|
|
*/ |
|
35
|
|
|
public function __construct( |
|
36
|
|
|
CommandScheduleRepositoryInterface $commandScheduleRepository, |
|
37
|
|
|
LoggerInterface $logger, |
|
38
|
|
|
CommandProvider $commandProvider |
|
39
|
|
|
) { |
|
40
|
|
|
$this->commandScheduleRepository = $commandScheduleRepository; |
|
41
|
|
|
$this->logger = $logger; |
|
42
|
|
|
$this->commandProvider = $commandProvider; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
public function processNext(): string |
|
46
|
|
|
{ |
|
47
|
|
|
$schedule = $this->commandScheduleRepository->getOneByStatus('pending'); |
|
48
|
|
|
|
|
49
|
|
|
if ($schedule === null) { |
|
50
|
|
|
return 'no pending schedule'; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
try { |
|
54
|
|
|
$this->commandScheduleRepository->updateStatus($schedule, 'running'); |
|
55
|
|
|
|
|
56
|
|
|
$command = $this->commandProvider->getByName($schedule->getCommandName()); |
|
|
|
|
|
|
57
|
|
|
|
|
58
|
|
|
if ($command === null) { |
|
59
|
|
|
throw new \Exception(sprintf('Invalid command: "%s"', $schedule->getCommandName())); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
if (!empty($schedule->getCommandParams())) { |
|
63
|
|
|
if ($command->getApplication() !== null) { |
|
64
|
|
|
$input = new StringInput($schedule->getCommandName() . ' ' . $schedule->getCommandParams()); |
|
65
|
|
|
} else { |
|
66
|
|
|
$input = new StringInput($schedule->getCommandParams()); |
|
67
|
|
|
} |
|
68
|
|
|
} else { |
|
69
|
|
|
$input = new ArrayInput([]); |
|
70
|
|
|
} |
|
71
|
|
|
$output = new BufferedOutput(); |
|
72
|
|
|
|
|
73
|
|
|
$command->run($input, $output); |
|
74
|
|
|
$result = $output->fetch(); |
|
75
|
|
|
|
|
76
|
|
|
$this->commandScheduleRepository->updateStatus($schedule, 'done'); |
|
77
|
|
|
$this->commandScheduleRepository->updateResult($schedule, $result); |
|
78
|
|
|
|
|
79
|
|
|
return $result; |
|
80
|
|
|
} catch (\Exception $e) { |
|
81
|
|
|
$this->logger->critical($e); |
|
82
|
|
|
|
|
83
|
|
|
$this->commandScheduleRepository->updateStatus($schedule, 'failed'); |
|
84
|
|
|
$this->commandScheduleRepository->updateResult($schedule, $e->getMessage()); |
|
85
|
|
|
|
|
86
|
|
|
return $e->getMessage(); |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|