StopWorkerCommand   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 8 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
/**
12
 * Class StopWorkerCommand
13
 * @package Mpclarkson\ResqueBundle\Command
14
 */
15
class StopWorkerCommand extends ContainerAwareCommand
16
{
17
    /**
18
     *
19
     */
20
    protected function configure()
21
    {
22
        $this
23
            ->setName('resque:worker-stop')
24
            ->setDescription('Stop a resque worker')
25
            ->addArgument('id', InputArgument::OPTIONAL, 'Worker id')
26
            ->addOption('all', 'a', InputOption::VALUE_NONE, 'Should kill all workers');
27
    }
28
29
    /**
30
     * @param InputInterface $input
31
     * @param OutputInterface $output
32
     * @return int
33
     */
34
    protected function execute(InputInterface $input, OutputInterface $output)
35
    {
36
        $resque = $this->getContainer()->get('resque');
37
38
        if ($input->getOption('all')) {
39
            $workers = $resque->getWorkers();
40
        } else {
41
            $worker = $resque->getWorker($input->getArgument('id'));
42
43
            if (!$worker) {
44
                $availableWorkers = $resque->getWorkers();
45
                if (!empty($availableWorkers)) {
46
                    $output->writeln('<error>You need to give an existing worker.</error>');
47
                    $output->writeln('Running workers are:');
48
                    foreach ($resque->getWorkers() as $worker) {
49
                        $output->writeln($worker->getId());
50
                    }
51
                } else {
52
                    $output->writeln('<error>There are no running workers.</error>');
53
                }
54
55
                return 1;
56
            }
57
58
            $workers = [$worker];
59
        }
60
61
        if (!count($workers)) {
62
            $output->writeln('<error>There are no running workers to stop.</error>');
63
64
            return 0;
65
        }
66
        
67
        foreach ($workers as $worker) {
68
            $output->writeln(\sprintf('Stopping %s...', $worker->getId()));
69
            $worker->stop();
70
            $worker->getWorker()->unregisterWorker();
71
        }
72
73
        return 0;
74
    }
75
}
76