1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @package: chapi |
4
|
|
|
* |
5
|
|
|
* @author: msiebeneicher |
6
|
|
|
* @since: 2015-07-28 |
7
|
|
|
* |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Chapi\Commands; |
11
|
|
|
|
12
|
|
|
use Chapi\Entity\Chronos\ChronosJobEntity; |
13
|
|
|
use Chapi\Entity\JobEntityInterface; |
14
|
|
|
use Chapi\Entity\Marathon\MarathonAppEntity; |
15
|
|
|
use Chapi\Service\JobRepository\JobRepositoryInterface; |
16
|
|
|
use Symfony\Component\Console\Helper\Table; |
17
|
|
|
use Symfony\Component\Console\Input\InputOption; |
18
|
|
|
|
19
|
|
|
class ListCommand extends AbstractCommand |
20
|
|
|
{ |
21
|
|
|
const DEFAULT_VALUE_JOB_NAME = 'all'; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Configures the current command. |
25
|
|
|
*/ |
26
|
3 |
|
protected function configure() |
27
|
|
|
{ |
28
|
3 |
|
$this->setName('list') |
29
|
3 |
|
->setDescription('Display your jobs and filter them by status') |
30
|
3 |
|
->addOption('onlyFailed', 'f', InputOption::VALUE_NONE, 'Display only failed jobs') |
31
|
3 |
|
->addOption('onlyDisabled', 'd', InputOption::VALUE_NONE, 'Display only disabled jobs') |
32
|
|
|
; |
33
|
3 |
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @return int |
37
|
|
|
* @throws \LogicException |
38
|
|
|
*/ |
39
|
3 |
|
protected function process() |
40
|
|
|
{ |
41
|
|
|
/** @var JobRepositoryInterface $jobRepositoryChronos */ |
42
|
3 |
|
$jobRepositoryChronos = $this->getContainer()->get(JobRepositoryInterface::DIC_NAME_CHRONOS); |
43
|
|
|
/** @var JobRepositoryInterface $jobRepositoryMarathon */ |
44
|
3 |
|
$jobRepositoryMarathon = $this->getContainer()->get(JobRepositoryInterface::DIC_NAME_MARATHON); |
45
|
|
|
|
46
|
3 |
|
$onlyPrintFailed = (bool) $this->input->getOption('onlyFailed'); |
47
|
3 |
|
$onlyPrintDisabled = (bool) $this->input->getOption('onlyDisabled'); |
48
|
|
|
|
49
|
3 |
|
$table = new Table($this->output); |
50
|
3 |
|
$table->setHeaders(array( |
51
|
3 |
|
'Job', |
52
|
|
|
'Info', |
53
|
|
|
'Type' |
54
|
|
|
)); |
55
|
|
|
|
56
|
3 |
|
$allEntities = array_merge( |
57
|
3 |
|
$jobRepositoryChronos->getJobs()->getArrayCopy(), |
58
|
3 |
|
$jobRepositoryMarathon->getJobs()->getArrayCopy() |
59
|
|
|
); |
60
|
|
|
|
61
|
|
|
/** @var ChronosJobEntity $jobEntity */ |
62
|
3 |
|
foreach ($allEntities as $jobEntity) { |
63
|
3 |
|
if ($this->hasJobToPrint($jobEntity, $onlyPrintFailed, $onlyPrintDisabled)) { |
64
|
3 |
|
$this->printJobTableRow($table, $jobEntity); |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
3 |
|
$table->render(); |
69
|
|
|
|
70
|
3 |
|
return 0; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param JobEntityInterface $jobEntity |
75
|
|
|
* @param bool $onlyPrintFailed |
76
|
|
|
* @param bool $onlyPrintDisabled |
77
|
|
|
* @return bool |
78
|
|
|
*/ |
79
|
3 |
|
private function hasJobToPrint(JobEntityInterface $jobEntity, $onlyPrintFailed, $onlyPrintDisabled) |
80
|
|
|
{ |
81
|
3 |
|
if ($jobEntity->getEntityType() == JobEntityInterface::MARATHON_TYPE) { |
82
|
3 |
|
return true; |
83
|
|
|
} |
84
|
|
|
|
85
|
3 |
|
if (!$jobEntity instanceof ChronosJobEntity) { |
86
|
|
|
throw new \RuntimeException('Entity not of type ChronosJobEntity'); |
87
|
|
|
} |
88
|
|
|
|
89
|
3 |
|
$printAllJobs = (false === $onlyPrintFailed && false === $onlyPrintDisabled); |
90
|
3 |
|
if ($printAllJobs) { |
91
|
1 |
|
return true; |
92
|
|
|
} |
93
|
|
|
|
94
|
2 |
|
$hasToPrint = false; |
95
|
|
|
|
96
|
2 |
|
if (true === $onlyPrintFailed && $jobEntity->errorsSinceLastSuccess > 0) { |
97
|
1 |
|
$hasToPrint = true; |
98
|
|
|
} |
99
|
|
|
|
100
|
2 |
|
if (true === $onlyPrintDisabled && true === $jobEntity->disabled) { |
101
|
1 |
|
$hasToPrint = true; |
102
|
|
|
} |
103
|
|
|
|
104
|
2 |
|
return $hasToPrint; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @param Table $table |
109
|
|
|
* @param JobEntityInterface $jobEntity |
110
|
|
|
*/ |
111
|
3 |
|
private function printJobTableRow(Table $table, JobEntityInterface $jobEntity) |
112
|
|
|
{ |
113
|
3 |
|
$table->addRow([ |
114
|
3 |
|
sprintf( |
115
|
3 |
|
$this->getOutputFormat($jobEntity), |
116
|
3 |
|
$jobEntity->getKey() |
117
|
|
|
), |
118
|
|
|
|
119
|
3 |
|
sprintf( |
120
|
3 |
|
$this->getOutputFormat($jobEntity), |
121
|
3 |
|
$this->getOutputLabel($jobEntity) |
122
|
|
|
), |
123
|
3 |
|
sprintf( |
124
|
3 |
|
$this->getOutputFormat($jobEntity), |
125
|
3 |
|
$jobEntity->getEntityType() |
126
|
|
|
) |
127
|
|
|
]); |
128
|
3 |
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @param JobEntityInterface $jobEntity |
132
|
|
|
* @return string |
133
|
|
|
*/ |
134
|
3 |
|
private function getOutputLabel(JobEntityInterface $jobEntity) |
135
|
|
|
{ |
136
|
|
|
|
137
|
3 |
|
if ($jobEntity->getEntityType() == JobEntityInterface::MARATHON_TYPE) { |
138
|
3 |
|
return 'ok'; |
139
|
|
|
} |
140
|
|
|
|
141
|
3 |
|
if (!$jobEntity instanceof ChronosJobEntity) { |
142
|
|
|
throw new \RuntimeException('Entity not of type ChronosJobEntity'); |
143
|
|
|
} |
144
|
|
|
|
145
|
3 |
|
$jobInfoText = []; |
146
|
|
|
|
147
|
3 |
|
if ($jobEntity->disabled) { |
148
|
1 |
|
$jobInfoText[] = 'disabled'; |
149
|
|
|
} |
150
|
|
|
|
151
|
3 |
|
if ($jobEntity->errorCount > 0) { |
152
|
1 |
|
$errorRate = ($jobEntity->successCount > 0) |
153
|
1 |
|
? 100 / $jobEntity->successCount * $jobEntity->errorCount |
154
|
1 |
|
: 100; |
155
|
|
|
|
156
|
1 |
|
$jobInfoText[] = 'errors rate: ' . round($errorRate, 2) . '%'; |
157
|
|
|
} |
158
|
|
|
|
159
|
3 |
|
if ($jobEntity->errorsSinceLastSuccess > 0) { |
160
|
1 |
|
$jobInfoText[] = 'errors since last success:' . $jobEntity->errorsSinceLastSuccess; |
161
|
|
|
} |
162
|
|
|
|
163
|
3 |
|
return (!empty($jobInfoText)) |
164
|
2 |
|
? implode(' | ', $jobInfoText) |
165
|
3 |
|
: 'ok'; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* @param JobEntityInterface $jobEntity |
170
|
|
|
* @return string |
171
|
|
|
*/ |
172
|
3 |
|
private function getOutputFormat(JobEntityInterface $jobEntity) |
173
|
|
|
{ |
174
|
3 |
|
if ($jobEntity->getEntityType() == JobEntityInterface::MARATHON_TYPE) { |
175
|
3 |
|
return '<info>%s</info>'; |
176
|
|
|
} |
177
|
|
|
|
178
|
3 |
|
if (!$jobEntity instanceof ChronosJobEntity) { |
179
|
|
|
throw new \RuntimeException('Entity not of type ChronosJobEntity'); |
180
|
|
|
} |
181
|
|
|
|
182
|
3 |
|
if ($jobEntity->errorsSinceLastSuccess > 0) { |
183
|
1 |
|
return '<fg=red>%s</>'; |
184
|
|
|
} |
185
|
|
|
|
186
|
2 |
|
if ($jobEntity->errorCount > 0 || true === $jobEntity->disabled) { |
187
|
1 |
|
return '<comment>%s</comment>'; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
// else |
191
|
1 |
|
return '<info>%s</info>'; |
192
|
|
|
} |
193
|
|
|
} |
194
|
|
|
|