Completed
Push — master ( d4f4f0...01653d )
by Matthew
04:03
created

PruneCommand::pruneOlderThan()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 20
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 4.0092

Importance

Changes 0
Metric Value
dl 0
loc 20
ccs 11
cts 12
cp 0.9167
rs 9.2
c 0
b 0
f 0
cc 4
eloc 16
nc 4
nop 3
crap 4.0092
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|stalled_runs|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
            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
                if (!$older) {
49 1
                    $output->writeln('<error>--older must be specified</error>');
50 1
51
                    return 1;
52 1
                }
53
                if (!preg_match("/(\d+)([d|m|y|h|i|s]){0,1}$/", $older, $matches)) {
54
                    $output->writeln('<error>Wrong format for --older</error>');
55 1
56
                    return 1;
57
                }
58 3
59
                return $this->pruneOldJobs($matches, $type, $output);
60
        }
61
62
        return 0;
63
    }
64
65
    /**
66
     * @param string[]        $matches
67
     * @param string          $type
68
     * @param OutputInterface $output
69
     *
70 1
     * @return int
71
     *
72 1
     * @throws \Exception
73 1
     */
74
    protected function pruneOldJobs(array $matches, $type, OutputInterface $output)
75 1
    {
76
        $durationOrTimestamp = intval($matches[1]);
77
        $modifier = isset($matches[2]) ? $matches[2] : null;
78
79
        if (!$durationOrTimestamp) {
80 1
            $output->writeln('<error>No duration or timestamp passed in.</error>');
81 1
82 1
            return 1;
83
        }
84 1
        $olderThan = new \DateTime();
85 1
        if (null === $modifier) {
86
            $olderThan->setTimestamp($durationOrTimestamp);
87
        } else {
88 1
            $interval = $this->getInterval($modifier, $durationOrTimestamp);
89
            $olderThan->sub($interval);
90
        }
91
92
        return $this->pruneOlderThan($type, $olderThan, $output);
93
    }
94
95
    /**
96
     * @param string          $type
97
     * @param \DateTime       $olderThan
98
     * @param OutputInterface $output
99
     *
100 1
     * @return int
101
     *
102 1
     * @throws \Exception
103
     */
104 1
    protected function pruneOlderThan($type, \DateTime $olderThan, OutputInterface $output)
105 1
    {
106 1
        $container = $this->getContainer();
107 1
        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
                break;
114
            case 'old_job_timings':
115
                $count = $container->get('dtc_queue.run_manager')->pruneJobTimings($olderThan);
116 1
                break;
117
            default:
118 1
                throw new \Exception("Unknown type $type");
119
        }
120
        $output->writeln("$count Archived Job(s) pruned");
121
122
        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 1
     * @return \DateInterval
132
     *
133
     * @throws \Exception
134 1
     */
135 1
    protected function getInterval($modifier, $duration)
136 1
    {
137 1
        switch ($modifier) {
138 1
            case 'd':
139
                $interval = new \DateInterval("P${duration}D");
140
                break;
141
            case 'm':
142 1
                $interval = new \DateInterval("P${duration}M");
143 1
                break;
144 1
            case 'y':
145 1
                $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
                break;
154
            case 's':
155
                $interval = new \DateInterval("PT${duration}S");
156
                break;
157 1
            default:
158
                throw new \Exception("Unknown duration modifier: $modifier");
159
        }
160
161
        return $interval;
162
    }
163
}
164