diff --git a/docs/viewing-results/general.md b/docs/viewing-results/general.md index e769c235..a8943cd9 100644 --- a/docs/viewing-results/general.md +++ b/docs/viewing-results/general.md @@ -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')), +]); +``` diff --git a/src/Checks/Check.php b/src/Checks/Check.php index 84aa3c75..432a5ee5 100755 --- a/src/Checks/Check.php +++ b/src/Checks/Check.php @@ -18,6 +18,8 @@ abstract class Check protected ?string $label = null; + protected bool $shouldRun = true; + final public function __construct() { } @@ -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 diff --git a/tests/HealthTest.php b/tests/HealthTest.php index 6f4683db..6733c918 100644 --- a/tests/HealthTest.php +++ b/tests/HealthTest.php @@ -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([ @@ -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(),