Completed
Push — master ( 692985...e555b8 )
by Matthew
18:05
created

PruneCommand::setJobManager()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 2
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Dtc\QueueBundle\Command;
4
5
use Dtc\QueueBundle\Exception\UnsupportedException;
6
use Dtc\QueueBundle\Manager\JobManagerInterface;
7
use Dtc\QueueBundle\Manager\JobTimingManager;
8
use Dtc\QueueBundle\Manager\RunManager;
9
use Dtc\QueueBundle\Util\Util;
10
use Symfony\Component\Console\Command\Command;
11
use Symfony\Component\Console\Input\InputArgument;
12
use Symfony\Component\Console\Input\InputInterface;
13
use Symfony\Component\Console\Input\InputOption;
14
use Symfony\Component\Console\Output\OutputInterface;
15
16
class PruneCommand extends Command
17 5
{
18
    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';
19
20 5
    /** @var JobManagerInterface */
21 5
    private $jobManager;
22 5
23 5
    /** @var RunManager */
24 5
    private $runManager;
25
26 5
    /** @var JobTimingManager */
27
    private $jobTimingManager;
28 5
29 5
    protected function configure()
30 5
    {
31
        $this
32
        ->setName('dtc:queue:prune')
33
        ->setDescription('Prune jobs')
34 5
        ->addArgument('type', InputArgument::REQUIRED, '<stalled|stalled_runs|exception|expired|old|old_runs|old_job_timings> Prune stalled, exception, expired, or old jobs')
35 1
            ->addOption('older', null, InputOption::VALUE_REQUIRED, self::OLDER_MESSAGE);
36 1
    }
37 4
38 1
    public function setJobManager($jobManager) {
39 1
        $this->jobManager = $jobManager;
40 1
    }
41 1
42 1
    public function setRunManager($runManager) {
43
        $this->runManager = $runManager;
44 3
    }
45
46
    public function setJobTimingManager($jobTimingManager) {
47 2
        $this->jobTimingManager = $jobTimingManager;
48
    }
49
50 1
    protected function execute(InputInterface $input, OutputInterface $output)
51
    {
52 1
        $type = $input->getArgument('type');
53 1
        switch ($type) {
54 1
            case 'erroneous':
55 1
                $output->writeln("(Warning): 'erroneous' is deprecated, please use 'exception' instead");
56 1
                $this->pruneExceptionJobs($output);
57
                break;
58 3
            case 'exception':
59
                $this->pruneExceptionJobs($output);
60 3
                break;
61 3
            case 'expired':
62 3
                $count = $this->jobManager->pruneExpiredJobs();
63 3
                $output->writeln("$count Expired Job(s) pruned");
64 3
                break;
65 1
            default:
66 1
                return $this->executeStalledOther($input, $output);
67 1
        }
68 2
69 1
        return 0;
70 1
    }
71 1
72
    protected function pruneExceptionJobs(OutputInterface $output)
73 1
    {
74
        // @TODO: move this to dependency injection.
75
        $count = $this->jobManager->pruneExceptionJobs();
76 2
        $output->writeln("$count Job(s) with status 'exception' pruned");
77
    }
78
79 1
    public function executeStalledOther(InputInterface $input, OutputInterface $output)
80
    {
81 1
        $type = $input->getArgument('type');
82 1
        switch ($type) {
83 1
            case 'stalled':
84 1
                $count = $this->jobManager->pruneStalledJobs();
0 ignored issues
show
Bug introduced by
The method pruneStalledJobs() does not exist on Dtc\QueueBundle\Manager\JobManagerInterface. It seems like you code against a sub-type of Dtc\QueueBundle\Manager\JobManagerInterface such as Dtc\QueueBundle\Manager\StallableJobManager. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

84
                /** @scrutinizer ignore-call */ 
85
                $count = $this->jobManager->pruneStalledJobs();
Loading history...
85
                $output->writeln("$count Stalled Job(s) pruned");
86 1
                break;
87
            case 'stalled_runs':
88 1
                $count = $this->runManager->pruneStalledRuns();
89 1
                $output->writeln("$count Stalled Job(s) pruned");
90
                break;
91 1
            default:
92
                return $this->executeOlder($input, $output);
93
        }
94 1
95
        return 0;
96
    }
97
98
    public function executeOlder(InputInterface $input, OutputInterface $output)
99
    {
100
        $older = $input->getOption('older');
101
        $type = $input->getArgument('type');
102
        if (!$older) {
103
            $output->writeln('<error>--older must be specified</error>');
104
105
            return 1;
106 1
        }
107
        if (!preg_match("/(\d+)([d|m|y|h|i|s]){0,1}$/", $older, $matches)) {
108 1
            $output->writeln('<error>Wrong format for --older</error>');
109 1
110
            return 1;
111 1
        }
112
113
        return $this->pruneOldJobs($matches, $type, $output);
0 ignored issues
show
Bug introduced by
It seems like $type can also be of type string[]; however, parameter $type of Dtc\QueueBundle\Command\...Command::pruneOldJobs() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

113
        return $this->pruneOldJobs($matches, /** @scrutinizer ignore-type */ $type, $output);
Loading history...
114
    }
115
116 1
    /**
117 1
     * @param string[]        $matches
118 1
     * @param string          $type
119
     * @param OutputInterface $output
120 1
     *
121 1
     * @return int
122
     *
123
     * @throws \Exception
124 1
     */
125
    protected function pruneOldJobs(array $matches, $type, OutputInterface $output)
126
    {
127
        $durationOrTimestamp = intval($matches[1]);
128
        $modifier = isset($matches[2]) ? $matches[2] : null;
129
130
        if (!$durationOrTimestamp) {
131
            $output->writeln('<error>No duration or timestamp passed in.</error>');
132
133
            return 1;
134
        }
135
        $olderThan = Util::getMicrotimeDateTime();
136 1
        if (null === $modifier) {
137
            $olderThan->setTimestamp($durationOrTimestamp);
138 1
        } else {
139 1
            $interval = $this->getInterval($modifier, $durationOrTimestamp);
140 1
            $olderThan->sub($interval);
141 1
        }
142 1
143 1
        return $this->pruneOlderThan($type, $olderThan, $output);
144 1
    }
145 1
146 1
    /**
147 1
     * @param string          $type
148 1
     * @param \DateTime       $olderThan
149 1
     * @param OutputInterface $output
150 1
     *
151 1
     * @return int
152 1
     *
153
     * @throws UnsupportedException
154
     */
155
    protected function pruneOlderThan($type, \DateTime $olderThan, OutputInterface $output)
156 1
    {
157
        $typeName = null;
158 1
        switch ($type) {
159
            case 'old':
160
                $count = $this->jobManager->pruneArchivedJobs($olderThan);
161
                $typeName = 'Job';
162
                break;
163
            case 'old_runs':
164
                $count = $this->runManager->pruneArchivedRuns($olderThan);
165
                $typeName = 'Run';
166
                break;
167
            case 'old_job_timings':
168
                $count = $this->jobTimingManager->pruneJobTimings($olderThan);
169
                $typeName = 'Job Timing';
170
                break;
171 1
            default:
172
                throw new UnsupportedException("Unknown type $type");
173 1
        }
174 1
        $output->writeln("$count Archived $typeName(s) pruned");
175 1
176 1
        return 0;
177 1
    }
178 1
179
    /**
180
     * Returns the date interval based on the modifier and the duration.
181
     *
182 1
     * @param string $modifier
183
     * @param int    $duration
184 1
     *
185
     * @return \DateInterval
186
     *
187 1
     * @throws UnsupportedException
188
     */
189
    protected function getInterval($modifier, $duration)
190
    {
191
        switch ($modifier) {
192
            case 'd':
193
                $interval = new \DateInterval("P${duration}D");
194
                break;
195
            case 'm':
196
                $interval = new \DateInterval("P${duration}M");
197
                break;
198
            case 'y':
199
                $interval = new \DateInterval("P${duration}Y");
200 1
                break;
201 1
            default:
202 1
                $interval = $this->getIntervalTime($modifier, $duration);
203 1
        }
204 1
205 1
        return $interval;
206 1
    }
207 1
208 1
    /**
209 1
     * @param string $modifier
210 1
     * @param int    $duration
211
     *
212
     * @return \DateInterval
213
     *
214
     * @throws UnsupportedException
215 1
     */
216
    protected function getIntervalTime($modifier, $duration)
217
    {
218
        switch ($modifier) {
219
            case 'h':
220
                $interval = new \DateInterval("PT${duration}H");
221
                break;
222
            case 'i':
223
                $seconds = $duration * 60;
224
                $interval = new \DateInterval("PT${seconds}S");
225
                break;
226
            case 's':
227
                $interval = new \DateInterval("PT${duration}S");
228
                break;
229
            default:
230
                throw new UnsupportedException("Unknown duration modifier: $modifier");
231
        }
232
233
        return $interval;
234
    }
235
}
236