Completed
Push — master ( 0b0f4b...0669e2 )
by Matthew
03:15
created

PruneCommand::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

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