Skip to content

Commit

Permalink
Merge pull request #40 from vormkracht10/feature/notification-timeout
Browse files Browse the repository at this point in the history
add timeout for notifications on a per check basis.
  • Loading branch information
markvaneijk authored Oct 10, 2023
2 parents cb29f52 + 28ca98c commit 02ea0ee
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 12 deletions.
6 changes: 5 additions & 1 deletion config/ok.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,16 @@

return [
'notifications' => [
'enabled' => env('LARAVEL_OK_NOTIFICATIONS_ENABLED', true),
'enabled' => env('OK_NOTIFICATIONS_ENABLED', true),

'failed_notification' => CheckFailedNotification::class,

'notifiable' => Notifiable::class,

'interval_in_minutes' => env('OK_NOTIFICATION_INTERVAL', 60 * 60 * 24),

'cache_driver' => env('OK_CACHE_DRIVER', 'file'),

'via' => [
// 'discord' => [
// 'channel' => 123456790,
Expand Down
30 changes: 26 additions & 4 deletions src/Checks/Base/Check.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ abstract class Check

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

protected int $repeatSeconds;

protected int $notificationIntervalInMinutes;

protected ?string $name = null;

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

protected int $timesToFailWithoutNotification = 1;

public function __construct()
{
}

public static function config(): static
{
$instance = app(static::class);
Expand All @@ -47,6 +47,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 +118,16 @@ public function markAsCrashed(): Result
{
return new Result(Status::CRASHED);
}

public function getNotificationInterval(): int
{
return $this->notificationIntervalInMinutes ?? config('ok.notifications.interval_in_minutes');
}

public function setNotificationInterval(int $intervalInMinutes): static
{
$this->notificationIntervalInMinutes = $intervalInMinutes;

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
46 changes: 44 additions & 2 deletions src/Listeners/SendCheckFailedNotification.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,60 @@

namespace Vormkracht10\LaravelOK\Listeners;

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

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);

$failedNotificationClass = config('ok.notifications.failed_notification');

$notification = (new $failedNotificationClass($event->check, $event->result));
$notification = new $failedNotificationClass($event->check, $event->result);

$notifiable->notify($notification);

$this->setNotificationTimestamp($check);

Check failure on line 28 in src/Listeners/SendCheckFailedNotification.php

View workflow job for this annotation

GitHub Actions / phpstan

Call to an undefined method Vormkracht10\LaravelOK\Listeners\SendCheckFailedNotification::setNotificationTimestamp().

Check failure on line 28 in src/Listeners/SendCheckFailedNotification.php

View workflow job for this annotation

GitHub Actions / phpstan

Undefined variable: $check
}

protected function setNotificationTime(Check $check): int
{
return Cache::driver('file')->forever(

Check failure on line 33 in src/Listeners/SendCheckFailedNotification.php

View workflow job for this annotation

GitHub Actions / phpstan

Method Vormkracht10\LaravelOK\Listeners\SendCheckFailedNotification::setNotificationTime() should return int but returns bool.
'laravel-ok::notifications::'.$check::class,
now()->getTimestamp(),
);
}

protected function getLastNotifiedTime(Check $check): ?Carbon
{
$timestamp = Cache::driver('file')
->get('laravel-ok::notifications::'.$check::class);

if (! $timestamp) {
return null;
}

return Carbon::createFromTimestamp($timestamp);
}

protected function shouldSendNotification(Check $check): bool
{
$lastNotified = $this->getLastNotifiedTime($check);

if (! $lastNotified) {
return true;
}

return now() >= $lastNotified->addMinutes($check->getNotificationInterval());
}
}

0 comments on commit 02ea0ee

Please sign in to comment.