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

StopWorkerCommand   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Importance

Changes 3
Bugs 1 Features 0
Metric Value
wmc 8
c 3
b 1
f 0
lcom 0
cbo 5
dl 0
loc 54
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 9 1
C execute() 0 41 7
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