CommandService::runDue()   B
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 43
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 28
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 43
ccs 28
cts 28
cp 1
rs 8.439
c 0
b 0
f 0
cc 5
eloc 25
nc 5
nop 1
crap 5
1
<?php namespace Indatus\Dispatcher\Services;
2
3
/**
4
 * This file is part of Dispatcher
5
 *
6
 * (c) Ben Kuhl <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
use App;
13
use Illuminate\Console\Command;
14
use Indatus\Dispatcher\Debugger;
15
use Indatus\Dispatcher\Scheduling\ScheduledCommandInterface;
16
17
class CommandService
18
{
19
    /**
20
     * @var \Indatus\Dispatcher\Services\ScheduleService
21
     */
22
    private $scheduleService;
23
24 21
    public function __construct(ScheduleService $scheduleService)
25
    {
26 21
        $this->scheduleService = $scheduleService;
27 21
    }
28
29
    /**
30
     * Run all commands that are due to be run
31
     */
32 4
    public function runDue(Debugger $debugger)
33
    {
34 4
        $debugger->log('Running commands...');
35
36
        /** @var \Indatus\Dispatcher\BackgroundProcessRunner $backgroundProcessRunner */
37 4
        $backgroundProcessRunner = App::make('Indatus\Dispatcher\BackgroundProcessRunner');
38
39
        /** @var \Indatus\Dispatcher\Queue $queue */
40 4
        $queue = $this->scheduleService->getQueue($debugger);
41
42 4
        foreach ($queue->flush() as $queueItem) {
43
            /** @var \Indatus\Dispatcher\Scheduling\ScheduledCommandInterface $command */
44 4
            $command = $queueItem->getCommand();
45
46
            //determine if the command is enabled
47 4
            if ($command->isEnabled()) {
48 3
                if ($this->runnableInCurrentMaintenanceSetting($command)) {
49 2
                    if ($this->runnableInEnvironment($command)) {
50 1
                        $scheduler = $queueItem->getScheduler();
51
52 1
                        $backgroundProcessRunner->run(
53 1
                            $command,
54 1
                            $scheduler->getArguments(),
55 1
                            $scheduler->getOptions(),
56
                            $debugger
57 1
                        );
58 1
                    } else {
59 1
                        $debugger->commandNotRun(
60 1
                            $command,
61 1
                            'Command is not configured to run in '.App::environment()
62 1
                        );
63
                    }
64 2
                } else {
65 1
                    $debugger->commandNotRun(
66 1
                        $command,
67
                        'Command is not configured to run while application is in maintenance mode'
68 1
                    );
69
                }
70 3
            } else {
71 1
                $debugger->commandNotRun($command, 'Command is disabled');
72
            }
73 4
        }
74 4
    }
75
76
    /**
77
     * Determine if a scheduled command is in the correct environment
78
     *
79
     * @param \Indatus\Dispatcher\Scheduling\ScheduledCommandInterface $command
80
     *
81
     * @return bool
82
     */
83 4
    public function runnableInEnvironment(ScheduledCommandInterface $command)
84
    {
85 4
        $environment = $command->environment();
86
87
        //if any
88 4
        if ($environment == '*' || $environment == App::environment()) {
89 2
            return true;
90
        }
91
92 2
        if (is_array($environment) && in_array(App::environment(), $environment)) {
93 1
            return true;
94
        }
95
96 1
        return false;
97
    }
98
99
    /**
100
     * Determine if application is in maintenance mode and if scheduled command can run
101
     *
102
     * @param \Indatus\Dispatcher\Scheduling\ScheduledCommandInterface $command
103
     *
104
     * @return bool
105
     */
106 5
    public function runnableInCurrentMaintenanceSetting(ScheduledCommandInterface $command)
107
    {
108 5
        if (App::isDownForMaintenance()) {
109 2
            return $command->runInMaintenanceMode();
110
        }
111
112 3
        return true;
113
    }
114
115
    /**
116
     * Prepare a command's arguments for command line usage
117
     *
118
     * @param array $arguments
119
     *
120
     * @return string
121
     */
122 3
    public function prepareArguments(array $arguments)
123
    {
124 3
        return implode(' ', $arguments);
125
    }
126
127
    /**
128
     * Prepare a command's options for command line usage
129
     *
130
     * @param array $options
131
     *
132
     * @return string
133
     */
134 4
    public function prepareOptions(array $options)
135
    {
136 4
        $optionPieces = [];
137 4
        foreach ($options as $opt => $value) {
138
            //if it's an array of options, throw them in there as well
139 3
            if (is_array($value)) {
140 1
                foreach ($value as $optArrayValue) {
141 1
                    $optionPieces[] = '--'.$opt.'="'.addslashes($optArrayValue).'"';
142 1
                }
143 1
            } else {
144 3
                $option = null;
145
146
                //option exists with no value
147 3
                if (is_numeric($opt)) {
148 2
                    $option = $value;
149 3
                } elseif (!empty($value)) {
150 3
                    $option = $opt.'="'.addslashes($value).'"';
151 3
                }
152
153 3
                if (!is_null($option)) {
154 3
                    $optionPieces[] = '--'.$option;
155 3
                }
156
            }
157 4
        }
158
159 4
        return implode(' ', $optionPieces);
160
    }
161
162
    /**
163
     * Get a command to run this application
164
     *
165
     * @param \Indatus\Dispatcher\Scheduling\ScheduledCommandInterface $scheduledCommand
166
     * @param array                                                    $arguments
167
     * @param array                                                    $options
168
     *
169
     * @return string
170
     */
171 7
    public function getRunCommand(
172
        ScheduledCommandInterface $scheduledCommand,
173
        array $arguments = [],
174
        array $options = []
175
    ) {
176
        /** @var \Indatus\Dispatcher\Platform $platform */
177 7
        $platform = App::make('Indatus\Dispatcher\Platform');
178
179 7
        $commandPieces = [];
180 7
        if ($platform->isHHVM()) {
181 1
            $commandPieces[] = '/usr/bin/env hhvm';
182 1
        } else {
183 6
            $commandPieces[] = PHP_BINARY;
184
        }
185
186 7
        $commandPieces[] = base_path().'/artisan';
187 7
        $commandPieces[] = $scheduledCommand->getName();
188
189 7
        if (count($arguments) > 0) {
190 1
            $commandPieces[] = $this->prepareArguments($arguments);
191 1
        }
192
193 7
        if (count($options) > 0) {
194 1
            $commandPieces[] = $this->prepareOptions($options);
195 1
        }
196
197
        //always pass environment
198 7
        $commandPieces[] = '--env='.App::environment();
199
200 7
        if ($platform->isUnix()) {
201 6
            $commandPieces[] = '> /dev/null'; //don't show output, errors can be viewed in the Laravel log
202 6
            $commandPieces[] = '&'; //run in background
203
204
            //run the command as a different user
205 6
            if (is_string($scheduledCommand->user())) {
206 1
                array_unshift($commandPieces, 'sudo -u '.$scheduledCommand->user());
207 1
            }
208 7
        } elseif ($platform->isWindows()) {
209 1
            $commandPieces[] = '> NULL'; //don't show output, errors can be viewed in the Laravel log
210
211
            //run in background on windows
212 1
            array_unshift($commandPieces, '/B');
213 1
            array_unshift($commandPieces, 'START');
214 1
        }
215
216 7
        return implode(' ', $commandPieces);
217
    }
218
}
219