mirror of
https://github.com/coollabsio/coolify.git
synced 2026-11-06 17:42:34 +00:00
The missed daily backups on Coolify Cloud came from Redis evicting queue keys (allkeys-lfu on a full instance), not from the scan of all schedules. The next_run_at design (452259aac,5c5740533) did not prevent that, and it added stored state that must be kept in sync on every frequency, timezone, and server change. It was not released, so remove it: - revert next_run_at, its model hooks, the "Next run" UI, and the diagnostics changes; restore scheduled_job_states and the queue overlap lock - a new migration removes the next_run_at columns and creates scheduled_job_states again where the removed migration already ran Keep the changes that fix real problems: - run the manager every minute from the scheduler with `scheduled:dispatch`, so a busy queue cannot delay it past the catch-up window - load the server chain of scheduled backups with each chunk, which removes about 7,000 queries per run on Coolify Cloud - catch Throwable for each schedule, so one broken schedule does not stop the others - publish database and volume backups again when their queued job was not started after 60 minutes (for example, lost in Redis); log lost tasks and Docker cleanups as missed. A lost backup is not run again when a newer occurrence of the same schedule exists.
126 lines
4.4 KiB
PHP
126 lines
4.4 KiB
PHP
<?php
|
|
|
|
use App\Jobs\CleanupOrphanedPreviewContainersJob;
|
|
use App\Jobs\ScheduledJobManager;
|
|
use App\Models\PrivateKey;
|
|
use App\Models\Server;
|
|
use App\Models\Team;
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
use Tests\TestCase;
|
|
|
|
uses(TestCase::class, RefreshDatabase::class);
|
|
|
|
function createServerForPlaceholderIpTest(string $ip): Server
|
|
{
|
|
$team = Team::create([
|
|
'name' => 'Test Team',
|
|
'personal_team' => false,
|
|
]);
|
|
$privateKey = PrivateKey::factory()->create(['team_id' => $team->id]);
|
|
|
|
return Server::factory()->create([
|
|
'team_id' => $team->id,
|
|
'private_key_id' => $privateKey->id,
|
|
'ip' => $ip,
|
|
]);
|
|
}
|
|
|
|
it('detects placeholder IPs', function () {
|
|
$server = createServerForPlaceholderIpTest(Server::PLACEHOLDER_IP);
|
|
|
|
foreach ([Server::PLACEHOLDER_IP, '0.0.0.0', '::'] as $ip) {
|
|
$server->update(['ip' => $ip]);
|
|
|
|
expect($server->fresh()->hasPlaceholderIp())->toBeTrue();
|
|
}
|
|
});
|
|
|
|
it('does not treat a real IP as a placeholder', function () {
|
|
$server = createServerForPlaceholderIpTest('203.0.113.5');
|
|
|
|
expect($server->hasPlaceholderIp())->toBeFalse();
|
|
});
|
|
|
|
it('backfills a placeholder IP with the real address', function () {
|
|
$server = createServerForPlaceholderIpTest(Server::PLACEHOLDER_IP);
|
|
|
|
expect($server->backfillPlaceholderIp('203.0.113.5'))->toBeTrue();
|
|
expect($server->fresh()->ip)->toBe('203.0.113.5');
|
|
});
|
|
|
|
it('does not overwrite a real IP when backfilling', function () {
|
|
$server = createServerForPlaceholderIpTest('203.0.113.5');
|
|
|
|
expect($server->backfillPlaceholderIp('198.51.100.9'))->toBeFalse();
|
|
expect($server->fresh()->ip)->toBe('203.0.113.5');
|
|
});
|
|
|
|
it('ignores a missing IP when backfilling', function () {
|
|
$server = createServerForPlaceholderIpTest(Server::PLACEHOLDER_IP);
|
|
|
|
expect($server->backfillPlaceholderIp(null))->toBeFalse();
|
|
expect($server->fresh()->ip)->toBe(Server::PLACEHOLDER_IP);
|
|
});
|
|
|
|
it('does not replace a placeholder with another placeholder', function (string $replacementIp) {
|
|
$server = createServerForPlaceholderIpTest(Server::PLACEHOLDER_IP);
|
|
|
|
expect($server->backfillPlaceholderIp($replacementIp))->toBeFalse();
|
|
expect($server->fresh()->ip)->toBe(Server::PLACEHOLDER_IP);
|
|
})->with([
|
|
'unspecified IPv4 address' => '0.0.0.0',
|
|
'unspecified IPv6 address' => '::',
|
|
]);
|
|
|
|
it('does not overwrite an address configured after the placeholder model was loaded', function () {
|
|
$staleServer = createServerForPlaceholderIpTest(Server::PLACEHOLDER_IP);
|
|
$concurrentServer = Server::query()->findOrFail($staleServer->id);
|
|
|
|
$concurrentServer->update(['ip' => 'server.internal.example.com']);
|
|
|
|
expect($staleServer->backfillPlaceholderIp('203.0.113.10'))->toBeFalse()
|
|
->and($staleServer->fresh()->ip)->toBe('server.internal.example.com');
|
|
});
|
|
|
|
it('skips servers with a placeholder IP in scheduled jobs', function () {
|
|
$server = createServerForPlaceholderIpTest(Server::PLACEHOLDER_IP);
|
|
|
|
expect($server->skipServer())->toBeTrue();
|
|
});
|
|
|
|
it('excludes every placeholder address from scheduled Docker cleanup', function () {
|
|
$realServer = createServerForPlaceholderIpTest('203.0.113.10');
|
|
|
|
foreach (Server::PLACEHOLDER_IPS as $placeholderIp) {
|
|
Server::factory()->create([
|
|
'team_id' => $realServer->team_id,
|
|
'private_key_id' => $realServer->private_key_id,
|
|
'ip' => $placeholderIp,
|
|
]);
|
|
}
|
|
|
|
$method = new ReflectionMethod(ScheduledJobManager::class, 'getServersForCleanupQuery');
|
|
$servers = $method->invoke(new ScheduledJobManager)->get();
|
|
|
|
expect($servers->modelKeys())->toBe([$realServer->id]);
|
|
});
|
|
|
|
it('excludes every placeholder address from orphaned preview cleanup', function () {
|
|
$realServer = createServerForPlaceholderIpTest('203.0.113.10');
|
|
$realServer->settings->update(['is_reachable' => true, 'is_usable' => true]);
|
|
|
|
foreach (Server::PLACEHOLDER_IPS as $placeholderIp) {
|
|
$server = Server::factory()->create([
|
|
'team_id' => $realServer->team_id,
|
|
'private_key_id' => $realServer->private_key_id,
|
|
'ip' => $placeholderIp,
|
|
]);
|
|
$server->settings->update(['is_reachable' => true, 'is_usable' => true]);
|
|
}
|
|
|
|
$method = new ReflectionMethod(CleanupOrphanedPreviewContainersJob::class, 'getServersToCheck');
|
|
$servers = $method->invoke(new CleanupOrphanedPreviewContainersJob);
|
|
|
|
expect($servers->modelKeys())->toBe([$realServer->id]);
|
|
});
|