StartScheduledWorkerCommand::execute()   F
last analyzed

Complexity

Conditions 16
Paths 1153

Size

Total Lines 89
Code Lines 53

Duplication

Lines 19
Ratio 21.35 %

Importance

Changes 5
Bugs 2 Features 0
Metric Value
c 5
b 2
f 0
dl 19
loc 89
rs 2
cc 16
eloc 53
nc 1153
nop 2

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Mpclarkson\ResqueBundle\Command;
4
5
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
6
use Symfony\Component\Console\Input\InputInterface;
7
use Symfony\Component\Console\Input\InputOption;
8
use Symfony\Component\Console\Output\OutputInterface;
9
use Symfony\Component\Process\Process;
10
11
/**
12
 * Class StartScheduledWorkerCommand
13
 * @package Mpclarkson\ResqueBundle\Command
14
 */
15
class StartScheduledWorkerCommand extends ContainerAwareCommand
16
{
17
    /**
18
     *
19
     */
20
    protected function configure()
21
    {
22
        $this
23
            ->setName('resque:scheduledworker-start')
24
            ->setDescription('Start a scheduled resque worker')
25
            ->addOption('foreground', 'f', InputOption::VALUE_NONE, 'Should the worker run in foreground')
26
            ->addOption('force', NULL, InputOption::VALUE_NONE, 'Force creation of a new worker if the PID file exists')
27
            ->addOption('interval', 'i', InputOption::VALUE_REQUIRED, 'How often to check for new jobs across the queues', 5);
28
    }
29
30
    /**
31
     * @param InputInterface $input
32
     * @param OutputInterface $output
33
     * @return int|null|void
34
     * @throws \Exception
35
     */
36
    protected function execute(InputInterface $input, OutputInterface $output)
37
    {
38
        $pidFile = $this->getContainer()->get('kernel')->getCacheDir() . '/resque_scheduledworker.pid';
39
        if (file_exists($pidFile) && !$input->getOption('force')) {
40
            throw new \Exception('PID file exists - use --force to override');
41
        }
42
43
        if (file_exists($pidFile)) {
44
            unlink($pidFile);
45
        }
46
47
        $env = [
48
            'APP_INCLUDE' => $this->getContainer()->getParameter('resque.app_include'),
49
            'VVERBOSE'    => 1,
50
            'RESQUE_PHP'  => $this->getContainer()->getParameter('resque.vendor_dir') . '/chrisboulton/php-resque/lib/Resque.php',
51
            'INTERVAL'    => $input->getOption('interval'),
52
        ];
53
54
        if (FALSE !== getenv('APP_INCLUDE')) {
55
            $env['APP_INCLUDE'] = getenv('APP_INCLUDE');
56
        }
57
58
        $prefix = $this->getContainer()->getParameter('resque.prefix');
59
60
        if (!empty($prefix)) {
61
            $env['PREFIX'] = $this->getContainer()->getParameter('resque.prefix');
62
        }
63
64
        $redisHost = $this->getContainer()->getParameter('resque.redis.host');
65
        $redisPort = $this->getContainer()->getParameter('resque.redis.port');
66
        $redisDatabase = $this->getContainer()->getParameter('resque.redis.database');
67
68 View Code Duplication
        if ($redisHost != NULL && $redisPort != NULL) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
69
            $env['REDIS_BACKEND'] = $redisHost . ':' . $redisPort;
70
        }
71
72
        if (isset($redisDatabase)) {
73
            $env['REDIS_BACKEND_DB'] = $redisDatabase;
74
        }
75
76 View Code Duplication
        if (version_compare(PHP_VERSION, '5.4.0') >= 0) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
77
            $phpExecutable = PHP_BINARY;
78
        } else {
79
            $phpExecutable = PHP_BINDIR . '/php';
80
            if (defined('PHP_WINDOWS_VERSION_BUILD')) {
81
                $phpExecutable = 'php';
82
            }
83
        }
84
85
        $workerCommand = $phpExecutable . ' ' . __DIR__ . '/../bin/resque-scheduler';
86
87
        if (!$input->getOption('foreground')) {
88
            $logFile = $this->getContainer()->getParameter(
89
                    'kernel.logs_dir'
90
                ) . '/resque-scheduler_' . $this->getContainer()->getParameter('kernel.environment') . '.log';
91
            $workerCommand = 'nohup ' . $workerCommand . ' > ' . $logFile . ' 2>&1 & echo $!';
92
        }
93
94
        // In windows: When you pass an environment to CMD it replaces the old environment
95
        // That means we create a lot of problems with respect to user accounts and missing vars
96
        // this is a workaround where we add the vars to the existing environment.
97 View Code Duplication
        if (defined('PHP_WINDOWS_VERSION_BUILD')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
98
            foreach ($env as $key => $value) {
99
                putenv($key . "=" . $value);
100
            }
101
            $env = NULL;
102
        }
103
104
        $process = new Process($workerCommand, NULL, $env, NULL, NULL);
105
106
        $output->writeln(\sprintf('Starting worker <info>%s</info>', $process->getCommandLine()));
107
108
        if ($input->getOption('foreground')) {
109
            $process->run(function ($type, $buffer) use ($output) {
110
                $output->write($buffer);
111
            });
112
        } // else we recompose and display the worker id
113
        else {
114
            $process->run();
115
            $pid = \trim($process->getOutput());
116
            if (function_exists('gethostname')) {
117
                $hostname = gethostname();
118
            } else {
119
                $hostname = php_uname('n');
120
            }
121
            $output->writeln(\sprintf('<info>Worker started</info> %s:%s', $hostname, $pid));
122
            file_put_contents($pidFile, $pid);
123
        }
124
    }
125
}
126