mirror of
https://github.com/coollabsio/coolify.git
synced 2026-11-09 14:33:00 -04: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.
189 lines
6.5 KiB
PHP
189 lines
6.5 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
|
use Illuminate\Database\Eloquent\Relations\HasOne;
|
|
use Illuminate\Database\Eloquent\Relations\MorphTo;
|
|
|
|
class ScheduledVolumeBackup extends BaseModel
|
|
{
|
|
public const int DEFAULT_TIMEOUT = 36000;
|
|
|
|
protected $fillable = [
|
|
'uuid',
|
|
'backupable_type',
|
|
'backupable_id',
|
|
'team_id',
|
|
's3_storage_id',
|
|
'frequency',
|
|
'enabled',
|
|
'save_s3',
|
|
'disable_local_backup',
|
|
'stop_during_backup',
|
|
'retention_amount_locally',
|
|
'retention_days_locally',
|
|
'retention_max_storage_locally',
|
|
'retention_amount_s3',
|
|
'retention_days_s3',
|
|
'retention_max_storage_s3',
|
|
'timeout',
|
|
];
|
|
|
|
protected function casts(): array
|
|
{
|
|
return [
|
|
'enabled' => 'boolean',
|
|
'save_s3' => 'boolean',
|
|
'disable_local_backup' => 'boolean',
|
|
'stop_during_backup' => 'boolean',
|
|
'retention_amount_locally' => 'integer',
|
|
'retention_days_locally' => 'integer',
|
|
'retention_max_storage_locally' => 'float',
|
|
'retention_amount_s3' => 'integer',
|
|
'retention_days_s3' => 'integer',
|
|
'retention_max_storage_s3' => 'float',
|
|
'timeout' => 'integer',
|
|
];
|
|
}
|
|
|
|
public function scopeForApplication(Builder $query, Application $application): Builder
|
|
{
|
|
$volumeIds = $application->persistentStorages()->pluck('id');
|
|
$directoryIds = $application->fileStorages()
|
|
->where('is_directory', true)
|
|
->where('is_host_file', false)
|
|
->pluck('id');
|
|
|
|
return $query->where(function (Builder $query) use ($volumeIds, $directoryIds): void {
|
|
$query->where(function (Builder $query) use ($volumeIds): void {
|
|
$query->where('backupable_type', (new LocalPersistentVolume)->getMorphClass())
|
|
->whereIn('backupable_id', $volumeIds);
|
|
})->orWhere(function (Builder $query) use ($directoryIds): void {
|
|
$query->where('backupable_type', (new LocalFileVolume)->getMorphClass())
|
|
->whereIn('backupable_id', $directoryIds);
|
|
});
|
|
});
|
|
}
|
|
|
|
public function scopeForService(Builder $query, Service $service): Builder
|
|
{
|
|
$resources = $service->applications()->get()->concat($service->databases()->get());
|
|
if ($resources->isEmpty()) {
|
|
return $query->whereRaw('1 = 0');
|
|
}
|
|
$resourceIdsByType = $resources->groupBy(fn (Model $resource): string => $resource->getMorphClass());
|
|
|
|
$volumeIds = LocalPersistentVolume::query()
|
|
->where(function (Builder $query) use ($resourceIdsByType): void {
|
|
foreach ($resourceIdsByType as $type => $resources) {
|
|
$query->orWhere(fn (Builder $query) => $query
|
|
->where('resource_type', $type)
|
|
->whereIn('resource_id', $resources->pluck('id')));
|
|
}
|
|
})->pluck('id');
|
|
$directoryIds = LocalFileVolume::query()
|
|
->where('is_directory', true)
|
|
->where('is_host_file', false)
|
|
->where(function (Builder $query) use ($resourceIdsByType): void {
|
|
foreach ($resourceIdsByType as $type => $resources) {
|
|
$query->orWhere(fn (Builder $query) => $query
|
|
->where('resource_type', $type)
|
|
->whereIn('resource_id', $resources->pluck('id')));
|
|
}
|
|
})->pluck('id');
|
|
|
|
return $query->where(function (Builder $query) use ($volumeIds, $directoryIds): void {
|
|
$query->where(fn (Builder $query) => $query
|
|
->where('backupable_type', (new LocalPersistentVolume)->getMorphClass())
|
|
->whereIn('backupable_id', $volumeIds))
|
|
->orWhere(fn (Builder $query) => $query
|
|
->where('backupable_type', (new LocalFileVolume)->getMorphClass())
|
|
->whereIn('backupable_id', $directoryIds));
|
|
});
|
|
}
|
|
|
|
public function backupable(): MorphTo
|
|
{
|
|
return $this->morphTo();
|
|
}
|
|
|
|
public function team(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Team::class);
|
|
}
|
|
|
|
public function s3(): BelongsTo
|
|
{
|
|
return $this->belongsTo(S3Storage::class, 's3_storage_id');
|
|
}
|
|
|
|
public function executions(): HasMany
|
|
{
|
|
return $this->hasMany(ScheduledVolumeBackupExecution::class)->latest();
|
|
}
|
|
|
|
public function latestExecution(): HasOne
|
|
{
|
|
return $this->hasOne(ScheduledVolumeBackupExecution::class)->latestOfMany();
|
|
}
|
|
|
|
public function server(): ?Server
|
|
{
|
|
$resource = $this->targetResource();
|
|
|
|
if ($resource instanceof ServiceApplication || $resource instanceof ServiceDatabase) {
|
|
return $resource->service?->server?->fresh();
|
|
}
|
|
|
|
return $resource?->destination?->server?->fresh();
|
|
}
|
|
|
|
public function targetResource(): ?Model
|
|
{
|
|
return $this->backupable?->resource;
|
|
}
|
|
|
|
public function targetType(): string
|
|
{
|
|
return $this->backupable instanceof LocalFileVolume ? 'Directory' : 'Volume';
|
|
}
|
|
|
|
public function targetName(): string
|
|
{
|
|
return match (true) {
|
|
$this->backupable instanceof LocalFileVolume => $this->backupable->fs_path,
|
|
$this->backupable instanceof LocalPersistentVolume => $this->backupable->name,
|
|
default => 'Unknown storage',
|
|
};
|
|
}
|
|
|
|
public function sourcePath(): string
|
|
{
|
|
$target = $this->backupable;
|
|
|
|
if ($target instanceof LocalPersistentVolume) {
|
|
return filled($target->host_path) ? $target->host_path : $target->name;
|
|
}
|
|
|
|
if (! $target instanceof LocalFileVolume || ! $target->is_directory) {
|
|
throw new \RuntimeException('The backup target is not a directory or persistent volume.');
|
|
}
|
|
|
|
$path = str($target->fs_path);
|
|
if ($path->startsWith('.')) {
|
|
$resource = $this->targetResource();
|
|
if (! $resource || ! method_exists($resource, 'workdir')) {
|
|
throw new \RuntimeException('The directory backup workdir is unavailable.');
|
|
}
|
|
|
|
return $resource->workdir().$path->after('.')->toString();
|
|
}
|
|
|
|
return $path->toString();
|
|
}
|
|
}
|