generated from spatie/package-skeleton-laravel
-
-
Notifications
You must be signed in to change notification settings - Fork 159
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #255 from faustoFF/add-pause-resume-commands
Add pause and resume commands for health checks
- Loading branch information
Showing
11 changed files
with
217 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
--- | ||
title: Pausing and resuming checks | ||
weight: 5 | ||
--- | ||
|
||
You might want to temporarily pause checks to avoid unnecessary alerts during deployments or maintenance. This is particularly useful when services might experience brief downtime, which could otherwise trigger false alarms. | ||
|
||
## Pause Checks | ||
|
||
You can pause checks for a specified duration. By default, checks will be paused for 300 seconds (5 minutes). | ||
|
||
```bash | ||
# Pause checks for the default duration (300 seconds) | ||
php artisan health:pause | ||
|
||
# Pause checks for a custom duration (in seconds) | ||
php artisan health:pause 60 | ||
``` | ||
|
||
During the pause period, checks will not run, and no alerts will be triggered. | ||
|
||
## Resume Checks | ||
|
||
If you need to resume checks before the pause duration ends, you can do so with the following command: | ||
|
||
```bash | ||
# Resume checks immediately | ||
php artisan health:resume | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
namespace Spatie\Health\Commands; | ||
|
||
use Illuminate\Console\Command; | ||
use Illuminate\Support\Facades\Cache; | ||
|
||
class PauseHealthChecksCommand extends Command | ||
{ | ||
public const CACHE_KEY = 'health_paused'; | ||
public const DEFAULT_TTL = 300; | ||
|
||
protected $signature = 'health:pause {seconds=' . self::DEFAULT_TTL . '}'; | ||
protected $description = 'Pause all health checks for the giving time'; | ||
|
||
public function handle(): int | ||
{ | ||
$seconds = (int) $this->argument('seconds'); | ||
|
||
Cache::put(self::CACHE_KEY, true, $seconds); | ||
|
||
$this->comment('All health check paused until ' . now()->addSeconds($seconds)->toDateTimeString()); | ||
|
||
return self::SUCCESS; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
namespace Spatie\Health\Commands; | ||
|
||
use Illuminate\Console\Command; | ||
use Illuminate\Support\Facades\Cache; | ||
|
||
class ResumeHealthChecksCommand extends Command | ||
{ | ||
protected $signature = 'health:resume'; | ||
protected $description = 'Resume all health checks'; | ||
|
||
public function handle(): int | ||
{ | ||
Cache::forget(PauseHealthChecksCommand::CACHE_KEY); | ||
|
||
$this->comment('All health check resumed'); | ||
|
||
return self::SUCCESS; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php | ||
|
||
use Illuminate\Contracts\Cache\Repository; | ||
use Illuminate\Support\Facades\Cache; | ||
use Spatie\Health\Commands\PauseHealthChecksCommand; | ||
|
||
use function Pest\Laravel\artisan; | ||
|
||
it('sets cache value to true for default ttl', function () { | ||
$mockRepository = Mockery::mock(Repository::class); | ||
|
||
$mockRepository->shouldReceive('put') | ||
->once() | ||
->with( | ||
PauseHealthChecksCommand::CACHE_KEY, | ||
true, | ||
PauseHealthChecksCommand::DEFAULT_TTL | ||
) | ||
->andReturn(true); | ||
|
||
Cache::swap($mockRepository); | ||
|
||
Cache::shouldReceive('driver')->andReturn($mockRepository); | ||
|
||
artisan(PauseHealthChecksCommand::class) | ||
->assertSuccessful() | ||
->expectsOutputToContain('All health check paused until'); | ||
}); | ||
|
||
it('sets cache value to true for custom ttl', function () { | ||
$mockRepository = Mockery::mock(Repository::class); | ||
|
||
$mockRepository->shouldReceive('put') | ||
->once() | ||
->with( | ||
PauseHealthChecksCommand::CACHE_KEY, | ||
true, | ||
60 | ||
) | ||
->andReturn(true); | ||
|
||
Cache::swap($mockRepository); | ||
|
||
Cache::shouldReceive('driver')->andReturn($mockRepository); | ||
|
||
artisan(PauseHealthChecksCommand::class, ['seconds' => '60']) | ||
->assertSuccessful() | ||
->expectsOutputToContain('All health check paused until'); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
use Illuminate\Contracts\Cache\Repository; | ||
use Illuminate\Support\Facades\Cache; | ||
use Spatie\Health\Commands\PauseHealthChecksCommand; | ||
|
||
use Spatie\Health\Commands\ResumeHealthChecksCommand; | ||
use function Pest\Laravel\artisan; | ||
|
||
it('forgets cache value', function () { | ||
$mockRepository = Mockery::mock(Repository::class); | ||
|
||
$mockRepository->shouldReceive('forget') | ||
->once() | ||
->with(PauseHealthChecksCommand::CACHE_KEY) | ||
->andReturn(true); | ||
|
||
Cache::swap($mockRepository); | ||
|
||
Cache::shouldReceive('driver')->andReturn($mockRepository); | ||
|
||
artisan(ResumeHealthChecksCommand::class) | ||
->assertSuccessful() | ||
->expectsOutput('All health check resumed') | ||
; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
..._/SimpleHealthCheckControllerTest__it_does_not_perform_checks_if_checks_are_paused__1.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
healthy: true |