-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path02-inspect-worker-group.php
More file actions
79 lines (61 loc) · 2.32 KB
/
Copy path02-inspect-worker-group.php
File metadata and controls
79 lines (61 loc) · 2.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
<?php
declare(strict_types=1);
/**
* This file is part of fast-forward/fork.
*
* This source file is subject to the license bundled
* with this source code in the file LICENSE.
*
* @copyright Copyright (c) 2026 Felipe Sayão Lobato Abreu <github@mentordosnerds.com>
* @license https://opensource.org/licenses/MIT MIT License
*
* @see https://github.com/php-fast-forward/fork
* @see https://github.com/php-fast-forward
* @see https://datatracker.ietf.org/doc/html/rfc2119
*/
use FastForward\Fork\Examples\Support\ExampleConsole;
use FastForward\Fork\Examples\Support\ExampleRuntime;
use FastForward\Fork\Manager\ForkManager;
use FastForward\Fork\Worker\WorkerInterface;
require __DIR__ . '/bootstrap.php';
$console = new ExampleConsole();
$console->title(
'02 Inspect worker group',
'Observe workers while they are still running and access them by PID.',
);
$manager = new ForkManager();
$group = $manager->fork(
static function (WorkerInterface $worker): int {
$delayMilliseconds = match ($worker->getPid() % 3) {
0 => 250,
1 => 450,
default => 650,
};
echo sprintf("worker %d sleeping for %dms\n", $worker->getPid(), $delayMilliseconds);
usleep($delayMilliseconds * 1_000);
echo sprintf("worker %d finished\n", $worker->getPid());
return 0;
},
3,
);
$firstPid = array_key_first($group->all());
if (! is_int($firstPid)) {
throw new RuntimeException('Expected at least one worker in the group.');
}
$console->line(sprintf('Initial group size: %d', $group->count()));
$console->line(sprintf('Initial running workers: %d', count($group->getRunning())));
$console->printWorker(
sprintf('Worker fetched with $group->get(%d)', $firstPid),
$group->get($firstPid) ?? throw new RuntimeException('Expected the worker fetched by PID to exist.'),
);
$stoppedDetected = ExampleRuntime::waitUntil(
static fn(): bool => [] !== $group->getStopped(),
timeoutSeconds: 1.5,
);
if (! $stoppedDetected) {
throw new RuntimeException('Expected at least one worker to finish during the inspection window.');
}
$console->printWorkers('Workers still running', $group->getRunning());
$console->printWorkers('Workers already stopped', $group->getStopped());
$group->wait();
$console->printGroup('Group after completion', $group);