Skip to content

Commit

Permalink
straighten up the notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
markvaneijk committed Oct 10, 2023
1 parent 7a7861f commit 28ca98c
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 25 deletions.
6 changes: 4 additions & 2 deletions config/ok.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@

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

'failed_notification' => CheckFailedNotification::class,

'notifiable' => Notifiable::class,

'interval' => now()->subMinutes(15),
'interval_in_minutes' => env('OK_NOTIFICATION_INTERVAL', 60 * 60 * 24),

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

'via' => [
// 'discord' => [
Expand Down
11 changes: 5 additions & 6 deletions src/Checks/Base/Check.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Vormkracht10\LaravelOK\Checks\Base;

use Carbon\Carbon;
use Cron\CronExpression;
use Illuminate\Console\Scheduling\ManagesFrequencies;
use Illuminate\Support\Facades\Date;
Expand All @@ -27,7 +26,7 @@ abstract class Check

protected int $repeatSeconds;

protected Carbon $reportInterval;
protected int $notificationIntervalInMinutes;

protected ?string $name = null;

Expand Down Expand Up @@ -120,14 +119,14 @@ public function markAsCrashed(): Result
return new Result(Status::CRASHED);
}

public function getReportInterval(): Carbon
public function getNotificationInterval(): int
{
return $this->reportInterval ?? config('ok.notifications.interval', Carbon::now()->subMinutes(30));
return $this->notificationIntervalInMinutes ?? config('ok.notifications.interval_in_minutes');
}

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

return $this;
}
Expand Down
31 changes: 24 additions & 7 deletions src/Listeners/SendCheckFailedNotification.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

namespace Vormkracht10\LaravelOK\Listeners;

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

class SendCheckFailedNotification
{
Expand All @@ -25,20 +25,37 @@ public function handle(CheckFailed $event)

$notifiable->notify($notification);

$class = $event->check::class;
$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
}

Cache::driver('file')->set(
"laravel-ok::runs::{$class}",
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
{
$lastRun = OK::lastRun($check::class);
$lastNotified = $this->getLastNotifiedTime($check);

$interval = $check->getReportInterval();
if (! $lastNotified) {
return true;
}

return $lastRun < $interval;
return now() >= $lastNotified->addMinutes($check->getNotificationInterval());
}
}
10 changes: 0 additions & 10 deletions src/OK.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@

namespace Vormkracht10\LaravelOK;

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

class OK
{
protected array $checks = [];
Expand All @@ -16,13 +13,6 @@ 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 28ca98c

Please sign in to comment.