Completed
Push — master ( 1a2ba0...915763 )
by Matthew
04:34 queued 02:12
created

StopWorkerCommand::execute()   C

Complexity

Conditions 7
Paths 8

Size

Total Lines 41
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 41
rs 6.7272
cc 7
eloc 25
nc 8
nop 2
1
<?php
2
3
namespace Mpclarkson\ResqueBundle\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 StopWorkerCommand extends ContainerAwareCommand
12
{
13
    protected function configure()
14
    {
15
        $this
16
            ->setName('resque:worker-stop')
17
            ->setDescription('Stop a resque worker')
18
            ->addArgument('id', InputArgument::OPTIONAL, 'Worker id')
19
            ->addOption('all', 'a', InputOption::VALUE_NONE, 'Should kill all workers')
20
        ;
21
    }
22
23
    protected function execute(InputInterface $input, OutputInterface $output)
24
    {
25
        $resque = $this->getContainer()->get('resque');
26
27
        if ($input->getOption('all')) {
28
            $workers = $resque->getWorkers();
29
        } else {
30
            $worker = $resque->getWorker($input->getArgument('id'));
31
32
            if (!$worker) {
33
                $availableWorkers = $resque->getWorkers();
34
                if (!empty($availableWorkers)) {
35
                    $output->writeln('<error>You need to give an existing worker.</error>');
36
                    $output->writeln('Running workers are:');
37
                    foreach ($resque->getWorkers() as $worker) {
38
                        $output->writeln($worker->getId());
39
                    }
40
                } else {
41
                    $output->writeln('<error>There are no running workers.</error>');
42
                }
43
44
                return 1;
45
            }
46
47
            $workers = array($worker);
48
        }
49
50
        if (!count($workers)) {
51
            $output->writeln('<error>There are no running workers to stop.</error>');
52
53
            return 0;
54
        }
55
        
56
        foreach ($workers as $worker) {
57
            $output->writeln(\sprintf('Stopping %s...', $worker->getId()));
58
            $worker->stop();
59
            $worker->getWorker()->unregisterWorker();
60
        }
61
62
        return 0;
63
    }
64
}
65