/
Monitor.php
117 lines (106 loc) · 2.94 KB
/
Monitor.php
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
<?php
declare(strict_types = 1);
namespace Innmind\LabStation;
use Innmind\LabStation\Activity\Type;
use Innmind\ProcessManager\{
Manager,
Running,
Process\Unkillable,
};
use Innmind\IPC\{
IPC,
Message,
Process\Name,
};
use Innmind\CLI\Console;
use Innmind\Immutable\{
Maybe,
Str,
Set,
};
final class Monitor
{
private Protocol $protocol;
private Manager $manager;
private IPC $ipc;
private Name $name;
private Iteration $iteration;
private Trigger $trigger;
/** @var list<Agent> */
private array $agents;
/**
* @no-named-arguments
*/
public function __construct(
Protocol $protocol,
Manager $manager,
IPC $ipc,
Name $name,
Iteration $iteration,
Trigger $trigger,
Agent ...$agents,
) {
$this->protocol = $protocol;
$this->manager = $manager;
$this->ipc = $ipc;
$this->name = $name;
$this->iteration = $iteration;
$this->trigger = $trigger;
$this->agents = $agents;
}
/**
* @param Set<Triggers> $triggers
*/
public function __invoke(Console $console, Set $triggers): Console
{
$project = $console->workingDirectory();
$manager = $this->manager;
foreach ($this->agents as $agent) {
$manager = $manager->schedule(static function() use ($agent, $project): void {
$agent($project);
});
}
return $manager
->start()
->maybe()
->flatMap(fn($agents) => $this->start($agents, $console, $triggers))
->match(
static fn($console) => $console
->error(Str::of("Terminated\n"))
->exit(1),
static fn() => $console
->error(Str::of("Unable to start the agents\n"))
->exit(1),
);
}
/**
* @param Set<Triggers> $triggers
*
* @return Maybe<Console>
*/
private function start(
Running $agents,
Console $console,
Set $triggers,
): Maybe {
$console = ($this->trigger)(new Activity(Type::start), $console, $triggers);
$server = $this->ipc->listen($this->name);
/** @psalm-suppress InvalidArgument */
return $server(
$console,
function($message, $continuation, Console $console) use ($triggers) {
$activity = $this->protocol->decode($message);
$this->iteration->start();
$console = ($this->trigger)($activity, $console, $triggers);
$console = $this->iteration->end($console);
return $continuation->continue($console);
},
)
->flatMap(
static fn(Console $console) => $agents
->kill()
->map(static fn() => $console),
)
->maybe();
}
}