Skip to content

Commit d5b8ada

Browse files
committed
feat: refactor status checker
1 parent 379c455 commit d5b8ada

20 files changed

Lines changed: 441 additions & 202 deletions

File tree

src/Components/Health/Checker/MysqlChecker.php renamed to src/Components/Health/Checker/HealthChecker/MysqlChecker.php

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
11
<?php declare(strict_types=1);
22

3-
namespace Frosh\Tools\Components\Health\Checker;
3+
namespace Frosh\Tools\Components\Health\Checker\HealthChecker;
44

55
use Doctrine\DBAL\Connection;
6+
use Frosh\Tools\Components\Health\Checker\CheckerInterface;
67
use Frosh\Tools\Components\Health\HealthCollection;
7-
use Frosh\Tools\Components\Health\HealthResult;
8+
use Frosh\Tools\Components\Health\SettingsResult;
89

910
class MysqlChecker implements CheckerInterface
1011
{
11-
/**
12-
* @var Connection
13-
*/
14-
private $connection;
12+
private Connection $connection;
1513

1614
public function __construct(Connection $connection)
1715
{
@@ -35,15 +33,18 @@ public function collect(HealthCollection $collection): void
3533
return;
3634
}
3735

38-
$collection->add(HealthResult::error('frosh-tools.checker.mysqlError'));
36+
$collection->add(SettingsResult::error('frosh-tools.checker.mysqlError'));
3937
}
4038

4139
private function checkMariadbVersion($collection, $version): void
4240
{
4341
$minVersion = '10.3';
4442

4543
if (version_compare($version, $minVersion, '>=')) {
46-
$collection->add(HealthResult::ok('frosh-tools.checker.mariaDbVersion', ['version' => $version]));
44+
$collection->add(SettingsResult::ok('frosh-tools.checker.mariaDbVersion',
45+
$version,
46+
'min ' . $minVersion
47+
));
4748
}
4849
}
4950

@@ -55,19 +56,30 @@ private function checkMysqlVersion($collection, $version): void
5556
'8.0.21',
5657
];
5758

59+
$recommended = 'min ' . $minVersion . ', but not ' . \implode(' or ', $brokenVersions);
60+
5861
if (in_array($version, $brokenVersions, true)) {
59-
$collection->add(HealthResult::error('frosh-tools.checker.mysqlDbVersionError', ['version' => $version]));
62+
$collection->add(SettingsResult::error('frosh-tools.checker.mysqlDbVersionError',
63+
$version,
64+
$recommended
65+
));
6066

6167
return;
6268
}
6369

6470
if (version_compare($version, $minVersion, '>=')) {
65-
$collection->add(HealthResult::ok('frosh-tools.checker.mysqlDbVersion', ['version' => $version]));
71+
$collection->add(SettingsResult::ok('frosh-tools.checker.mysqlDbVersion',
72+
$version,
73+
$recommended
74+
));
6675

6776
return;
6877
}
6978

70-
$collection->add(HealthResult::error('frosh-tools.checker.mysqlDbOutdated', ['version' => $version]));
79+
$collection->add(SettingsResult::error('frosh-tools.checker.mysqlDbOutdated',
80+
$version,
81+
'min ' . $minVersion
82+
));
7183
}
7284

7385
private function extract(string $versionString): array

src/Components/Health/Checker/PhpChecker.php renamed to src/Components/Health/Checker/HealthChecker/PhpChecker.php

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
<?php declare(strict_types=1);
22

3-
namespace Frosh\Tools\Components\Health\Checker;
3+
namespace Frosh\Tools\Components\Health\Checker\HealthChecker;
44

5+
use Frosh\Tools\Components\Health\Checker\CheckerInterface;
56
use Frosh\Tools\Components\Health\HealthCollection;
6-
use Frosh\Tools\Components\Health\HealthResult;
7+
use Frosh\Tools\Components\Health\SettingsResult;
78

89
class PhpChecker implements CheckerInterface
910
{
@@ -29,12 +30,17 @@ private function checkPhp(HealthCollection $collection): void
2930
$currentPhpVersion = \PHP_VERSION;
3031
if (version_compare($minPhpVersion, $currentPhpVersion, '>')) {
3132
$collection->add(
32-
HealthResult::error('frosh-tools.checker.phpOutdated',
33-
['minPhpVersion' => $minPhpVersion, 'version' => $currentPhpVersion])
33+
SettingsResult::error('frosh-tools.checker.phpOutdated',
34+
$currentPhpVersion,
35+
'min ' . $minPhpVersion
36+
)
3437
);
3538
} else {
3639
$collection->add(
37-
HealthResult::ok('frosh-tools.checker.phpGood', ['version' => $currentPhpVersion])
40+
SettingsResult::ok('frosh-tools.checker.phpGood',
41+
$currentPhpVersion,
42+
'min ' . $minPhpVersion
43+
)
3844
);
3945
}
4046
}
@@ -45,14 +51,19 @@ private function checkMaxExecutionTime(HealthCollection $collection): void
4551
$currentMaxExecutionTime = (int)ini_get('max_execution_time');
4652
if ($currentMaxExecutionTime < $minMaxExecutionTime) {
4753
$collection->add(
48-
HealthResult::error('frosh-tools.checker.maxExecutionTimeError',
49-
['minMaxExecutionTime' => $minMaxExecutionTime, 'maxExecutionTime' => $currentMaxExecutionTime])
54+
SettingsResult::error('frosh-tools.checker.maxExecutionTimeError',
55+
(string) $currentMaxExecutionTime,
56+
'min ' . $minMaxExecutionTime
57+
)
5058
);
5159

5260
return;
5361
}
5462

55-
$collection->add(HealthResult::ok('frosh-tools.checker.maxExecutionTimeGood', ['maxExecutionTime' => $currentMaxExecutionTime]));
63+
$collection->add(SettingsResult::ok('frosh-tools.checker.maxExecutionTimeGood',
64+
(string) $currentMaxExecutionTime,
65+
'min ' . $minMaxExecutionTime
66+
));
5667
}
5768

5869
private function checkMemoryLimit(HealthCollection $collection): void
@@ -61,21 +72,28 @@ private function checkMemoryLimit(HealthCollection $collection): void
6172
$currentMemoryLimit = $this->decodePhpSize(ini_get('memory_limit'));
6273
if ($currentMemoryLimit < $minMemoryLimit) {
6374
$collection->add(
64-
HealthResult::error('frosh-tools.checker.memoryLimitError',
65-
['minMemoryLimit' => $this->formatSize($minMemoryLimit), 'memoryLimit' => $this->formatSize($currentMemoryLimit)])
75+
SettingsResult::error('frosh-tools.checker.memoryLimitError',
76+
$this->formatSize($currentMemoryLimit),
77+
'min ' . $this->formatSize($minMemoryLimit)
78+
)
6679
);
67-
} else {
68-
$collection->add(HealthResult::ok('frosh-tools.checker.memoryLimitGood', ['memoryLimit' => $this->formatSize($currentMemoryLimit)]));
80+
return;
6981
}
82+
83+
$collection->add(SettingsResult::ok('frosh-tools.checker.memoryLimitGood',
84+
$this->formatSize($currentMemoryLimit),
85+
'min ' . $this->formatSize($minMemoryLimit)
86+
));
7087
}
7188

7289
private function checkOpCacheActive(HealthCollection $collection): void
7390
{
7491
if (\extension_loaded('Zend OPcache') && ini_get('opcache.enable')) {
75-
$collection->add(HealthResult::ok('frosh-tools.checker.zendOpcacheGood'));
76-
} else {
77-
$collection->add(HealthResult::warning('frosh-tools.checker.zendOpcacheWarning'));
92+
$collection->add(SettingsResult::ok('frosh-tools.checker.zendOpcacheGood'));
93+
return;
7894
}
95+
96+
$collection->add(SettingsResult::warning('frosh-tools.checker.zendOpcacheWarning'));
7997
}
8098

8199
private function decodePhpSize($val): float

src/Components/Health/Checker/ProductionChecker.php renamed to src/Components/Health/Checker/HealthChecker/ProductionChecker.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
<?php declare(strict_types=1);
22

3-
namespace Frosh\Tools\Components\Health\Checker;
3+
namespace Frosh\Tools\Components\Health\Checker\HealthChecker;
44

5+
use Frosh\Tools\Components\Health\Checker\CheckerInterface;
56
use Frosh\Tools\Components\Health\HealthCollection;
6-
use Frosh\Tools\Components\Health\HealthResult;
7+
use Frosh\Tools\Components\Health\SettingsResult;
78

89
class ProductionChecker implements CheckerInterface
910
{
@@ -17,11 +18,11 @@ public function __construct(string $environment)
1718
public function collect(HealthCollection $collection): void
1819
{
1920
if ($this->environment !== 'prod') {
20-
$collection->add(HealthResult::error('frosh-tools.checker.not-prod'));
21+
$collection->add(SettingsResult::error('frosh-tools.checker.not-prod'));
2122

2223
return;
2324
}
2425

25-
$collection->add(HealthResult::ok('frosh-tools.checker.prodGood'));
26+
$collection->add(SettingsResult::ok('frosh-tools.checker.prodGood'));
2627
}
2728
}
Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
11
<?php declare(strict_types=1);
22

3-
namespace Frosh\Tools\Components\Health\Checker;
3+
namespace Frosh\Tools\Components\Health\Checker\HealthChecker;
44

55
use Doctrine\DBAL\Connection;
6+
use Frosh\Tools\Components\Health\Checker\CheckerInterface;
67
use Frosh\Tools\Components\Health\HealthCollection;
7-
use Frosh\Tools\Components\Health\HealthResult;
8+
use Frosh\Tools\Components\Health\SettingsResult;
89

910
class QueueChecker implements CheckerInterface
1011
{
11-
/**
12-
* @var Connection
13-
*/
14-
private $connection;
12+
private Connection $connection;
1513

1614
public function __construct(Connection $connection)
1715
{
@@ -20,22 +18,18 @@ public function __construct(Connection $connection)
2018

2119
public function collect(HealthCollection $collection): void
2220
{
23-
$oldestMessage = (int) $this->connection->fetchOne('SELECT IFNULL(MIN(published_at), 0) FROM enqueue');
24-
25-
if ($oldestMessage === 0) {
26-
$collection->add(HealthResult::ok('frosh-tools.checker.queuesGood'));
27-
28-
return;
29-
}
21+
$result = SettingsResult::ok('frosh-tools.checker.queuesGood');
3022

23+
$oldestMessage = (int) $this->connection->fetchOne('SELECT IFNULL(MIN(published_at), 0) FROM enqueue');
3124
$oldestMessage /= 10000;
3225
$minutes = 15;
3326

3427
// When the oldest message is older then $minutes minutes
3528
if (($oldestMessage + ($minutes * 60)) < time()) {
36-
$collection->add(HealthResult::warning('frosh-tools.checker.queuesWarning', ['minutes' => $minutes]));
37-
} else {
38-
$collection->add(HealthResult::ok('frosh-tools.checker.queuesGood'));
29+
$result = SettingsResult::warning('frosh-tools.checker.queuesWarning');
3930
}
31+
32+
$result->url = 'https://developer.shopware.com/docs/guides/hosting/infrastructure/message-queue';;
33+
$collection->add($result);
4034
}
4135
}

src/Components/Health/Checker/TaskChecker.php renamed to src/Components/Health/Checker/HealthChecker/TaskChecker.php

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
<?php declare(strict_types=1);
22

3-
namespace Frosh\Tools\Components\Health\Checker;
3+
namespace Frosh\Tools\Components\Health\Checker\HealthChecker;
44

5+
use Frosh\Tools\Components\Health\Checker\CheckerInterface;
56
use Frosh\Tools\Components\Health\HealthCollection;
6-
use Frosh\Tools\Components\Health\HealthResult;
7+
use Frosh\Tools\Components\Health\SettingsResult;
78
use Shopware\Core\Framework\Context;
89
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
910
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
@@ -14,10 +15,7 @@
1415

1516
class TaskChecker implements CheckerInterface
1617
{
17-
/**
18-
* @var EntityRepositoryInterface
19-
*/
20-
private $scheduledTaskRepository;
18+
private EntityRepositoryInterface $scheduledTaskRepository;
2119

2220
public function __construct(EntityRepositoryInterface $scheduledTaskRepository)
2321
{
@@ -39,7 +37,7 @@ public function collect(HealthCollection $collection): void
3937
)
4038
);
4139
$criteria->addFilter(new NotFilter(
42-
NotFilter::CONNECTION_AND,
40+
NotFilter::CONNECTION_AND,
4341
[
4442
new EqualsFilter('status', ScheduledTaskDefinition::STATUS_INACTIVE)
4543
]
@@ -49,11 +47,11 @@ public function collect(HealthCollection $collection): void
4947
->searchIds($criteria, Context::createDefaultContext())->getIds();
5048

5149
if (count($oldTasks) === 0) {
52-
$collection->add(HealthResult::ok('frosh-tools.checker.scheduledTaskGood'));
50+
$collection->add(SettingsResult::ok('frosh-tools.checker.scheduledTaskGood'));
5351

5452
return;
5553
}
5654

57-
$collection->add(HealthResult::warning('frosh-tools.checker.scheduledTaskWarning', ['minutes' => $minutes]));
55+
$collection->add(SettingsResult::warning('frosh-tools.checker.scheduledTaskWarning'));
5856
}
5957
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Frosh\Tools\Components\Health\Checker\PerformanceChecker;
4+
5+
use Frosh\Tools\Components\Health\Checker\CheckerInterface;
6+
use Frosh\Tools\Components\Health\HealthCollection;
7+
use Frosh\Tools\Components\Health\SettingsResult;
8+
9+
class AdminWorkerChecker implements CheckerInterface
10+
{
11+
private bool $adminWorkerEnabled;
12+
13+
public function __construct(bool $adminWorkerEnabled)
14+
{
15+
$this->adminWorkerEnabled = $adminWorkerEnabled;
16+
}
17+
18+
public function collect(HealthCollection $collection): void
19+
{
20+
if ($this->adminWorkerEnabled) {
21+
$collection->add(
22+
SettingsResult::warning('frosh-tools.checker.adminWorkerWarning',
23+
'enabled',
24+
'disabled',
25+
'https://developer.shopware.com/docs/guides/plugins/plugins/framework/message-queue/add-message-handler#the-admin-worker'
26+
)
27+
);
28+
29+
return;
30+
}
31+
32+
$collection->add(
33+
SettingsResult::ok('frosh-tools.checker.adminWorkerGood',
34+
'disabled',
35+
'disabled'
36+
)
37+
);
38+
}
39+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Frosh\Tools\Components\Health\Checker\PerformanceChecker;
4+
5+
use Frosh\Tools\Components\Elasticsearch\ElasticsearchManager;
6+
use Frosh\Tools\Components\Health\Checker\CheckerInterface;
7+
use Frosh\Tools\Components\Health\HealthCollection;
8+
use Frosh\Tools\Components\Health\SettingsResult;
9+
10+
class EsChecker implements CheckerInterface
11+
{
12+
protected bool $esEnabled = false;
13+
14+
public function __construct(ElasticsearchManager $elasticsearchManager)
15+
{
16+
$this->esEnabled = $elasticsearchManager->isEnabled();
17+
}
18+
19+
public function collect(HealthCollection $collection): void
20+
{
21+
22+
if (!$this->esEnabled) {
23+
$collection->add(
24+
SettingsResult::warning('frosh-tools.checker.esWarning',
25+
'disabled',
26+
'enabled',
27+
'https://developer.shopware.com/docs/guides/hosting/infrastructure/infrastructure/elasticsearch-setup'
28+
)
29+
);
30+
31+
return;
32+
}
33+
34+
$collection->add(
35+
SettingsResult::ok('frosh-tools.checker.esGood',
36+
'enabled',
37+
'enabled'
38+
)
39+
);
40+
}
41+
}

0 commit comments

Comments
 (0)