Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
### Changed

- Requires `innmind/immutable:~5.14`
- `Innmind\Server\Status\Server::cpu()` now returns `Innmind\Immutable\Attempt<Innmind\Server\Status\Server\Cpu>`
- `Innmind\Server\Status\Server::memory()` now returns `Innmind\Immutable\Attempt<Innmind\Server\Status\Server\Memory>`

## 4.1.1 - 2024-09-30

Expand Down
21 changes: 14 additions & 7 deletions src/Facade/Cpu/LinuxFacade.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
};
use Innmind\Immutable\{
Str,
Attempt,
Maybe,
Monoid\Concat,
};
Expand All @@ -31,9 +32,9 @@ public function __construct(Processes $processes)
}

/**
* @return Maybe<Cpu>
* @return Attempt<Cpu>
*/
public function __invoke(): Maybe
public function __invoke(): Attempt
{
return $this
->processes
Expand All @@ -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()
Expand All @@ -57,9 +60,9 @@ public function __invoke(): Maybe
}

/**
* @return Maybe<Cpu>
* @return Attempt<Cpu>
*/
private function parse(Str $output): Maybe
private function parse(Str $output): Attempt
{
$percentages = $output
->trim()
Expand Down Expand Up @@ -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')),
);
}
}
21 changes: 14 additions & 7 deletions src/Facade/Cpu/OSXFacade.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
};
use Innmind\Immutable\{
Str,
Attempt,
Maybe,
Monoid\Concat,
};
Expand All @@ -31,9 +32,9 @@ public function __construct(Processes $processes)
}

/**
* @return Maybe<Cpu>
* @return Attempt<Cpu>
*/
public function __invoke(): Maybe
public function __invoke(): Attempt
{
return $this
->processes
Expand All @@ -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()
Expand All @@ -58,9 +61,9 @@ public function __invoke(): Maybe
}

/**
* @return Maybe<Cpu>
* @return Attempt<Cpu>
*/
private function parse(Str $output): Maybe
private function parse(Str $output): Attempt
{
$percentages = $output
->trim()
Expand Down Expand Up @@ -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')),
);
}
}
21 changes: 14 additions & 7 deletions src/Facade/Memory/LinuxFacade.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Innmind\Immutable\{
Str,
Map,
Attempt,
Maybe,
Monoid\Concat,
};
Expand All @@ -38,18 +39,20 @@ public function __construct(Processes $processes)
}

/**
* @return Maybe<Memory>
* @return Attempt<Memory>
*/
public function __invoke(): Maybe
public function __invoke(): Attempt
{
return $this
->processes
->execute(
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()
Expand All @@ -60,9 +63,9 @@ public function __invoke(): Maybe
}

/**
* @return Maybe<Memory>
* @return Attempt<Memory>
*/
private function parse(Str $output): Maybe
private function parse(Str $output): Attempt
{
/** @var Map<string, int> */
$amounts = $output
Expand Down Expand Up @@ -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')),
);
}
}
11 changes: 8 additions & 3 deletions src/Facade/Memory/OSXFacade.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
};
use Innmind\Immutable\{
Str,
Attempt,
Maybe,
Monoid\Concat,
};
Expand All @@ -33,9 +34,9 @@ public function __construct(Processes $processes, EnvironmentPath $path)
}

/**
* @return Maybe<Memory>
* @return Attempt<Memory>
*/
public function __invoke(): Maybe
public function __invoke(): Attempt
{
$total = $this
->run(
Expand Down Expand Up @@ -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
Expand Down
10 changes: 5 additions & 5 deletions src/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,19 @@
Disk,
};
use Innmind\Url\Path;
use Innmind\Immutable\Maybe;
use Innmind\Immutable\Attempt;

interface Server
{
/**
* @return Maybe<Cpu>
* @return Attempt<Cpu>
*/
public function cpu(): Maybe;
public function cpu(): Attempt;

/**
* @return Maybe<Memory>
* @return Attempt<Memory>
*/
public function memory(): Maybe;
public function memory(): Attempt;
public function processes(): Processes;
public function loadAverage(): LoadAverage;
public function disk(): Disk;
Expand Down
6 changes: 3 additions & 3 deletions src/Servers/Linux.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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)();
}
Expand Down
6 changes: 3 additions & 3 deletions src/Servers/Logger.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -25,7 +25,7 @@ public function __construct(Server $server, LoggerInterface $logger)
}

#[\Override]
public function cpu(): Maybe
public function cpu(): Attempt
{
return $this
->server
Expand All @@ -38,7 +38,7 @@ public function cpu(): Maybe
}

#[\Override]
public function memory(): Maybe
public function memory(): Attempt
{
return $this
->server
Expand Down
6 changes: 3 additions & 3 deletions src/Servers/OSX.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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)();
}
Expand Down