Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Running checks conditionally #139

Merged
merged 4 commits into from
Dec 14, 2022
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
15 changes: 15 additions & 0 deletions docs/viewing-results/general.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,18 @@ Health::checks([
UsedDiskSpaceCheck::new()->label('Disk space on main disk'),
]);
```

## Running checks conditionally

If you would like to conditionally run a check, you can use the `if` and `unless` methods.

```php
use Spatie\Health\Facades\Health;
use Spatie\Health\Checks\Checks\DebugModeCheck;
use Spatie\Health\Checks\Checks\RedisCheck;

Health::checks([
DebugModeCheck::new()->if(app()->isProduction()),
RedisCheck::new()->unless(app()->environment('development')),
]);
```
20 changes: 20 additions & 0 deletions src/Checks/Check.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ abstract class Check

protected ?string $label = null;

protected bool $shouldRun = true;

final public function __construct()
{
}
Expand Down Expand Up @@ -69,11 +71,29 @@ public function getName(): string

public function shouldRun(): bool
{
if (! $this->shouldRun) {
return false;
}

$date = Date::now();

return (new CronExpression($this->expression))->isDue($date->toDateTimeString());
}

public function if(bool $condition)
{
$this->shouldRun = $condition;

return $this;
}

public function unless(bool $condition)
{
$this->shouldRun = !$condition;

return $this;
}

abstract public function run(): Result;

public function markAsCrashed(): Result
Expand Down
66 changes: 66 additions & 0 deletions tests/HealthTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
use Spatie\Health\Exceptions\InvalidCheck;
use Spatie\Health\Facades\Health;
use Spatie\Health\Testing\FakeCheck;
use Spatie\Health\Checks\Checks\DebugModeCheck;
use Spatie\Health\Checks\Check;

it('can register checks', function () {
Health::checks([
Expand All @@ -22,6 +24,70 @@
->toBeInstanceOf(UsedDiskSpaceCheck::class);
});

it('can run checks conditionally using if method', function () {
Health::checks([
UsedDiskSpaceCheck::new(),
DebugModeCheck::new()->if(false),
]);

$checks = Health::registeredChecks()->filter(function (Check $check) {
return $check->shouldRun();
});

expect($checks)
->toHaveCount(1)
->and($checks->first())
->toBeInstanceOf(UsedDiskSpaceCheck::class);

Health::clearChecks();

Health::checks([
UsedDiskSpaceCheck::new(),
DebugModeCheck::new()->if(true),
]);

$checks = Health::registeredChecks()->filter(function (Check $check) {
return $check->shouldRun();
});

expect($checks)
->toHaveCount(2)
->and($checks[1])
->toBeInstanceOf(DebugModeCheck::class);
});

it('can run checks conditionally using unless method', function () {
Health::checks([
UsedDiskSpaceCheck::new(),
DebugModeCheck::new()->unless(true),
]);

$checks = Health::registeredChecks()->filter(function (Check $check) {
return $check->shouldRun();
});

expect($checks)
->toHaveCount(1)
->and($checks->first())
->toBeInstanceOf(UsedDiskSpaceCheck::class);

Health::clearChecks();

Health::checks([
UsedDiskSpaceCheck::new(),
DebugModeCheck::new()->unless(false),
]);

$checks = Health::registeredChecks()->filter(function (Check $check) {
return $check->shouldRun();
});

expect($checks)
->toHaveCount(2)
->and($checks[1])
->toBeInstanceOf(DebugModeCheck::class);
});

it('will throw an exception when duplicate checks are registered', function () {
Health::checks([
PingCheck::new(),
Expand Down