Skip to content

Commit c90f08c

Browse files
committed
Merge branch 'develop'
* develop: specify next release fix example use ramsey/composer-install make sure the start date is parsed correctly use specific value to correctly run macOS memory facade use innmind/server-control 5 fix documentation fix tests fix mininum version of psalm use innmind/server-control instead of symfony/process require php 8.1 fix psalm config fix psalm errors fix tests CS
2 parents 0ab9d64 + 7bafc1b commit c90f08c

25 files changed

Lines changed: 536 additions & 212 deletions

.github/workflows/ci.yml

Lines changed: 11 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ jobs:
88
strategy:
99
matrix:
1010
os: [ubuntu-latest, macOS-latest]
11-
php-version: ['8.0', '8.1']
11+
php-version: ['8.1', '8.2']
1212
dependencies: ['lowest', 'highest']
1313
name: 'PHPUnit'
1414
steps:
@@ -20,21 +20,10 @@ jobs:
2020
php-version: ${{ matrix.php-version }}
2121
extensions: mbstring, intl
2222
coverage: xdebug
23-
- name: Get Composer Cache Directory
24-
id: composer-cache
25-
run: echo "::set-output name=dir::$(composer config cache-files-dir)"
26-
- name: Cache dependencies
27-
uses: actions/cache@v2
23+
- name: Composer
24+
uses: "ramsey/composer-install@v2"
2825
with:
29-
path: ${{ steps.composer-cache.outputs.dir }}
30-
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.json') }}
31-
restore-keys: ${{ runner.os }}-composer-
32-
- name: Install Dependencies
33-
if: ${{ matrix.dependencies == 'lowest' }}
34-
run: composer update --prefer-lowest --no-progress
35-
- name: Install Dependencies
36-
if: ${{ matrix.dependencies == 'highest' }}
37-
run: composer install --no-progress
26+
dependency-versions: ${{ matrix.dependencies }}
3827
- name: PHPUnit
3928
run: vendor/bin/phpunit --coverage-clover=coverage.clover
4029
- uses: codecov/codecov-action@v1
@@ -44,7 +33,7 @@ jobs:
4433
runs-on: ubuntu-latest
4534
strategy:
4635
matrix:
47-
php-version: ['8.0', '8.1']
36+
php-version: ['8.1', '8.2']
4837
dependencies: ['lowest', 'highest']
4938
name: 'Psalm'
5039
steps:
@@ -55,28 +44,17 @@ jobs:
5544
with:
5645
php-version: ${{ matrix.php-version }}
5746
extensions: mbstring, intl
58-
- name: Get Composer Cache Directory
59-
id: composer-cache
60-
run: echo "::set-output name=dir::$(composer config cache-files-dir)"
61-
- name: Cache dependencies
62-
uses: actions/cache@v2
47+
- name: Composer
48+
uses: "ramsey/composer-install@v2"
6349
with:
64-
path: ${{ steps.composer-cache.outputs.dir }}
65-
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.json') }}
66-
restore-keys: ${{ runner.os }}-composer-
67-
- name: Install Dependencies
68-
if: ${{ matrix.dependencies == 'lowest' }}
69-
run: composer update --prefer-lowest --no-progress
70-
- name: Install Dependencies
71-
if: ${{ matrix.dependencies == 'highest' }}
72-
run: composer install --no-progress
50+
dependency-versions: ${{ matrix.dependencies }}
7351
- name: Psalm
7452
run: vendor/bin/psalm --shepherd
7553
cs:
7654
runs-on: ubuntu-latest
7755
strategy:
7856
matrix:
79-
php-version: ['8.0']
57+
php-version: ['8.1']
8058
name: 'CS'
8159
steps:
8260
- name: Checkout
@@ -86,16 +64,7 @@ jobs:
8664
with:
8765
php-version: ${{ matrix.php-version }}
8866
extensions: mbstring, intl
89-
- name: Get Composer Cache Directory
90-
id: composer-cache
91-
run: echo "::set-output name=dir::$(composer config cache-files-dir)"
92-
- name: Cache dependencies
93-
uses: actions/cache@v2
94-
with:
95-
path: ${{ steps.composer-cache.outputs.dir }}
96-
key: ${{ runner.os }}-${{ matrix.php-version }}-composer-${{ hashFiles('**/composer.json') }}
97-
restore-keys: ${{ runner.os }}-${{ matrix.php-version }}-composer-
98-
- name: Install Dependencies
99-
run: composer install --no-progress
67+
- name: Composer
68+
uses: "ramsey/composer-install@v2"
10069
- name: CS
10170
run: vendor/bin/php-cs-fixer fix --diff --dry-run

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Changelog
22

3+
## 4.0.0 - 2023-01-29
4+
5+
### Changed
6+
7+
- `Innmind\Server\Status\ServerFactory::build()` now expects `Innmind\Server\Control\Server` as second argument and `Innmind\Server\Status\EnvironmentPath` as third argument
8+
39
## 3.0.0 - 2022-01-23
410

511
### Added

README.md

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,22 @@ use Innmind\Server\Status\{
2121
ServerFactory,
2222
Server\Disk\Volume\MountPoint,
2323
Server\Process\Pid,
24+
EnvironmentPath,
2425
};
26+
use Innmind\Server\Control\ServerFactory as Control;
2527
use Innmind\TimeContinuum\Earth\Clock;
26-
27-
$server = ServerFactory::build(new Clock);
28+
use Innmind\TimeWarp\Halt\Usleep;
29+
use Innmind\Stream\Streams;
30+
31+
$server = ServerFactory::build(
32+
$clock = new Clock,
33+
Control::build(
34+
$clock,
35+
Streams::fromAmbientAuthority(),
36+
new Usleep,
37+
),
38+
EnvironmentPath::of(\getenv('PATH')),
39+
);
2840

2941
$server->cpu()->match(
3042
function($cpu) {

composer.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@
1414
"issues": "http://github.com/Innmind/ServerStatus/issues"
1515
},
1616
"require": {
17-
"php": "~8.0",
17+
"php": "~8.1",
1818
"innmind/immutable": "~4.0",
1919
"innmind/time-continuum": "~3.0",
20-
"symfony/process": "~6.0",
2120
"innmind/url": "~4.0",
22-
"psr/log": "~3.0"
21+
"psr/log": "~3.0",
22+
"innmind/server-control": "~5.0"
2323
},
2424
"autoload": {
2525
"psr-4": {
@@ -33,7 +33,7 @@
3333
},
3434
"require-dev": {
3535
"phpunit/phpunit": "~9.0",
36-
"vimeo/psalm": "~4.9",
36+
"vimeo/psalm": "~4.30",
3737
"innmind/coding-standard": "~2.0"
3838
}
3939
}

psalm.xml

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0"?>
22
<psalm
3-
totallyTyped="true"
3+
errorLevel="1"
44
resolveFromConfigFile="true"
55
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
66
xmlns="https://getpsalm.org/schema/config"
@@ -12,8 +12,4 @@
1212
<directory name="vendor" />
1313
</ignoreFiles>
1414
</projectFiles>
15-
16-
<issueHandlers>
17-
<LessSpecificReturnType errorLevel="info" />
18-
</issueHandlers>
1915
</psalm>

src/EnvironmentPath.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
declare(strict_types = 1);
3+
4+
namespace Innmind\Server\Status;
5+
6+
/**
7+
* @psalm-immutable
8+
*/
9+
final class EnvironmentPath
10+
{
11+
private string $value;
12+
13+
private function __construct(string $value)
14+
{
15+
$this->value = $value;
16+
}
17+
18+
/**
19+
* @psalm-pure
20+
*/
21+
public static function of(string $value): self
22+
{
23+
return new self($value);
24+
}
25+
26+
public function toString(): string
27+
{
28+
return $this->value;
29+
}
30+
}

src/Facade/Cpu/LinuxFacade.php

Lines changed: 44 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,45 +8,73 @@
88
Server\Cpu\Percentage,
99
Server\Cpu\Cores,
1010
};
11+
use Innmind\Server\Control\Server\{
12+
Processes,
13+
Command,
14+
};
1115
use Innmind\Immutable\{
1216
Str,
1317
Maybe,
1418
};
15-
use Symfony\Component\Process\Process;
1619

1720
/**
1821
* @internal
1922
*/
2023
final class LinuxFacade
2124
{
25+
private Processes $processes;
26+
27+
public function __construct(Processes $processes)
28+
{
29+
$this->processes = $processes;
30+
}
31+
2232
/**
2333
* @return Maybe<Cpu>
2434
*/
2535
public function __invoke(): Maybe
2636
{
27-
$process = Process::fromShellCommandline('top -bn1 | grep \'%Cpu\'');
28-
$process->run();
29-
30-
if (!$process->isSuccessful()) {
31-
/** @var Maybe<Cpu> */
32-
return Maybe::nothing();
33-
}
37+
return $this
38+
->processes
39+
->execute(
40+
Command::foreground('top')
41+
->withShortOption('bn1')
42+
->pipe(
43+
Command::foreground('grep')
44+
->withArgument('%Cpu'),
45+
),
46+
)
47+
->wait()
48+
->maybe()
49+
->map(static fn($success) => $success->output()->toString())
50+
->map(Str::of(...))
51+
->flatMap($this->parse(...));
52+
}
3453

35-
$percentages = Str::of($process->getOutput())
54+
/**
55+
* @return Maybe<Cpu>
56+
*/
57+
private function parse(Str $output): Maybe
58+
{
59+
$percentages = $output
3660
->trim()
3761
->capture(
3862
'~^%Cpu\(s\): *(?P<user>\d+\.?\d*) us, *(?P<sys>\d+\.?\d*) sy, *(\d+\.?\d*) ni, *(?P<idle>\d+\.?\d*) id~',
3963
)
4064
->map(static fn($_, $percentage) => $percentage->toString())
4165
->map(static fn($_, $percentage) => (float) $percentage);
4266

43-
$process = Process::fromShellCommandline('nproc');
44-
$process->run();
45-
$cores = 1;
46-
47-
if ($process->isSuccessful()) {
48-
$cores = ((int) $process->getOutput()) ?: 1;
49-
}
67+
$cores = $this
68+
->processes
69+
->execute(Command::foreground('nproc'))
70+
->wait()
71+
->maybe()
72+
->map(static fn($success) => $success->output()->toString())
73+
->map(static fn($cores) => (int) $cores)
74+
->match(
75+
static fn($cores) => $cores,
76+
static fn() => 1,
77+
);
5078

5179
$user = $percentages->get('user');
5280
$sys = $percentages->get('sys');

src/Facade/Cpu/OSXFacade.php

Lines changed: 50 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,45 +8,79 @@
88
Server\Cpu\Percentage,
99
Server\Cpu\Cores,
1010
};
11+
use Innmind\Server\Control\Server\{
12+
Processes,
13+
Command,
14+
};
1115
use Innmind\Immutable\{
1216
Str,
1317
Maybe,
1418
};
15-
use Symfony\Component\Process\Process;
1619

1720
/**
1821
* @internal
1922
*/
2023
final class OSXFacade
2124
{
25+
private Processes $processes;
26+
27+
public function __construct(Processes $processes)
28+
{
29+
$this->processes = $processes;
30+
}
31+
2232
/**
2333
* @return Maybe<Cpu>
2434
*/
2535
public function __invoke(): Maybe
2636
{
27-
$process = Process::fromShellCommandline('top -l 1 -s 0 | grep \'CPU usage\'');
28-
$process->run();
29-
30-
if (!$process->isSuccessful()) {
31-
/** @var Maybe<Cpu> */
32-
return Maybe::nothing();
33-
}
37+
return $this
38+
->processes
39+
->execute(
40+
Command::foreground('top')
41+
->withShortOption('l', '1')
42+
->withShortOption('s', '0')
43+
->pipe(
44+
Command::foreground('grep')
45+
->withArgument('CPU usage'),
46+
),
47+
)
48+
->wait()
49+
->maybe()
50+
->map(static fn($success) => $success->output()->toString())
51+
->flatMap($this->parse(...));
52+
}
3453

35-
$percentages = Str::of($process->getOutput())
54+
/**
55+
* @return Maybe<Cpu>
56+
*/
57+
private function parse(string $output): Maybe
58+
{
59+
$percentages = Str::of($output)
3660
->trim()
3761
->capture(
3862
'~^CPU usage: (?P<user>\d+\.?\d*)% user, (?P<sys>\d+\.?\d*)% sys, (?P<idle>\d+\.?\d*)% idle$~',
3963
)
4064
->map(static fn($_, $percentage) => $percentage->toString())
4165
->map(static fn($_, $percentage) => (float) $percentage);
4266

43-
$process = Process::fromShellCommandline('sysctl -a | grep \'hw.ncpu:\'');
44-
$process->run();
45-
46-
$cores = Str::of($process->getOutput())
47-
->trim()
48-
->capture('~^hw.ncpu: (?P<cores>\d+)$~')
49-
->get('cores')
67+
$cores = $this
68+
->processes
69+
->execute(
70+
Command::foreground('sysctl')
71+
->withShortOption('a')
72+
->pipe(
73+
Command::foreground('grep')
74+
->withArgument('hw.ncpu:'),
75+
),
76+
)
77+
->wait()
78+
->maybe()
79+
->map(static fn($success) => $success->output()->toString())
80+
->map(Str::of(...))
81+
->map(static fn($output) => $output->trim())
82+
->map(static fn($output) => $output->capture('~^hw.ncpu: (?P<cores>\d+)$~'))
83+
->flatMap(static fn($output) => $output->get('cores'))
5084
->map(static fn($cores) => $cores->toString())
5185
->map(static fn($cores) => (int) $cores)
5286
->otherwise(static fn() => Maybe::just(1));

0 commit comments

Comments
 (0)