Skip to content

Commit

Permalink
Merge pull request #139 from stfndamjanovic/main
Browse files Browse the repository at this point in the history
Running checks conditionally
  • Loading branch information
freekmurze authored Dec 14, 2022
2 parents 97cbfa4 + b4daec4 commit a536042
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 0 deletions.
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

0 comments on commit a536042

Please sign in to comment.