Work   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
eloc 27
c 1
b 0
f 0
dl 0
loc 66
ccs 0
cts 11
cp 0
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B execute() 0 35 7
1
<?php
2
3
/**
4
 * This file is part of BlitzPHP Tasks.
5
 *
6
 * (c) 2025 Dimitri Sitchet Tomkeu <[email protected]>
7
 *
8
 * For the full copyright and license information, please view
9
 * the LICENSE file that was distributed with this source code.
10
 */
11
12
namespace BlitzPHP\Tasks\Commands;
13
14
use BlitzPHP\Utilities\Date;
15
use Symfony\Component\Process\Process;
16
17
class Work extends TaskCommand
18
{
19
    /**
20
     * {@inheritDoc}
21
     */
22
    protected $name = 'tasks:work';
23
24
    /**
25
     * {@inheritDoc}
26
     */
27
    protected $options = [
28
        '--run-output-file' => 'Le fichier vers lequel diriger la sortie de `tasks:run`.',
29
    ];
30
31
    /**
32
     * {@inheritDoc}
33
     */
34
    protected $description = 'Démarre le planificateur de tâche et l\'exécute localement.';
35
36
    /**
37
     * @var list<string>
38
     */
39
    protected $required = [
40
        'symfony/process:^7.2',
41
    ];
42
43
    /**
44
     * {@inheritDoc}
45
     *
46
     * @return void
47
     */
48
    public function execute(array $params)
49
    {
50
        $this->info('Exéctutions des tâches programmées.');
51
52
        [$lastExecutionStartedAt, $executions] = [Date::now()->subMinutes(10), []];
53
54
        $command = sprintf(
55
            '%s %s %s',
56
            escapeshellarg(PHP_BINARY),
57
            escapeshellarg(base_path('klinge')),
58
            'tasks:run'
59
        );
60
61
        if ($this->option('run-output-file')) {
62
            $command .= ' >> ' . escapeshellarg($this->option('run-output-file')) . ' 2>&1';
63
        }
64
65
        while (true) {
66
            usleep(100 * 1000);
67
68
            if ((int) (Date::now()->getSecond()) === 0 && ! Date::now()->startOfMinute()->equalTo($lastExecutionStartedAt)) {
69
                $executions[] = $execution = Process::fromShellCommandline($command);
70
71
                $execution->start();
72
73
                $lastExecutionStartedAt = Date::now()->startOfMinute();
74
            }
75
76
            foreach ($executions as $key => $execution) {
77
                $output = $execution->getIncrementalOutput() . $execution->getIncrementalErrorOutput();
78
79
                $this->write(ltrim($output, "\n"))->eol();
80
81
                if (! $execution->isRunning()) {
82
                    unset($executions[$key]);
83
                }
84
            }
85
        }
86
    }
87
}
88