mirror of
https://github.com/tiennm99/coolify.git
synced 2026-10-16 20:14:08 +00:00
refactor: send immediate Traefik version notifications instead of delayed aggregation
Move notification logic from NotifyOutdatedTraefikServersJob into CheckTraefikVersionForServerJob to send immediate notifications when outdated Traefik is detected. This is more suitable for cloud environments with thousands of servers. Changes: - CheckTraefikVersionForServerJob now sends notifications immediately after detecting outdated Traefik - Remove NotifyOutdatedTraefikServersJob (no longer needed) - Remove delay calculation logic from CheckTraefikVersionJob - Update tests to reflect new immediate notification pattern Trade-offs: - Pro: Faster notifications (immediate alerts) - Pro: Simpler codebase (removed complex delay calculation) - Pro: Better scalability for thousands of servers - Con: Teams may receive multiple notifications if they have many outdated servers 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
1 parent
0a62739b11
commit
50d55a9509
5 files changed
+56
-261
No files matched your search
@@ -3,6 +3,7 @@
|
||||
namespace App\Jobs;
|
||||
|
||||
use App\Models\Server;
|
||||
use App\Notifications\Server\TraefikVersionOutdated;
|
||||
use Illuminate\Bus\Queueable;
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
use Illuminate\Foundation\Bus\Dispatchable;
|
||||
@@ -126,7 +127,7 @@ class CheckTraefikVersionForServerJob implements ShouldQueue
|
||||
}
|
||||
|
||||
/**
|
||||
* Store outdated information in database.
|
||||
* Store outdated information in database and send immediate notification.
|
||||
*/
|
||||
private function storeOutdatedInfo(string $current, string $latest, string $type, ?string $upgradeTarget = null, ?array $newerBranchInfo = null): void
|
||||
{
|
||||
@@ -149,5 +150,24 @@ class CheckTraefikVersionForServerJob implements ShouldQueue
|
||||
}
|
||||
|
||||
$this->server->update(['traefik_outdated_info' => $outdatedInfo]);
|
||||
|
||||
// Send immediate notification to the team
|
||||
$this->sendNotification($outdatedInfo);
|
||||
}
|
||||
|
||||
/**
|
||||
* Send notification to team about outdated Traefik.
|
||||
*/
|
||||
private function sendNotification(array $outdatedInfo): void
|
||||
{
|
||||
// Attach the outdated info as a dynamic property for the notification
|
||||
$this->server->outdatedInfo = $outdatedInfo;
|
||||
|
||||
// Get the team and send notification
|
||||
$team = $this->server->team()->first();
|
||||
|
||||
if ($team) {
|
||||
$team->notify(new TraefikVersionOutdated(collect([$this->server])));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -32,65 +32,14 @@ class CheckTraefikVersionJob implements ShouldQueue
|
||||
->whereRelation('settings', 'is_usable', true)
|
||||
->get();
|
||||
|
||||
$serverCount = $servers->count();
|
||||
|
||||
if ($serverCount === 0) {
|
||||
if ($servers->isEmpty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Dispatch individual server check jobs in parallel
|
||||
// Each job will send immediate notifications when outdated Traefik is detected
|
||||
foreach ($servers as $server) {
|
||||
CheckTraefikVersionForServerJob::dispatch($server, $traefikVersions);
|
||||
}
|
||||
|
||||
// Dispatch notification job with delay to allow server checks to complete
|
||||
// Jobs run in parallel via queue workers, but we need to account for:
|
||||
// - Queue worker capacity (workers process jobs concurrently)
|
||||
// - Job timeout (60s per server check)
|
||||
// - Retry attempts (3 retries with exponential backoff)
|
||||
// - Network latency and SSH connection overhead
|
||||
//
|
||||
// Calculation strategy:
|
||||
// - Assume ~10-20 workers processing the high queue
|
||||
// - Each server check takes up to 60s (timeout)
|
||||
// - With retries, worst case is ~180s per job
|
||||
// - More conservative: 0.2s per server (instead of 0.1s)
|
||||
// - Higher minimum: 120s (instead of 60s) to account for retries
|
||||
// - Keep 300s maximum to avoid excessive delays
|
||||
$delaySeconds = $this->calculateNotificationDelay($serverCount);
|
||||
if (isDev()) {
|
||||
$delaySeconds = 1;
|
||||
}
|
||||
NotifyOutdatedTraefikServersJob::dispatch()->delay(now()->addSeconds($delaySeconds));
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculate the delay in seconds before sending notifications.
|
||||
*
|
||||
* This method calculates an appropriate delay to allow all parallel
|
||||
* CheckTraefikVersionForServerJob instances to complete before sending
|
||||
* notifications to teams.
|
||||
*
|
||||
* The calculation considers:
|
||||
* - Server count (more servers = longer delay)
|
||||
* - Queue worker capacity
|
||||
* - Job timeout (60s) and retry attempts (3x)
|
||||
* - Network latency and SSH connection overhead
|
||||
*
|
||||
* @param int $serverCount Number of servers being checked
|
||||
* @return int Delay in seconds
|
||||
*/
|
||||
protected function calculateNotificationDelay(int $serverCount): int
|
||||
{
|
||||
$minDelay = config('constants.server_checks.notification_delay_min');
|
||||
$maxDelay = config('constants.server_checks.notification_delay_max');
|
||||
$scalingFactor = config('constants.server_checks.notification_delay_scaling');
|
||||
|
||||
// Calculate delay based on server count
|
||||
// More conservative approach: 0.2s per server
|
||||
$calculatedDelay = (int) ($serverCount * $scalingFactor);
|
||||
|
||||
// Apply min/max boundaries
|
||||
return min($maxDelay, max($minDelay, $calculatedDelay));
|
||||
}
|
||||
}
|
||||
@@ -1,68 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Jobs;
|
||||
|
||||
use App\Enums\ProxyTypes;
|
||||
use App\Models\Server;
|
||||
use App\Models\Team;
|
||||
use App\Notifications\Server\TraefikVersionOutdated;
|
||||
use Illuminate\Bus\Queueable;
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
use Illuminate\Foundation\Bus\Dispatchable;
|
||||
use Illuminate\Queue\InteractsWithQueue;
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
|
||||
class NotifyOutdatedTraefikServersJob implements ShouldQueue
|
||||
{
|
||||
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
||||
|
||||
public $tries = 3;
|
||||
|
||||
/**
|
||||
* Create a new job instance.
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
$this->onQueue('high');
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the job.
|
||||
*/
|
||||
public function handle(): void
|
||||
{
|
||||
// Query servers that have outdated info stored
|
||||
$servers = Server::whereNotNull('proxy')
|
||||
->whereProxyType(ProxyTypes::TRAEFIK->value)
|
||||
->whereRelation('settings', 'is_reachable', true)
|
||||
->whereRelation('settings', 'is_usable', true)
|
||||
->get();
|
||||
|
||||
$outdatedServers = collect();
|
||||
|
||||
foreach ($servers as $server) {
|
||||
if ($server->traefik_outdated_info) {
|
||||
// Attach the outdated info as a dynamic property for the notification
|
||||
$server->outdatedInfo = $server->traefik_outdated_info;
|
||||
$outdatedServers->push($server);
|
||||
}
|
||||
}
|
||||
|
||||
if ($outdatedServers->isEmpty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Group by team and send notifications
|
||||
$serversByTeam = $outdatedServers->groupBy('team_id');
|
||||
|
||||
foreach ($serversByTeam as $teamId => $teamServers) {
|
||||
$team = Team::find($teamId);
|
||||
if (! $team) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Send one notification per team with all outdated servers
|
||||
$team->notify(new TraefikVersionOutdated($teamServers));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user