Completed
Push — master ( ad518d...0b0f4b )
by Matthew
05:51
created

PruneCommand::pruneOldJobs()   C

Complexity

Conditions 7
Paths 18

Size

Total Lines 35
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 21
CRAP Score 7.0957

Importance

Changes 0
Metric Value
dl 0
loc 35
ccs 21
cts 24
cp 0.875
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 27
nc 18
nop 3
crap 7.0957
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 OutputInterface $output
64
     *
65
     * @return int
66
     *
67
     * @throws \Exception
68
     */
69 1
    protected function pruneOldJobs(array $matches, $type, OutputInterface $output)
70
    {
71 1
        $durationOrTimestamp = intval($matches[1]);
72 1
        $modifier = isset($matches[2]) ? $matches[2] : null;
73
74 1
        if (!$durationOrTimestamp) {
75
            $output->writeln('<error>No duration or timestamp passed in.</error>');
76
77
            return 1;
78
        }
79 1
        $olderThan = new \DateTime();
80 1
        if (null === $modifier) {
81 1
            $olderThan->setTimestamp($durationOrTimestamp);
82
        } else {
83 1
            $interval = $this->getInterval($modifier, $durationOrTimestamp);
84 1
            $olderThan->sub($interval);
85
        }
86 1
        $container = $this->getContainer();
87
        switch ($type) {
88 1
            case 'old':
89 1
                $count = $container->get('dtc_queue.job_manager')->pruneArchivedJobs($olderThan);
90 1
                break;
91 1
            case 'old_runs':
92 1
                $count = $container->get('dtc_queue.run_manager')->pruneArchivedRuns($olderThan);
93 1
                break;
94 1
            case 'old_job_timings':
95 1
                $count = $container->get('dtc_queue.run_manager')->pruneJobTimings($olderThan);
96 1
                break;
97
            default:
98
                throw new \Exception("Unknown type $type");
99
        }
100 1
        $output->writeln("$count Archived Job(s) pruned");
101
102 1
        return 0;
103
    }
104
105
    /**
106
     * Returns the date interval based on the modifier and the duration.
107
     *
108
     * @param string $modifier
109
     * @param int    $duration
110
     *
111
     * @return \DateInterval
112
     *
113
     * @throws \Exception
114
     */
115 1
    protected function getInterval($modifier, $duration)
116
    {
117
        switch ($modifier) {
118 1
            case 'd':
119 1
                $interval = new \DateInterval("P${duration}D");
120 1
                break;
121 1
            case 'm':
122 1
                $interval = new \DateInterval("P${duration}M");
123
                break;
124
            case 'y':
125
                $interval = new \DateInterval("P${duration}Y");
126 1
                break;
127 1
            case 'h':
128 1
                $interval = new \DateInterval("PT${duration}H");
129 1
                break;
130 1
            case 'i':
131 1
                $seconds = $duration * 60;
132 1
                $interval = new \DateInterval("PT${seconds}S");
133 1
                break;
134 1
            case 's':
135 1
                $interval = new \DateInterval("PT${duration}S");
136 1
                break;
137
            default:
138
                throw new \Exception("Unknown duration modifier: $modifier");
139
        }
140
141 1
        return $interval;
142
    }
143
}
144