mirror of
https://github.com/coollabsio/coolify.git
synced 2026-11-14 08:42:38 -04:00
High disk usage notifications were sent every ~10 minutes on Coolify Cloud because the cache rate limiter did not hold across Sentinel-triggered checks. - Add a notification_throttles table and NotificationThrottle helper that atomically records when a notification was sent for a resource. - Add the ThrottledNotification contract; Team::notify() skips a throttled notification that was already sent within its interval. - High disk usage: configurable per-server interval (server_disk_usage_notification_interval_hours, default 24h) in Server > Advanced and the servers API. - Container restarted (coolify-proxy, database TCP proxy): at most one notification per hour per server/database during a restart loop. - Move the unreachable and missing-backup markers into the new table and migrate existing data (servers.unreachable_notification_sent and scheduled_database_backups.missing_backup_notification_sent_at). - Regenerate OpenAPI files.
112 lines
3.2 KiB
PHP
112 lines
3.2 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Contracts\ThrottledNotification;
|
|
use Carbon\CarbonInterface;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\MorphTo;
|
|
|
|
/**
|
|
* Records when a notification was last sent for a resource, so repeated checks
|
|
* (across queue workers) do not send the same notification again.
|
|
*/
|
|
class NotificationThrottle extends Model
|
|
{
|
|
public $timestamps = false;
|
|
|
|
protected $fillable = [
|
|
'notifiable_type',
|
|
'notifiable_id',
|
|
'notification',
|
|
'sent_at',
|
|
];
|
|
|
|
protected function casts(): array
|
|
{
|
|
return [
|
|
'sent_at' => 'datetime',
|
|
];
|
|
}
|
|
|
|
public function notifiable(): MorphTo
|
|
{
|
|
return $this->morphTo();
|
|
}
|
|
|
|
/**
|
|
* Atomically reserve the right to send a notification.
|
|
*
|
|
* Returns true when the notification was never sent, or when it was last sent
|
|
* before $sentBefore. Without $sentBefore, it is sent only once until released.
|
|
*/
|
|
public static function claim(Model $notifiable, string $notification, ?CarbonInterface $sentBefore = null): bool
|
|
{
|
|
if ($sentBefore !== null) {
|
|
$renewed = static::query()
|
|
->whereMorphedTo('notifiable', $notifiable)
|
|
->where('notification', $notification)
|
|
->where('sent_at', '<', $sentBefore)
|
|
->update(['sent_at' => now()]);
|
|
|
|
if ($renewed === 1) {
|
|
return true;
|
|
}
|
|
}
|
|
|
|
return static::query()->insertOrIgnore([
|
|
'notifiable_type' => $notifiable->getMorphClass(),
|
|
'notifiable_id' => $notifiable->getKey(),
|
|
'notification' => $notification,
|
|
'sent_at' => now(),
|
|
]) === 1;
|
|
}
|
|
|
|
/**
|
|
* Claim the throttle a notification declares. Notifications without a throttle subject are always allowed.
|
|
*/
|
|
public static function claimFor(ThrottledNotification $notification): bool
|
|
{
|
|
$subject = $notification->throttleSubject();
|
|
if ($subject === null) {
|
|
return true;
|
|
}
|
|
|
|
return static::claim($subject, $notification::class, now()->subMinutes($notification->throttleIntervalMinutes()));
|
|
}
|
|
|
|
/**
|
|
* Mark a notification as sent without sending it.
|
|
*/
|
|
public static function record(Model $notifiable, string $notification): void
|
|
{
|
|
static::query()->updateOrInsert(
|
|
[
|
|
'notifiable_type' => $notifiable->getMorphClass(),
|
|
'notifiable_id' => $notifiable->getKey(),
|
|
'notification' => $notification,
|
|
],
|
|
['sent_at' => now()],
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Forget a sent notification. Returns true when a record was removed.
|
|
*/
|
|
public static function release(Model $notifiable, string $notification): bool
|
|
{
|
|
return static::query()
|
|
->whereMorphedTo('notifiable', $notifiable)
|
|
->where('notification', $notification)
|
|
->delete() > 0;
|
|
}
|
|
|
|
public static function wasSent(Model $notifiable, string $notification): bool
|
|
{
|
|
return static::query()
|
|
->whereMorphedTo('notifiable', $notifiable)
|
|
->where('notification', $notification)
|
|
->exists();
|
|
}
|
|
}
|