diff --git a/CHANGELOG.md b/CHANGELOG.md index 8c94bae..09dcf01 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,8 @@ ### Changed - Requires `innmind/immutable:~5.14` +- `Innmind\Server\Status\Server::cpu()` now returns `Innmind\Immutable\Attempt` +- `Innmind\Server\Status\Server::memory()` now returns `Innmind\Immutable\Attempt` ## 4.1.1 - 2024-09-30 diff --git a/src/Facade/Cpu/LinuxFacade.php b/src/Facade/Cpu/LinuxFacade.php index f376f46..c345bbf 100644 --- a/src/Facade/Cpu/LinuxFacade.php +++ b/src/Facade/Cpu/LinuxFacade.php @@ -14,6 +14,7 @@ }; use Innmind\Immutable\{ Str, + Attempt, Maybe, Monoid\Concat, }; @@ -31,9 +32,9 @@ public function __construct(Processes $processes) } /** - * @return Maybe + * @return Attempt */ - public function __invoke(): Maybe + public function __invoke(): Attempt { return $this ->processes @@ -45,8 +46,10 @@ public function __invoke(): Maybe ->withArgument('%Cpu'), ), ) - ->maybe() - ->flatMap(static fn($process) => $process->wait()->maybe()) + ->flatMap(static fn($process) => $process->wait()->match( + Attempt::result(...), + static fn() => Attempt::error(new \RuntimeException('Failed to retrieve CPU usage')), + )) ->map( static fn($success) => $success ->output() @@ -57,9 +60,9 @@ public function __invoke(): Maybe } /** - * @return Maybe + * @return Attempt */ - private function parse(Str $output): Maybe + private function parse(Str $output): Attempt { $percentages = $output ->trim() @@ -97,6 +100,10 @@ private function parse(Str $output): Maybe new Percentage($sys), new Percentage($idle), new Cores($cores), - )); + )) + ->match( + Attempt::result(...), + static fn() => Attempt::error(new \RuntimeException('Failed to parse CPU usage')), + ); } } diff --git a/src/Facade/Cpu/OSXFacade.php b/src/Facade/Cpu/OSXFacade.php index 6f5c440..a8706d8 100644 --- a/src/Facade/Cpu/OSXFacade.php +++ b/src/Facade/Cpu/OSXFacade.php @@ -14,6 +14,7 @@ }; use Innmind\Immutable\{ Str, + Attempt, Maybe, Monoid\Concat, }; @@ -31,9 +32,9 @@ public function __construct(Processes $processes) } /** - * @return Maybe + * @return Attempt */ - public function __invoke(): Maybe + public function __invoke(): Attempt { return $this ->processes @@ -46,8 +47,10 @@ public function __invoke(): Maybe ->withArgument('CPU usage'), ), ) - ->maybe() - ->flatMap(static fn($process) => $process->wait()->maybe()) + ->flatMap(static fn($process) => $process->wait()->match( + Attempt::result(...), + static fn() => Attempt::error(new \RuntimeException('Failed to retrieve CPU usage')), + )) ->map( static fn($success) => $success ->output() @@ -58,9 +61,9 @@ public function __invoke(): Maybe } /** - * @return Maybe + * @return Attempt */ - private function parse(Str $output): Maybe + private function parse(Str $output): Attempt { $percentages = $output ->trim() @@ -104,6 +107,10 @@ private function parse(Str $output): Maybe new Percentage($sys), new Percentage($idle), new Cores($cores), - )); + )) + ->match( + Attempt::result(...), + static fn() => Attempt::error(new \RuntimeException('Failed to parse CPU usage')), + ); } } diff --git a/src/Facade/Memory/LinuxFacade.php b/src/Facade/Memory/LinuxFacade.php index e1d03fb..e0ee702 100644 --- a/src/Facade/Memory/LinuxFacade.php +++ b/src/Facade/Memory/LinuxFacade.php @@ -14,6 +14,7 @@ use Innmind\Immutable\{ Str, Map, + Attempt, Maybe, Monoid\Concat, }; @@ -38,9 +39,9 @@ public function __construct(Processes $processes) } /** - * @return Maybe + * @return Attempt */ - public function __invoke(): Maybe + public function __invoke(): Attempt { return $this ->processes @@ -48,8 +49,10 @@ public function __invoke(): Maybe Command::foreground('cat') ->withArgument('/proc/meminfo'), ) - ->maybe() - ->flatMap(static fn($process) => $process->wait()->maybe()) + ->flatMap(static fn($process) => $process->wait()->match( + Attempt::result(...), + static fn() => Attempt::error(new \RuntimeException('Failed to retrieve memory usage')), + )) ->map( static fn($success) => $success ->output() @@ -60,9 +63,9 @@ public function __invoke(): Maybe } /** - * @return Maybe + * @return Attempt */ - private function parse(Str $output): Maybe + private function parse(Str $output): Attempt { /** @var Map */ $amounts = $output @@ -102,6 +105,10 @@ static function(Map $map, Str $line): Map { new Bytes($free), new Bytes($swap), new Bytes($used), - )); + )) + ->match( + Attempt::result(...), + static fn() => Attempt::error(new \RuntimeException('Failed to parse memory usage')), + ); } } diff --git a/src/Facade/Memory/OSXFacade.php b/src/Facade/Memory/OSXFacade.php index 5a8e49c..4eb3b9c 100644 --- a/src/Facade/Memory/OSXFacade.php +++ b/src/Facade/Memory/OSXFacade.php @@ -14,6 +14,7 @@ }; use Innmind\Immutable\{ Str, + Attempt, Maybe, Monoid\Concat, }; @@ -33,9 +34,9 @@ public function __construct(Processes $processes, EnvironmentPath $path) } /** - * @return Maybe + * @return Attempt */ - public function __invoke(): Maybe + public function __invoke(): Attempt { $total = $this ->run( @@ -96,7 +97,11 @@ public function __invoke(): Maybe $unused, $swap, $used, - )); + )) + ->match( + Attempt::result(...), + static fn() => Attempt::error(new \RuntimeException('Failed to parse memory usage')), + ); } private function run(Command $command): Str diff --git a/src/Server.php b/src/Server.php index acbc85d..4be95f3 100644 --- a/src/Server.php +++ b/src/Server.php @@ -11,19 +11,19 @@ Disk, }; use Innmind\Url\Path; -use Innmind\Immutable\Maybe; +use Innmind\Immutable\Attempt; interface Server { /** - * @return Maybe + * @return Attempt */ - public function cpu(): Maybe; + public function cpu(): Attempt; /** - * @return Maybe + * @return Attempt */ - public function memory(): Maybe; + public function memory(): Attempt; public function processes(): Processes; public function loadAverage(): LoadAverage; public function disk(): Disk; diff --git a/src/Servers/Linux.php b/src/Servers/Linux.php index f8cda82..a39a0e4 100644 --- a/src/Servers/Linux.php +++ b/src/Servers/Linux.php @@ -17,7 +17,7 @@ use Innmind\Server\Control\Server as Control; use Innmind\TimeContinuum\Clock; use Innmind\Url\Path; -use Innmind\Immutable\Maybe; +use Innmind\Immutable\Attempt; final class Linux implements Server { @@ -37,13 +37,13 @@ public function __construct(Clock $clock, Control $control) } #[\Override] - public function cpu(): Maybe + public function cpu(): Attempt { return ($this->cpu)(); } #[\Override] - public function memory(): Maybe + public function memory(): Attempt { return ($this->memory)(); } diff --git a/src/Servers/Logger.php b/src/Servers/Logger.php index 46c6d40..14ea4ed 100644 --- a/src/Servers/Logger.php +++ b/src/Servers/Logger.php @@ -10,7 +10,7 @@ Server\Disk, }; use Innmind\Url\Path; -use Innmind\Immutable\Maybe; +use Innmind\Immutable\Attempt; use Psr\Log\LoggerInterface; final class Logger implements Server @@ -25,7 +25,7 @@ public function __construct(Server $server, LoggerInterface $logger) } #[\Override] - public function cpu(): Maybe + public function cpu(): Attempt { return $this ->server @@ -38,7 +38,7 @@ public function cpu(): Maybe } #[\Override] - public function memory(): Maybe + public function memory(): Attempt { return $this ->server diff --git a/src/Servers/OSX.php b/src/Servers/OSX.php index 4956b09..edf3901 100644 --- a/src/Servers/OSX.php +++ b/src/Servers/OSX.php @@ -18,7 +18,7 @@ use Innmind\Server\Control\Server as Control; use Innmind\TimeContinuum\Clock; use Innmind\Url\Path; -use Innmind\Immutable\Maybe; +use Innmind\Immutable\Attempt; final class OSX implements Server { @@ -38,13 +38,13 @@ public function __construct(Clock $clock, Control $control, EnvironmentPath $pat } #[\Override] - public function cpu(): Maybe + public function cpu(): Attempt { return ($this->cpu)(); } #[\Override] - public function memory(): Maybe + public function memory(): Attempt { return ($this->memory)(); }