Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app/Console/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ protected function schedule(Schedule $schedule): void
$this->scheduleInstance->job(new CheckTraefikVersionJob)->weekly()->sundays()->at('00:00')->timezone($this->instanceTimezone)->onOneServer();

$this->scheduleInstance->command('cleanup:database --yes')->daily();
$this->scheduleInstance->command('queue:prune-batches --hours=48')->daily();
$this->scheduleInstance->command('uploads:clear')->everyTwoMinutes();

// Cleanup orphaned PR preview containers daily
Expand Down
25 changes: 8 additions & 17 deletions app/Jobs/CheckTraefikVersionForServerJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use App\Events\ProxyStatusChangedUI;
use App\Models\Server;
use App\Notifications\Server\TraefikVersionOutdated;
use Illuminate\Bus\Batchable;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeEncrypted;
use Illuminate\Contracts\Queue\ShouldQueue;
Expand All @@ -14,7 +15,7 @@

class CheckTraefikVersionForServerJob implements ShouldBeEncrypted, ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
use Batchable, Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

public $tries = 3;

Expand Down Expand Up @@ -142,7 +143,7 @@ private function getNewerBranchInfo(string $currentBranch): ?array
}

/**
* Store outdated information in database and send immediate notification.
* Store outdated information in database.
*/
private function storeOutdatedInfo(string $current, string $latest, string $type, ?string $upgradeTarget = null, ?array $newerBranchInfo = null): void
{
Expand All @@ -166,23 +167,13 @@ private function storeOutdatedInfo(string $current, string $latest, string $type

$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
// Send immediate notification to channels that have bundling disabled
$team = $this->server->team()->first();

if ($team) {
$team->notify(new TraefikVersionOutdated(collect([$this->server])));
$unbundledChannels = $team->getEnabledChannels('traefik_outdated', unbundledOnly: true);
if (! empty($unbundledChannels)) {
$team->notify(new TraefikVersionOutdated(collect([$this->server]), unbundledOnly: true));
}
}
}
}
14 changes: 9 additions & 5 deletions app/Jobs/CheckTraefikVersionJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Bus;

class CheckTraefikVersionJob implements ShouldBeEncrypted, ShouldQueue
{
Expand Down Expand Up @@ -37,10 +38,13 @@ public function handle(): void
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);
}
$jobs = $servers->map(fn ($server) => new CheckTraefikVersionForServerJob($server, $traefikVersions));

Bus::batch($jobs)
->allowFailures()
->finally(fn () => SendTraefikOutdatedNotificationJob::dispatch())
->name('traefik-version-check')
->onQueue('high')
->dispatch();
}
}
57 changes: 57 additions & 0 deletions app/Jobs/SendPatchCheckNotificationJob.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

namespace App\Jobs;

use App\Models\Server;
use App\Notifications\Server\ServerPatchCheck;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeEncrypted;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;

class SendPatchCheckNotificationJob implements ShouldBeEncrypted, ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

public $tries = 3;

/**
* @param array<int> $serverIds Server IDs from the batch that triggered this job
*/
public function __construct(public array $serverIds = [])
{
$this->onQueue('high');
}

public function handle(): void
{
$query = Server::whereNotNull('patch_check_data')
->whereRelation('settings', 'is_reachable', true)
->with('team');

// Scope to specific servers from the batch to avoid race conditions
if (! empty($this->serverIds)) {
$query->whereIn('id', $this->serverIds);
}

$servers = $query->get();

if ($servers->isEmpty()) {
return;
}

$servers->groupBy('team_id')->each(function ($teamServers) {
$team = $teamServers->first()->team;
if (! $team) {
return;
}

$team->notify(new ServerPatchCheck($teamServers, bundledOnly: true));
});

// Clear patch data only for the servers in this batch
Server::whereIn('id', $servers->pluck('id'))->update(['patch_check_data' => null]);
}
}
67 changes: 67 additions & 0 deletions app/Jobs/SendTraefikOutdatedNotificationJob.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

namespace App\Jobs;

use App\Enums\ProxyTypes;
use App\Models\Server;
use App\Notifications\Server\TraefikVersionOutdated;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeEncrypted;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;

class SendTraefikOutdatedNotificationJob implements ShouldBeEncrypted, ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

public $tries = 3;

public function __construct()
{
$this->onQueue('high');
}

public function handle(): void
{
$servers = Server::whereNotNull('traefik_outdated_info')
->whereProxyType(ProxyTypes::TRAEFIK->value)
->whereRelation('settings', 'is_reachable', true)
->with('team')
->get();

if ($servers->isEmpty()) {
return;
}

// Only include servers whose check is newer than the last notification
$serversToNotify = $servers->filter(function ($server) {
$info = $server->traefik_outdated_info;
$checkedAt = $info['checked_at'] ?? null;
$notifiedAt = $info['notified_at'] ?? null;

return $checkedAt && (! $notifiedAt || $checkedAt > $notifiedAt);
});

if ($serversToNotify->isEmpty()) {
return;
}

$serversToNotify->groupBy('team_id')->each(function ($teamServers) {
$team = $teamServers->first()->team;
if (! $team) {
return;
}

$team->notify(new TraefikVersionOutdated($teamServers, bundledOnly: true));
});

// Mark servers as notified so they aren't re-notified next week
$serversToNotify->each(function ($server) {
$info = $server->traefik_outdated_info;
$info['notified_at'] = now()->toIso8601String();
$server->update(['traefik_outdated_info' => $info]);
});
}
}
28 changes: 25 additions & 3 deletions app/Jobs/ServerManagerJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Carbon;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Bus;
use Illuminate\Support\Facades\Log;

class ServerManagerJob implements ShouldBeEncrypted, ShouldQueue
Expand All @@ -30,6 +31,8 @@ class ServerManagerJob implements ShouldBeEncrypted, ShouldQueue

private string $checkFrequency = '* * * * *';

private array $pendingPatchCheckJobs = [];

/**
* Create a new job instance.
*/
Expand Down Expand Up @@ -60,6 +63,9 @@ public function handle(): void

// Process server-specific scheduled tasks
$this->processScheduledTasks($servers);

// Dispatch collected patch check jobs as a batch (if bundling is enabled)
$this->dispatchPatchCheckJobs();
}

private function getServers(): Collection
Expand Down Expand Up @@ -160,11 +166,11 @@ private function processServerTasks(Server $server): void
}
}

// Dispatch ServerPatchCheckJob if due (weekly)
// Collect ServerPatchCheckJob if due (weekly)
$shouldRunPatchCheck = shouldRunCronNow('0 0 * * 0', $serverTimezone, "server-patch-check:{$server->id}", $this->executionTime);

if ($shouldRunPatchCheck) { // Weekly on Sunday at midnight
ServerPatchCheckJob::dispatch($server);
if ($shouldRunPatchCheck) {
$this->pendingPatchCheckJobs[] = new ServerPatchCheckJob($server);
}

// Note: CheckAndStartSentinelJob is only dispatched daily (line above) for version updates.
Expand Down Expand Up @@ -205,4 +211,20 @@ private function shouldSkipDueToBackoff(Server $server): bool

return ($cycleIndex + $serverHash) % $interval !== 0;
}

private function dispatchPatchCheckJobs(): void
{
if (empty($this->pendingPatchCheckJobs)) {
return;
}

$serverIds = collect($this->pendingPatchCheckJobs)->map(fn ($job) => $job->server->id)->all();

Bus::batch($this->pendingPatchCheckJobs)
->allowFailures()
->finally(fn () => SendPatchCheckNotificationJob::dispatch($serverIds))
->name('server-patch-check')
->onQueue('high')
->dispatch();
}
}
28 changes: 15 additions & 13 deletions app/Jobs/ServerPatchCheckJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,27 @@
use App\Actions\Server\CheckUpdates;
use App\Models\Server;
use App\Notifications\Server\ServerPatchCheck;
use Illuminate\Bus\Batchable;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeEncrypted;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\Middleware\WithoutOverlapping;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Log;

class ServerPatchCheckJob implements ShouldBeEncrypted, ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
use Batchable, Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

public $tries = 3;

public $timeout = 600;

public function middleware(): array
{
return [(new WithoutOverlapping('server-patch-check-'.$this->server->uuid))->expireAfter(600)->dontRelease()];
return [(new WithoutOverlapping('server-patch-check-'.$this->server->uuid))->expireAfter(600)->releaseAfter(60)];
}

public function __construct(public Server $server) {}
Expand All @@ -43,21 +45,21 @@ public function handle(): void
// Check for updates
$patchData = CheckUpdates::run($this->server);

if (isset($patchData['error'])) {
$team->notify(new ServerPatchCheck($this->server, $patchData));

return; // Skip if there's an error checking for updates
}

$totalUpdates = $patchData['total_updates'] ?? 0;

// Only send notification if there are updates available
if ($totalUpdates > 0) {
$team->notify(new ServerPatchCheck($this->server, $patchData));
if (isset($patchData['error']) || $totalUpdates > 0) {
$this->server->update(['patch_check_data' => $patchData]);

// Send immediate notification to channels that have bundling disabled
$unbundledChannels = $team->getEnabledChannels('server_patch', unbundledOnly: true);
if (! empty($unbundledChannels)) {
$team->notify(new ServerPatchCheck(collect([$this->server]), unbundledOnly: true));
}
} else {
$this->server->update(['patch_check_data' => null]);
}
} catch (\Throwable $e) {
// Log error but don't fail the job
\Illuminate\Support\Facades\Log::error('ServerPatchCheckJob failed: '.$e->getMessage(), [
Log::error('ServerPatchCheckJob failed: '.$e->getMessage(), [
'server_id' => $this->server->id,
'server_name' => $this->server->name,
'error' => $e->getMessage(),
Expand Down
12 changes: 12 additions & 0 deletions app/Livewire/Notifications/Discord.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@ class Discord extends Component
#[Validate(['boolean'])]
public bool $discordPingEnabled = true;

#[Validate(['boolean'])]
public bool $bundlePatchNotifications = false;

#[Validate(['boolean'])]
public bool $bundleTraefikNotifications = false;

public function mount()
{
try {
Expand Down Expand Up @@ -106,6 +112,9 @@ public function syncData(bool $toModel = false)

$this->settings->discord_ping_enabled = $this->discordPingEnabled;

$this->settings->bundle_patch_notifications = $this->bundlePatchNotifications;
$this->settings->bundle_traefik_notifications = $this->bundleTraefikNotifications;

$this->settings->save();
refreshSession();
} else {
Expand All @@ -128,6 +137,9 @@ public function syncData(bool $toModel = false)
$this->traefikOutdatedDiscordNotifications = $this->settings->traefik_outdated_discord_notifications;

$this->discordPingEnabled = $this->settings->discord_ping_enabled;

$this->bundlePatchNotifications = $this->settings->bundle_patch_notifications ?? false;
$this->bundleTraefikNotifications = $this->settings->bundle_traefik_notifications ?? false;
}
}

Expand Down
Loading