mirror of
https://github.com/coollabsio/coolify.git
synced 2026-11-16 01:32:39 -04:00
The scheduled job manager checked every enabled schedule each minute and evaluated its cron expression in PHP. On Coolify Cloud one run took more than 60 seconds, so runs overlapped and minutes were skipped. Each schedule now stores its next due time: - next_run_at on scheduled database backups, scheduled tasks, and volume backups, and docker_cleanup_next_run_at on server settings (UTC, indexed) - model hooks calculate it when a schedule is created or its frequency or enabled flag changes; a server timezone change or a move to another server calculates it again for the affected schedules The `scheduled:dispatch` command runs the manager every minute from the scheduler, not from a queue. It selects only due rows and claims each occurrence with an atomic update of next_run_at, together with its delivery record, so parallel runs on several nodes never dispatch it twice. - database and volume backups run once when they are late; tasks and Docker cleanups more than 10 minutes late are logged as missed - a local time that occurs two times when the clocks go back runs once - the migration continues each schedule after its last recorded occurrence and drops scheduled_job_states Show the next run, in the server timezone, on the database backup, scheduled task, volume backup, and Docker cleanup pages, or "Invalid frequency" when the frequency cannot be calculated. scheduled:diagnostics now lists overdue schedules and open deliveries. Remove workarounds that are no longer needed: the queue overlap lock and its stale-lock repair, the catch-up window, and debug-only logs.
154 lines
5.4 KiB
PHP
154 lines
5.4 KiB
PHP
<?php
|
|
|
|
namespace App\Livewire\Server;
|
|
|
|
use App\Jobs\DockerCleanupJob;
|
|
use App\Models\DockerCleanupExecution;
|
|
use App\Models\Server;
|
|
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
|
|
use Illuminate\Support\Facades\Cache;
|
|
use Livewire\Attributes\Computed;
|
|
use Livewire\Attributes\Validate;
|
|
use Livewire\Component;
|
|
|
|
class DockerCleanup extends Component
|
|
{
|
|
use AuthorizesRequests;
|
|
|
|
public Server $server;
|
|
|
|
public array $parameters = [];
|
|
|
|
#[Validate(['string', 'required'])]
|
|
public string $dockerCleanupFrequency = '*/10 * * * *';
|
|
|
|
#[Validate(['integer', 'min:1', 'max:99'])]
|
|
public int $dockerCleanupThreshold = 10;
|
|
|
|
#[Validate('boolean')]
|
|
public bool $forceDockerCleanup = false;
|
|
|
|
#[Validate('boolean')]
|
|
public bool $deleteUnusedVolumes = false;
|
|
|
|
#[Validate('boolean')]
|
|
public bool $deleteUnusedNetworks = false;
|
|
|
|
#[Validate('boolean')]
|
|
public bool $disableApplicationImageRetention = false;
|
|
|
|
/**
|
|
* The scheduled cleanup is stale when its next run is more than 10 minutes overdue,
|
|
* meaning the scheduled job dispatcher did not run it.
|
|
*/
|
|
#[Computed]
|
|
public function isCleanupStale(): bool
|
|
{
|
|
// The dispatcher does not run cleanups on servers without a real IP address.
|
|
if (blank($this->server->ip) || $this->server->hasPlaceholderIp()) {
|
|
return false;
|
|
}
|
|
|
|
$nextRunAt = $this->server->settings->docker_cleanup_next_run_at;
|
|
|
|
return $nextRunAt !== null && $nextRunAt->lt(now()->subMinutes(10));
|
|
}
|
|
|
|
#[Computed]
|
|
public function lastExecutionTime(): ?string
|
|
{
|
|
return DockerCleanupExecution::where('server_id', $this->server->id)
|
|
->orderBy('created_at', 'desc')
|
|
->first()
|
|
?->created_at
|
|
?->diffForHumans();
|
|
}
|
|
|
|
#[Computed]
|
|
public function isSchedulerHealthy(): bool
|
|
{
|
|
return Cache::get('scheduled-job-manager:heartbeat') !== null;
|
|
}
|
|
|
|
public function mount(string $server_uuid)
|
|
{
|
|
try {
|
|
$this->server = Server::ownedByCurrentTeam()->whereUuid($server_uuid)->firstOrFail();
|
|
$this->parameters = get_route_parameters();
|
|
$this->syncData();
|
|
} catch (\Throwable) {
|
|
return redirect()->route('server.index');
|
|
}
|
|
}
|
|
|
|
private function syncData(bool $toModel = false): void
|
|
{
|
|
if ($toModel) {
|
|
$this->validate();
|
|
$this->server->settings->force_docker_cleanup = $this->forceDockerCleanup;
|
|
$this->server->settings->docker_cleanup_frequency = $this->dockerCleanupFrequency;
|
|
$this->server->settings->docker_cleanup_threshold = $this->dockerCleanupThreshold;
|
|
$this->server->settings->delete_unused_volumes = $this->deleteUnusedVolumes;
|
|
$this->server->settings->delete_unused_networks = $this->deleteUnusedNetworks;
|
|
$this->server->settings->disable_application_image_retention = $this->disableApplicationImageRetention;
|
|
$this->server->settings->save();
|
|
} else {
|
|
$this->forceDockerCleanup = $this->server->settings->force_docker_cleanup;
|
|
$this->dockerCleanupFrequency = $this->server->settings->docker_cleanup_frequency;
|
|
$this->dockerCleanupThreshold = $this->server->settings->docker_cleanup_threshold;
|
|
$this->deleteUnusedVolumes = $this->server->settings->delete_unused_volumes;
|
|
$this->deleteUnusedNetworks = $this->server->settings->delete_unused_networks;
|
|
$this->disableApplicationImageRetention = $this->server->settings->disable_application_image_retention;
|
|
}
|
|
}
|
|
|
|
public function instantSave()
|
|
{
|
|
try {
|
|
$this->authorize('update', $this->server);
|
|
$this->syncData(true);
|
|
$this->dispatch('success', 'Server updated.');
|
|
} catch (\Throwable $e) {
|
|
return handleError($e, $this);
|
|
}
|
|
}
|
|
|
|
public function manualCleanup()
|
|
{
|
|
try {
|
|
$this->authorize('update', $this->server);
|
|
DockerCleanupJob::dispatch($this->server, true, $this->deleteUnusedVolumes, $this->deleteUnusedNetworks);
|
|
auditLog('ui.server.docker_cleanup_started', [
|
|
'team_id' => $this->server->team_id,
|
|
'server_uuid' => $this->server->uuid,
|
|
'server_name' => $this->server->name,
|
|
'delete_unused_volumes' => $this->deleteUnusedVolumes,
|
|
'delete_unused_networks' => $this->deleteUnusedNetworks,
|
|
]);
|
|
$this->dispatch('success', 'Manual cleanup job started. Depending on the amount of data, this might take a while.');
|
|
} catch (\Throwable $e) {
|
|
return handleError($e, $this);
|
|
}
|
|
}
|
|
|
|
public function submit()
|
|
{
|
|
try {
|
|
if (! validate_cron_expression($this->dockerCleanupFrequency)) {
|
|
$this->dockerCleanupFrequency = $this->server->settings->getOriginal('docker_cleanup_frequency');
|
|
throw new \Exception('Invalid Cron / Human expression for Docker Cleanup Frequency.');
|
|
}
|
|
$this->authorize('update', $this->server);
|
|
$this->syncData(true);
|
|
$this->dispatch('success', 'Server updated.');
|
|
} catch (\Throwable $e) {
|
|
return handleError($e, $this);
|
|
}
|
|
}
|
|
|
|
public function render()
|
|
{
|
|
return view('livewire.server.docker-cleanup');
|
|
}
|
|
}
|