Completed
Push — master ( 824463...2e93e9 )
by Matthew
05:21
created

PruneCommand   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 125
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 19
lcom 2
cbo 4
dl 0
loc 125
ccs 0
cts 88
cp 0
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 8 1
C execute() 0 40 7
B pruneOldJobs() 0 23 4
C getInterval() 0 28 7
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
            default:
57
                $output->writeln("<error>Unknown type $type.</error>");
58
59
                return 1;
60
        }
61
62
        return 0;
63
    }
64
65
    /**
66
     * @param string[]        $matches
67
     * @param OutputInterface $output
68
     *
69
     * @return int
70
     *
71
     * @throws \Exception
72
     */
73
    protected function pruneOldJobs(array $matches, OutputInterface $output)
74
    {
75
        $durationOrTimestamp = intval($matches[1]);
76
        $modifier = isset($matches[2]) ? $matches[2] : null;
77
78
        if (!$durationOrTimestamp) {
79
            $output->writeln('<error>No duration or timestamp passed in.</error>');
80
81
            return 1;
82
        }
83
        $olderThan = new \DateTime();
84
        if (null === $modifier) {
85
            $olderThan->setTimestamp($durationOrTimestamp);
86
        } else {
87
            $interval = $this->getInterval($modifier, $durationOrTimestamp);
88
            $olderThan->sub($interval);
89
        }
90
        $container = $this->getContainer();
91
        $count = $container->get('dtc_queue.job_manager')->pruneArchivedJobs($olderThan);
92
        $output->writeln("$count Archived Job(s) pruned");
93
94
        return 0;
95
    }
96
97
    /**
98
     * Returns the date interval based on the modifier and the duration.
99
     *
100
     * @param string $modifier
101
     * @param int    $duration
102
     *
103
     * @return \DateInterval
104
     *
105
     * @throws \Exception
106
     */
107
    protected function getInterval($modifier, $duration)
108
    {
109
        switch ($modifier) {
110
            case 'd':
111
                $interval = new \DateInterval("P${$duration}D");
112
                break;
113
            case 'm':
114
                $interval = new \DateInterval("P${$duration}M");
115
                break;
116
            case 'y':
117
                $interval = new \DateInterval("P${$duration}Y");
118
                break;
119
            case 'h':
120
                $interval = new \DateInterval("PT${$duration}H");
121
                break;
122
            case 'i':
123
                $seconds = $duration * 60;
124
                $interval = new \DateInterval("PT${seconds}S");
125
                break;
126
            case 's':
127
                $interval = new \DateInterval("PT${$duration}S");
128
                break;
129
            default:
130
                throw new \Exception("Unknown duration modifier: $modifier");
131
        }
132
133
        return $interval;
134
    }
135
}
136