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
|
|
|
protected function configure() |
16
|
|
|
{ |
17
|
|
|
$this |
18
|
|
|
->setName('dtc:queue:prune') |
19
|
|
|
->setDescription('Prune job with error status') |
20
|
|
|
->addArgument('type', InputArgument::REQUIRED, '<stalled|error|expired|old> Prune stalled, erroneous, expired, or old jobs') |
21
|
|
|
->addOption('older', null, InputOption::VALUE_REQUIRED, self::OLDER_MESSAGE); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
25
|
|
|
{ |
26
|
|
|
$container = $this->getContainer(); |
27
|
|
|
$jobManager = $container->get('dtc_queue.job_manager'); |
28
|
|
|
$type = $input->getArgument('type'); |
29
|
|
|
switch ($type) { |
30
|
|
|
case 'error': |
31
|
|
|
$count = $jobManager->pruneErroneousJobs(); |
32
|
|
|
$output->writeln("$count Erroneous Job(s) pruned"); |
33
|
|
|
break; |
34
|
|
|
case 'expired': |
35
|
|
|
$count = $jobManager->pruneExpiredJobs(); |
36
|
|
|
$output->writeln("$count Expired Job(s) pruned"); |
37
|
|
|
break; |
38
|
|
|
case 'stalled': |
39
|
|
|
$count = $jobManager->pruneStalledJobs(); |
40
|
|
|
$output->writeln("$count Stalled Job(s) pruned"); |
41
|
|
|
break; |
42
|
|
|
case 'old': |
43
|
|
|
$older = $input->getOption('older'); |
44
|
|
|
if (!$older) { |
45
|
|
|
$output->writeln('<error>--older must be specified</error>'); |
46
|
|
|
|
47
|
|
|
return 1; |
48
|
|
|
} |
49
|
|
|
if (!preg_match("/(\d+)([d|m|y|h|i|s]){0,1}/", $older, $matches)) { |
50
|
|
|
$output->writeln('<error>Wrong format for --older</error>'); |
51
|
|
|
|
52
|
|
|
return 1; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
return $this->pruneOldJobs($matches, $output); |
56
|
|
|
break; |
|
|
|
|
57
|
|
|
default: |
58
|
|
|
$output->writeln("<error>Unknown type $type.</error>"); |
59
|
|
|
|
60
|
|
|
return 1; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
return 0; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param array $matches |
68
|
|
|
* @param OutputInterface $output |
69
|
|
|
* |
70
|
|
|
* @return int |
71
|
|
|
* |
72
|
|
|
* @throws \Exception |
73
|
|
|
*/ |
74
|
|
|
protected function pruneOldJobs(array $matches, OutputInterface $output) |
75
|
|
|
{ |
76
|
|
|
$durationOrTimestamp = intval($matches[1]); |
77
|
|
|
$modifier = isset($matches[2]) ? $matches[2] : null; |
78
|
|
|
|
79
|
|
|
if (!$durationOrTimestamp) { |
80
|
|
|
$output->writeln('<error>No duration or timestamp passed in.</error>'); |
81
|
|
|
|
82
|
|
|
return 1; |
83
|
|
|
} |
84
|
|
|
$olderThan = new \DateTime(); |
85
|
|
|
if (!$modifier) { |
86
|
|
|
$olderThan->setTimestamp($durationOrTimestamp); |
87
|
|
|
} else { |
88
|
|
|
switch ($modifier) { |
89
|
|
|
case 'd': |
90
|
|
|
$interval = new \DateInterval("P${durationOrTimestamp}D"); |
91
|
|
|
break; |
92
|
|
|
case 'm': |
93
|
|
|
$interval = new \DateInterval("P${durationOrTimestamp}M"); |
94
|
|
|
break; |
95
|
|
|
case 'y': |
96
|
|
|
$interval = new \DateInterval("P${durationOrTimestamp}Y"); |
97
|
|
|
break; |
98
|
|
|
case 'h': |
99
|
|
|
$interval = new \DateInterval("PT${durationOrTimestamp}H"); |
100
|
|
|
break; |
101
|
|
|
case 'i': |
102
|
|
|
$seconds = $durationOrTimestamp * 60; |
103
|
|
|
$interval = new \DateInterval("PT${seconds}S"); |
104
|
|
|
break; |
105
|
|
|
case 's': |
106
|
|
|
$interval = new \DateInterval("PT${durationOrTimestamp}S"); |
107
|
|
|
break; |
108
|
|
|
default: |
109
|
|
|
throw new \Exception("Unknown duration modifier: $modifier"); |
110
|
|
|
} |
111
|
|
|
$olderThan->sub($interval); |
112
|
|
|
} |
113
|
|
|
$container = $this->getContainer(); |
114
|
|
|
$count = $container->get('dtc_queue.job_manager')->pruneArchivedJobs($olderThan); |
115
|
|
|
$output->writeln("$count Archived Job(s) pruned"); |
116
|
|
|
|
117
|
|
|
return 0; |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
The break statement is not necessary if it is preceded for example by a return statement:
If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.