Test Failed
Push — main ( 26fcea...6b8274 )
by Dimitri
59s queued 15s
created

Work::execute()   B

Complexity

Conditions 7
Paths 14

Size

Total Lines 35
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 20
c 1
b 0
f 0
dl 0
loc 35
ccs 0
cts 11
cp 0
rs 8.6666
cc 7
nc 14
nop 1
crap 56
1
<?php
2
3
namespace BlitzPHP\Tasks\Commands;
4
5
use BlitzPHP\Utilities\Date;
6
use Symfony\Component\Process\Process;
7
8
class Work extends TaskCommand
9
{
10
    /**
11
     * {@inheritDoc}
12
     */
13
    protected $name = 'tasks:work';
14
15
    /**
16
     * {@inheritDoc}
17
     */
18
    protected $options = [
19
        '--run-output-file' => 'Le fichier vers lequel diriger la sortie de `tasks:run`.',
20
    ];
21
22
    /**
23
     * {@inheritDoc}
24
     */
25
    protected $description = 'Démarre le planificateur de tâche et l\'exécute localement.';
26
27
    /**
28
     * {@inheritDoc}
29
     */
30
    protected $required = [
31
        'symfony/process:^7.2',
32
    ];
33
34
    /** 
35
     * {@inheritDoc}
36
     */
37
    public function execute(array $params)
38
    {
39
        $this->info('Exéctutions des tâches programmées.');
40
41
        [$lastExecutionStartedAt, $executions] = [Date::now()->subMinutes(10), []];
42
43
        $command = sprintf(
44
            '%s %s %s', 
45
            escapeshellarg(PHP_BINARY), 
46
            escapeshellarg(base_path('klinge')), 
47
            'tasks:run'
48
        );
49
50
        if ($this->option('run-output-file')) {
51
            $command .= ' >> ' . escapeshellarg($this->option('run-output-file')) . ' 2>&1';
52
        }
53
54
        while (true) {
55
            usleep(100 * 1000);
56
57
            if (intval(Date::now()->getSecond()) === 0 && ! Date::now()->startOfMinute()->equalTo($lastExecutionStartedAt)) {
58
                $executions[] = $execution = Process::fromShellCommandline($command);
59
60
                $execution->start();
61
62
                $lastExecutionStartedAt = Date::now()->startOfMinute();
63
            }
64
65
            foreach ($executions as $key => $execution) {
66
                $output = $execution->getIncrementalOutput(). $execution->getIncrementalErrorOutput();
67
68
                $this->write(ltrim($output, "\n"))->eol();
69
70
                if (! $execution->isRunning()) {
71
                    unset($executions[$key]);
72
                }
73
            }
74
        }
75
    }
76
}
77