fix(notifications): send a disk usage alert again after usage drops

The high disk usage throttle stayed for 24 hours after usage dropped,
so a new spike in that time sent no alert.

- Release the throttle when disk usage is at or below the threshold
This commit is contained in:
Andras Bacsai
2026-10-05 21:24:32 +02:00
parent e09876adcd
commit 8564741d38
2 changed files with 23 additions and 0 deletions
+4
View File
@@ -2,6 +2,7 @@
namespace App\Jobs;
use App\Models\NotificationThrottle;
use App\Models\Server;
use App\Notifications\Server\HighDiskUsage;
use Illuminate\Bus\Queueable;
@@ -59,6 +60,9 @@ class ServerStorageCheckJob implements ShouldBeEncrypted, ShouldQueue, Silenced
}
if ($this->percentage > $serverDiskUsageNotificationThreshold) {
$team->notify(new HighDiskUsage($this->server, $this->percentage, $serverDiskUsageNotificationThreshold));
} else {
// Usage recovered: the next spike should alert again instead of waiting for the interval.
NotificationThrottle::release($this->server, HighDiskUsage::class);
}
} catch (\Throwable $e) {
return handleError($e);
@@ -67,6 +67,25 @@ it('sends one high disk usage notification per configured interval', function ()
Notification::assertSentToTimes($server->team, HighDiskUsage::class, 2);
});
it('notifies again on a new spike after disk usage recovered within the interval', function () {
$server = diskUsageIntervalServer(intervalHours: 24);
runDiskUsageCheck($server, 93);
Notification::assertSentToTimes($server->team, HighDiskUsage::class, 1);
Carbon::setTestNow(now()->addMinutes(10));
runDiskUsageCheck($server, 95);
Notification::assertSentToTimes($server->team, HighDiskUsage::class, 1);
Carbon::setTestNow(now()->addHour());
runDiskUsageCheck($server, 40);
expect(NotificationThrottle::wasSent($server, HighDiskUsage::class))->toBeFalse();
Carbon::setTestNow(now()->addHour());
runDiskUsageCheck($server, 92);
Notification::assertSentToTimes($server->team, HighDiskUsage::class, 2);
});
it('defaults to one notification per 24 hours', function () {
$team = Team::factory()->create();
$server = Server::factory()->create(['team_id' => $team->id]);