Skip to content

Commit

Permalink
add timeout for notifications on a per check basis.
Browse files Browse the repository at this point in the history
  • Loading branch information
david-d-h committed Oct 9, 2023
1 parent e3cea66 commit 141b0be
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 10 deletions.
31 changes: 27 additions & 4 deletions src/Checks/Base/Check.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Vormkracht10\LaravelOK\Checks\Base;

use Carbon\Carbon;
use Cron\CronExpression;
use Illuminate\Console\Scheduling\ManagesFrequencies;
use Illuminate\Support\Facades\Date;
Expand All @@ -24,6 +25,10 @@ abstract class Check

protected string $expression = '* * * * *';

protected int $repeatSeconds;

protected Carbon $reportTimeout;

protected ?string $name = null;

protected ?string $message = null;
Expand All @@ -34,10 +39,6 @@ abstract class Check

protected int $timesToFailWithoutNotification = 1;

public function __construct()
{
}

public static function config(): static
{
$instance = app(static::class);
Expand All @@ -47,6 +48,16 @@ public static function config(): static
return $instance;
}

public function getExpression(): string
{
return $this->expression;
}

public function getRepeatSeconds(): int
{
return $this->repeatSeconds;
}

public function name(string $name): self
{
$this->name = $name;
Expand Down Expand Up @@ -108,4 +119,16 @@ public function markAsCrashed(): Result
{
return new Result(Status::CRASHED);
}

public function getReportTimeout(): Carbon
{
return $this->reportTimeout;
}

public function reportTimeout(Carbon $minimumDelay): static
{
$this->reportTimeout = $minimumDelay;

return $this;
}
}
2 changes: 0 additions & 2 deletions src/Checks/PermissionCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ class PermissionCheck extends Check

public function __construct(array $configured = [])
{
parent::__construct();

$this->configured = $configured;
}

Expand Down
4 changes: 1 addition & 3 deletions src/Commands/RunChecksCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,7 @@ public function runChecks()
{
return app(OK::class)
->configuredChecks()
->map(function (mixed $check) {
return is_string($check) ? app($check) : $check;
})
->map(fn ($check) => is_string($check) ? app($check) : $check)
->map(function (Check $check): Result {
return $check->shouldRun()
? $this->runCheck($check)
Expand Down
27 changes: 26 additions & 1 deletion src/Listeners/SendCheckFailedNotification.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,19 @@

namespace Vormkracht10\LaravelOK\Listeners;

use Illuminate\Support\Facades\Cache;
use Vormkracht10\LaravelOK\Checks\Base\Check;
use Vormkracht10\LaravelOK\Events\CheckFailed;
use Vormkracht10\LaravelOK\Facades\OK;

class SendCheckFailedNotification
{
public function handle($event)
public function handle(CheckFailed $event)
{
if (! $this->shouldSendNotification($event->check)) {
return;
}

$notifiableClass = config('ok.notifications.notifiable');

$notifiable = app($notifiableClass);
Expand All @@ -15,5 +24,21 @@ public function handle($event)
$notification = (new $failedNotificationClass($event->check, $event->result));

$notifiable->notify($notification);

$class = $event->check::class;

Cache::driver('file')->set(
"laravel-ok::runs::{$class}",
now()->getTimestamp(),
);
}

protected function shouldSendNotification(Check $check): bool
{
$lastRun = OK::lastRun($check::class);

$timeout = $check->getReportTimeout();

return $lastRun < $timeout;
}
}
10 changes: 10 additions & 0 deletions src/OK.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

namespace Vormkracht10\LaravelOK;

use Carbon\Carbon;
use Illuminate\Support\Facades\Cache;

class OK
{
protected array $checks = [];
Expand All @@ -13,6 +16,13 @@ public function checks(array $checks): self
return $this;
}

public function lastRun(string $check): Carbon
{
$timestamp = Cache::driver('file')->get("laravel-ok::runs::{$check}");

return Carbon::createFromTimestamp($timestamp);
}

public function configuredChecks()
{
return collect($this->checks);
Expand Down

0 comments on commit 141b0be

Please sign in to comment.