Skip to content

Commit 8def69c

Browse files
committed
update documentation with new APIs
1 parent cf935f2 commit 8def69c

8 files changed

Lines changed: 159 additions & 117 deletions

File tree

documentation/advanced/logging.md

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,12 @@
22

33
If you want to trace everything that is done on your operating system you can use the logger decorator that will automatically write to your log file (almost) all operations.
44

5-
!!! note ""
6-
Data and actions done on a socket are not logged as well as processes output to prevent logging too much data (at least for now).
7-
85
```php
9-
use Innmind\OperatingSystem\OperatingSystem\Logger;
6+
use Innmind\OperatingSystem\Config\Logger;
107
use Psr\Log\LoggerInterface;
118

12-
$os = Logger::psr(
13-
$os,
14-
/* any instance of LoggerInterface */
9+
$os = $os->map(
10+
Logger::psr(/* any instance of LoggerInterface */),
1511
);
1612
```
1713

documentation/use_cases/filesystem.md

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,14 @@ $backup = function(Adapter $source, Adapter $target): void {
1717
});
1818
};
1919
$backup(
20-
$os->filesystem()->mount(Path::of('/path/to/source/')),
21-
$os->filesystem()->mount(Path::of('/path/to/target/')),
20+
$os
21+
->filesystem()
22+
->mount(Path::of('/path/to/source/'))
23+
->unwrap(),
24+
$os
25+
->filesystem()
26+
->mount(Path::of('/path/to/target/'))
27+
->unwrap(),
2228
);
2329
```
2430

@@ -53,7 +59,10 @@ $addUserPicture = function(
5359
);
5460
};
5561
$addUserPicture(
56-
$os->filesystem()->mount(Path::of('/path/to/users/data/')),
62+
$os
63+
->filesystem()
64+
->mount(Path::of('/path/to/users/data/'))
65+
->unwrap(),
5766
'some-unique-id',
5867
File::named(
5968
'picture.png',
@@ -94,10 +103,18 @@ use Innmind\Url\Path;
94103
$path = Path::of('/path/to/some/required/folder/');
95104

96105
if (!$os->filesystem()->contains($path)) {
97-
$os->control()->processes()->execute($mkdirCommand);
106+
$os
107+
->control()
108+
->processes()
109+
->execute($mkdirCommand)
110+
->unwrap();
98111
}
99112

100-
$os->control()->processes()->execute($subProcessCommand);
113+
$os
114+
->control()
115+
->processes()
116+
->execute($subProcessCommand)
117+
->unwrap();
101118
```
102119

103120
See [processes](processes.md) section on how to execute commands on your operating system.
@@ -109,7 +126,10 @@ Sometimes you want to use the `tmp` folder to write down files such as cache tha
109126
```php
110127
use Innmind\Filesystem\Adapter;
111128

112-
$tmp = $os->filesystem()->mount($os->status()->tmp());
129+
$tmp = $os
130+
->filesystem()
131+
->mount($os->status()->tmp())
132+
->unwrap();
113133
$tmp instanceof Adapter; // true
114134
```
115135

@@ -131,10 +151,17 @@ $count = $runTests(
131151
return $continuation->stop($count);
132152
}
133153

134-
$os->control()->processes()->execute($phpunitCommand);
154+
$os
155+
->control()
156+
->processes()
157+
->execute($phpunitCommand)
158+
->unwrap();
135159

136160
return $continuation->continue(++$count);
137161
},
162+
)->match(
163+
static fn(int $count) => $count, // always 42 as it's the stopping value
164+
static fn(\Throwable $e) => throw $e,
138165
);
139166
```
140167

documentation/use_cases/http.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,10 @@ All elements of a request/response call is built using objects to enforce correc
3636
One of the first things taught when working with distributed systems is that they will intermittently fail. To prevent your app to crash for an occasional failure a common pattern is the _retry pattern_ with a backoff strategy allowing the client to retry safe requests a certain amount of time before giving up. You can use this pattern like so:
3737

3838
```php
39-
use Innmind\OperatingSystem\OperatingSystem\Resilient;
39+
use Innmind\OperatingSystem\Config\Resilient;
4040
use Innmind\HttpTransport\ExponentialBackoff;
4141

42-
$os = Resilient::of($os);
42+
$os = $os->map(Resilient::new());
4343
$http = $os->remote()->http();
4444
$http instanceof ExponentialBackoff; // true
4545
```
@@ -48,12 +48,12 @@ Another strategy you can add on top of that is the [circuit breaker pattern](htt
4848

4949
```php
5050
use Innmind\HttpTransport\CircuitBreaker;
51-
use Innmind\TimeContinuum\Earth\Period\Minute;
51+
use Innmind\TimeContinuum\Period;
5252

5353
$http = CircuitBreaker::of(
5454
$http,
5555
$os->clock(),
56-
new Minute(1),
56+
Period::minute(1),
5757
);
5858
$request = Request::of(/* ...args */)
5959
$response = $http($request);

documentation/use_cases/ipc.md

Lines changed: 49 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -7,57 +7,60 @@ The later is the safest of the two (but not exempt of problems) and you will fin
77
!!! tip ""
88
The adage `share state through messages and not messages through state` is a pillar of the [actor model](https://en.wikipedia.org/wiki/Actor_model) and [initially of object oriented programming](https://www.youtube.com/watch?v=7erJ1DV_Tlo).
99

10-
```php
11-
# process acting as a server
12-
use Innmind\Socket\Address\Unix as Address;
13-
use Innmind\TimeContinuum\Earth\ElapsedPeriod;
14-
use Innmind\Immutable\{
15-
Sequence,
16-
Str,
17-
};
10+
=== "Server"
11+
```php
12+
use Innmind\IO\Sockets\Unix\Address;
13+
use Innmind\Url\Path;
14+
use Innmind\TimeContinuum\Period;
15+
use Innmind\Immutable\{
16+
Sequence,
17+
Str,
18+
};
1819

19-
$server = $os->sockets()->open(Address::of('/tmp/foo'))->match(
20-
static fn($server) => $server,
21-
static fn() => throw new \RuntimeException('Unable to start the server'),
22-
);
23-
$watch = $os->sockets()->watch(new ElapsedPeriod(1000))->forRead($server);
20+
$server = $os
21+
->sockets()
22+
->open(Address::of(Path::of('/tmp/foo')))
23+
->unwrap();
24+
->timeoutAfter(Period::second(1));
2425

25-
while (true) {
26-
$_ = $server
27-
->timeoutAfter(ElapsedPeriod::of(1_000))
28-
->accept()
29-
->match(
30-
static fn($client) => $client
31-
->send(Sequence::of(Str::of('Hello')))
32-
->flatMap(static fn() => $client->close())
33-
->match(
34-
static fn() => null, // everyhting is ok
35-
static fn() => throw new \RuntimeException('Unable to send data or close the connection'),
36-
),
37-
static fn() => null, // no new connection available
38-
),
39-
}
40-
```
26+
while (true) {
27+
$_ = $server
28+
->accept()
29+
->match(
30+
static fn($client) => $client
31+
->sink(Sequence::of(Str::of('Hello')))
32+
->flatMap(static fn() => $client->close())
33+
->match(
34+
static fn() => null, // everyhting is ok
35+
static fn(\Throwable $e) => throw $e,
36+
),
37+
static fn() => null, // no new connection available
38+
),
39+
}
40+
```
4141

42-
```php
43-
# process acting as client
44-
use Innmind\Socket\Address\Unix as Address;
45-
use Innmind\IO\Readable\Frame;
42+
=== "Client"
43+
```php
44+
use Innmind\IO\{
45+
Sockets\Unix\Address,
46+
Frame,
47+
};
48+
use Innmind\Url\Path;
4649

47-
$client = $os->sockets()->connectTo(Address::of('/tmp/foo'))->match(
48-
static fn($client) => $client,
49-
static fn() => throw new \RuntimeException('Unable to connect to the server'),
50-
);
50+
$client = $os
51+
->sockets()
52+
->connectTo(Address::of(Path::of('/tmp/foo')))
53+
->unwrap();
5154

52-
echo $client
53-
->watch()
54-
->frames(Frame\Chunk::of(5))
55-
->one()
56-
->match(
57-
static fn($data) => $data->toString(),
58-
static fn() => 'unable to read the stream',
59-
);
60-
```
55+
echo $client
56+
->watch()
57+
->frames(Frame::chunk(5)->strict())
58+
->one()
59+
->match(
60+
static fn($data) => $data->toString(),
61+
static fn() => 'unable to read the stream',
62+
);
63+
```
6164

6265
In the case the server is started first then the client would print `Hello`.
6366

documentation/use_cases/processes.md

Lines changed: 45 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,38 @@ use Innmind\Server\Control\Server\{
1212
Signal,
1313
};
1414

15-
$webserver = $os->control()->processes()->execute(
16-
Command::foreground('php')
17-
->withShortOption('S', 'localhost:8080'),
18-
);
15+
$webserver = $os
16+
->control()
17+
->processes()
18+
->execute(
19+
Command::foreground('php')
20+
->withShortOption('S', 'localhost:8080'),
21+
)
22+
->unwrap();
1923
// do some stuff
20-
$os->control()->processes()->kill($webserver->pid(), Signal::kill);
24+
$webserver->pid()->match(
25+
static fn($pid) => $os
26+
->control()
27+
->processes()
28+
->kill($pid, Signal::kill)
29+
->unwrap(),
30+
static fn() => null, // background processes don't have a pid
31+
);
2132
```
2233

2334
Here we start the PHP builtin webserver and perform some imaginary action before killing it, but you could also wait the process to finish (see below) instead of killing it (in the case of the webserver it never finishes unless with a crash).
2435

2536
```php
2637
use Innmind\Server\Control\Server\Command;
2738

28-
$webserver = $os->control()->processes()->execute(
29-
Command::foreground('php')
30-
->withShortOption('S', 'localhost:8080'),
31-
);
39+
$webserver = $os
40+
->control()
41+
->processes()
42+
->execute(
43+
Command::foreground('php')
44+
->withShortOption('S', 'localhost:8080'),
45+
)
46+
->unwrap();
3247
$webserver->wait();
3348
```
3449

@@ -37,10 +52,14 @@ Or you could start the process as an independent one (meaning you can't control
3752
```php
3853
use Innmind\Server\Control\Server\Command;
3954

40-
$os->control()->processes()->execute(
41-
Command::background('php')
42-
->withShortOption('S', 'localhost:8080'),
43-
);
55+
$os
56+
->control()
57+
->processes()
58+
->execute(
59+
Command::background('php')
60+
->withShortOption('S', 'localhost:8080'),
61+
)
62+
->unwrap();
4463
```
4564

4665
## Executing processes on a remote machine
@@ -63,15 +82,15 @@ $installMariadb($os->remote()->ssh(Url::of('ssh://user@replication2')));
6382

6483
```php
6584
use Innmind\Server\Status\Server\Process;
66-
use Innmind\TimeContinuum\Earth\Format\ISO8601;
85+
use Innmind\TimeContinuum\Format;
6786

6887
$os->status()->processes()->all()->foreach(function(Process $process): void {
6988
\printf(
7089
"Process %s started by %s at %s\n",
7190
$process->command()->toString(),
7291
$process->user()->toString(),
7392
$process->start()->match(
74-
static fn($date) => $date->format(new ISO8601),
93+
static fn($date) => $date->format(Format::iso8601()),
7594
static fn() => 'unknown start date',
7695
),
7796
);
@@ -97,9 +116,13 @@ $backupRunning = $os
97116
);
98117

99118
if (!$backupRunning) {
100-
$os->control()->processes()->execute(
101-
Command::background('my-backup-tool'),
102-
);
119+
$os
120+
->control()
121+
->processes()
122+
->execute(
123+
Command::background('my-backup-tool'),
124+
)
125+
->unwrap();
103126
}
104127
```
105128

@@ -108,8 +131,8 @@ if (!$backupRunning) {
108131
```php
109132
use Innmind\Url\Url;
110133

111-
$os->control()->reboot();
112-
$os->control()->shutdown();
113-
$os->remote()->ssh(Url::of('ssh://user@remote-server'))->reboot();
114-
$os->remote()->ssh(Url::of('ssh://user@remote-server'))->shutdown();
134+
$os->control()->reboot()->unwrap();
135+
$os->control()->shutdown()->unwrap();
136+
$os->remote()->ssh(Url::of('ssh://user@remote-server'))->reboot()->unwrap();
137+
$os->remote()->ssh(Url::of('ssh://user@remote-server'))->shutdown()->unwrap();
115138
```

0 commit comments

Comments
 (0)