1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Dtc\QueueBundle\Command; |
4
|
|
|
|
5
|
|
|
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; |
6
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
7
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
8
|
|
|
use Symfony\Component\Console\Input\InputOption; |
9
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
10
|
|
|
|
11
|
|
|
class PruneCommand extends ContainerAwareCommand |
12
|
|
|
{ |
13
|
|
|
const OLDER_MESSAGE = '<int>[d|m|y|h|i|s] Specify how old the jobs should (defaults to timestamp unless a quantifier is specified [d_ays, m_onths, y_years, h_ours, i_minutes, s_econds'; |
14
|
|
|
|
15
|
5 |
|
protected function configure() |
16
|
|
|
{ |
17
|
|
|
$this |
18
|
5 |
|
->setName('dtc:queue:prune') |
19
|
5 |
|
->setDescription('Prune job with error status') |
20
|
5 |
|
->addArgument('type', InputArgument::REQUIRED, '<stalled|stalled_runs|error|expired|old|old_runs|old_job_timings> Prune stalled, erroneous, expired, or old jobs') |
21
|
5 |
|
->addOption('older', null, InputOption::VALUE_REQUIRED, self::OLDER_MESSAGE); |
22
|
5 |
|
} |
23
|
|
|
|
24
|
5 |
|
protected function execute(InputInterface $input, OutputInterface $output) |
25
|
|
|
{ |
26
|
5 |
|
$container = $this->getContainer(); |
27
|
5 |
|
$jobManager = $container->get('dtc_queue.job_manager'); |
28
|
5 |
|
$type = $input->getArgument('type'); |
29
|
|
|
switch ($type) { |
30
|
5 |
|
case 'error': |
31
|
1 |
|
$count = $jobManager->pruneErroneousJobs(); |
32
|
1 |
|
$output->writeln("$count Erroneous Job(s) pruned"); |
33
|
1 |
|
break; |
34
|
4 |
|
case 'expired': |
35
|
1 |
|
$count = $jobManager->pruneExpiredJobs(); |
36
|
1 |
|
$output->writeln("$count Expired Job(s) pruned"); |
37
|
1 |
|
break; |
38
|
3 |
|
case 'stalled': |
39
|
1 |
|
$count = $jobManager->pruneStalledJobs(); |
40
|
1 |
|
$output->writeln("$count Stalled Job(s) pruned"); |
41
|
1 |
|
break; |
42
|
2 |
|
case 'stalled_runs': |
43
|
1 |
|
$count = $container->get('dtc_queue.run_manager')->pruneStalledRuns(); |
44
|
1 |
|
$output->writeln("$count Stalled Job(s) pruned"); |
45
|
1 |
|
break; |
46
|
|
|
default: |
47
|
1 |
|
$older = $input->getOption('older'); |
48
|
1 |
|
if (!$older) { |
49
|
1 |
|
$output->writeln('<error>--older must be specified</error>'); |
50
|
|
|
|
51
|
1 |
|
return 1; |
52
|
|
|
} |
53
|
1 |
|
if (!preg_match("/(\d+)([d|m|y|h|i|s]){0,1}$/", $older, $matches)) { |
54
|
1 |
|
$output->writeln('<error>Wrong format for --older</error>'); |
55
|
|
|
|
56
|
1 |
|
return 1; |
57
|
|
|
} |
58
|
|
|
|
59
|
1 |
|
return $this->pruneOldJobs($matches, $type, $output); |
60
|
|
|
} |
61
|
|
|
|
62
|
4 |
|
return 0; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param string[] $matches |
67
|
|
|
* @param string $type |
68
|
|
|
* @param OutputInterface $output |
69
|
|
|
* |
70
|
|
|
* @return int |
71
|
|
|
* |
72
|
|
|
* @throws \Exception |
73
|
|
|
*/ |
74
|
1 |
|
protected function pruneOldJobs(array $matches, $type, OutputInterface $output) |
75
|
|
|
{ |
76
|
1 |
|
$durationOrTimestamp = intval($matches[1]); |
77
|
1 |
|
$modifier = isset($matches[2]) ? $matches[2] : null; |
78
|
|
|
|
79
|
1 |
|
if (!$durationOrTimestamp) { |
80
|
|
|
$output->writeln('<error>No duration or timestamp passed in.</error>'); |
81
|
|
|
|
82
|
|
|
return 1; |
83
|
|
|
} |
84
|
1 |
|
$olderThan = new \DateTime(); |
85
|
1 |
|
if (null === $modifier) { |
86
|
1 |
|
$olderThan->setTimestamp($durationOrTimestamp); |
87
|
|
|
} else { |
88
|
1 |
|
$interval = $this->getInterval($modifier, $durationOrTimestamp); |
89
|
1 |
|
$olderThan->sub($interval); |
90
|
|
|
} |
91
|
|
|
|
92
|
1 |
|
return $this->pruneOlderThan($type, $olderThan, $output); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @param string $type |
97
|
|
|
* @param \DateTime $olderThan |
98
|
|
|
* @param OutputInterface $output |
99
|
|
|
* |
100
|
|
|
* @return int |
101
|
|
|
* |
102
|
|
|
* @throws \Exception |
103
|
|
|
*/ |
104
|
1 |
|
protected function pruneOlderThan($type, \DateTime $olderThan, OutputInterface $output) |
105
|
|
|
{ |
106
|
1 |
|
$container = $this->getContainer(); |
107
|
|
|
switch ($type) { |
108
|
1 |
|
case 'old': |
109
|
1 |
|
$count = $container->get('dtc_queue.job_manager')->pruneArchivedJobs($olderThan); |
110
|
1 |
|
break; |
111
|
1 |
|
case 'old_runs': |
112
|
1 |
|
$count = $container->get('dtc_queue.run_manager')->pruneArchivedRuns($olderThan); |
113
|
1 |
|
break; |
114
|
1 |
|
case 'old_job_timings': |
115
|
1 |
|
$count = $container->get('dtc_queue.run_manager')->pruneJobTimings($olderThan); |
116
|
1 |
|
break; |
117
|
|
|
default: |
118
|
|
|
throw new \Exception("Unknown type $type"); |
119
|
|
|
} |
120
|
1 |
|
$output->writeln("$count Archived Job(s) pruned"); |
121
|
|
|
|
122
|
1 |
|
return 0; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Returns the date interval based on the modifier and the duration. |
127
|
|
|
* |
128
|
|
|
* @param string $modifier |
129
|
|
|
* @param int $duration |
130
|
|
|
* |
131
|
|
|
* @return \DateInterval |
132
|
|
|
* |
133
|
|
|
* @throws \Exception |
134
|
|
|
*/ |
135
|
1 |
|
protected function getInterval($modifier, $duration) |
136
|
|
|
{ |
137
|
|
|
switch ($modifier) { |
138
|
1 |
|
case 'd': |
139
|
1 |
|
$interval = new \DateInterval("P${duration}D"); |
140
|
1 |
|
break; |
141
|
1 |
|
case 'm': |
142
|
1 |
|
$interval = new \DateInterval("P${duration}M"); |
143
|
|
|
break; |
144
|
|
|
case 'y': |
145
|
|
|
$interval = new \DateInterval("P${duration}Y"); |
146
|
1 |
|
break; |
147
|
1 |
|
case 'h': |
148
|
1 |
|
$interval = new \DateInterval("PT${duration}H"); |
149
|
1 |
|
break; |
150
|
1 |
|
case 'i': |
151
|
1 |
|
$seconds = $duration * 60; |
152
|
1 |
|
$interval = new \DateInterval("PT${seconds}S"); |
153
|
1 |
|
break; |
154
|
1 |
|
case 's': |
155
|
1 |
|
$interval = new \DateInterval("PT${duration}S"); |
156
|
1 |
|
break; |
157
|
|
|
default: |
158
|
|
|
throw new \Exception("Unknown duration modifier: $modifier"); |
159
|
|
|
} |
160
|
|
|
|
161
|
1 |
|
return $interval; |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
|