• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

tempestphp / tempest-framework / 11743263957

08 Nov 2024 01:43PM UTC coverage: 82.172% (-0.3%) from 82.498%
11743263957

Pull #685

github

web-flow
Merge b9f02ce65 into 04000ace5
Pull Request #685: feat(commandbus): async commands

35 of 81 new or added lines in 6 files covered. (43.21%)

7278 of 8857 relevant lines covered (82.17%)

49.44 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/Tempest/CommandBus/src/MonitorAsyncCommands.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace Tempest\CommandBus;
6

7
use DateTimeImmutable;
8
use Symfony\Component\Process\Process;
9
use Tempest\Console\Console;
10
use Tempest\Console\ConsoleCommand;
11
use Tempest\Console\HasConsole;
12
use function Tempest\Support\arr;
13

14
final readonly class MonitorAsyncCommands
15
{
16
    use HasConsole;
17

NEW
18
    public function __construct(
×
19
        private AsyncCommandRepository $repository,
20
        private Console $console,
21
    ) {
NEW
22
    }
×
23

NEW
24
    #[ConsoleCommand(name: 'command:monitor')]
×
25
    public function __invoke(): void
26
    {
NEW
27
        $this->success("Monitoring for new commands. Press ctrl+c to stop.");
×
28

29
        /** @var \Symfony\Component\Process\Process[] $processes */
NEW
30
        $processes = [];
×
31

NEW
32
        while (true) { // @phpstan-ignore-line
×
NEW
33
            foreach ($processes as $key => $process) {
×
NEW
34
                $errorOutput = trim($process->getErrorOutput());
×
35

NEW
36
                $time = new DateTimeImmutable();
×
37

NEW
38
                if ($errorOutput) {
×
NEW
39
                    $this->error($errorOutput);
×
NEW
40
                    $this->writeln("<error>{$key}</error> failed at {$time->format('Y-m-d H:i:s')}");
×
NEW
41
                    unset($processes[$key]);
×
NEW
42
                } elseif ($process->isTerminated()) {
×
NEW
43
                    $this->writeln("<success>{$key}</success> finished at {$time->format('Y-m-d H:i:s')}");
×
NEW
44
                    unset($processes[$key]);
×
45
                }
46
            }
47

NEW
48
            $availableUuids = arr($this->repository->available())
×
NEW
49
                ->filter(fn (string $uuid) => ! in_array($uuid, array_keys($processes)));
×
50

NEW
51
            if (count($processes) === 5) {
×
NEW
52
                $this->sleep(0.5);
×
53

NEW
54
                continue;
×
55
            }
56

NEW
57
            if ($availableUuids->isEmpty()) {
×
NEW
58
                $this->sleep(0.5);
×
59

NEW
60
                continue;
×
61
            }
62

63
            // Start a task
NEW
64
            $uuid = $availableUuids->first();
×
NEW
65
            $time = new DateTimeImmutable();
×
NEW
66
            $this->writeln("<h2>{$uuid}</h2> started at {$time->format('Y-m-d H:i:s')}");
×
NEW
67
            $process = new Process(['php', 'tempest', 'command:handle', $uuid], getcwd());
×
NEW
68
            $process->start();
×
NEW
69
            $processes[$uuid] = $process;
×
70
        }
71
    }
72

NEW
73
    private function sleep(float $seconds): void
×
74
    {
NEW
75
        usleep((int) ($seconds * 1_000_000));
×
76
    }
77
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc