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.
206 lines
7.8 KiB
PHP
206 lines
7.8 KiB
PHP
<?php
|
|
|
|
namespace App\Services;
|
|
|
|
use App\Jobs\DatabaseBackupJob;
|
|
use App\Jobs\DockerCleanupJob;
|
|
use App\Jobs\ScheduledTaskJob;
|
|
use App\Jobs\VolumeBackupJob;
|
|
use App\Models\ScheduledDatabaseBackup;
|
|
use App\Models\ScheduledJobDelivery;
|
|
use App\Models\ScheduledTask;
|
|
use App\Models\ScheduledVolumeBackup;
|
|
use App\Models\Server;
|
|
use Carbon\CarbonInterface;
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
class ScheduledJobDeliveryService
|
|
{
|
|
/**
|
|
* Record a pending delivery for one occurrence of a schedule. Returns null when the
|
|
* occurrence already has a delivery.
|
|
*
|
|
* @param array<string, mixed> $payload
|
|
*/
|
|
public function create(string $scheduleKey, CarbonInterface $scheduledFor, string $jobType, int $resourceId, array $payload = []): ?ScheduledJobDelivery
|
|
{
|
|
$delivery = ScheduledJobDelivery::createOrFirst(
|
|
['schedule_key' => $scheduleKey, 'scheduled_for' => $scheduledFor->utc()],
|
|
['job_type' => $jobType, 'resource_id' => $resourceId, 'payload' => $payload, 'status' => 'pending'],
|
|
);
|
|
|
|
return $delivery->wasRecentlyCreated ? $delivery : null;
|
|
}
|
|
|
|
public function publishPending(): void
|
|
{
|
|
ScheduledJobDelivery::query()
|
|
->where('status', 'pending')
|
|
->orderBy('id')
|
|
->chunkById(100, function ($occurrences): void {
|
|
foreach ($occurrences as $occurrence) {
|
|
try {
|
|
$this->logOccurrence($occurrence, 'Scheduled occurrence publishing again', 'warning');
|
|
$this->publish($occurrence);
|
|
} catch (\Throwable $e) {
|
|
Log::channel('scheduled-errors')->error('Failed to publish pending scheduled occurrence', [
|
|
'occurrence_uuid' => $occurrence->uuid,
|
|
'schedule_key' => $occurrence->schedule_key,
|
|
'error' => $e->getMessage(),
|
|
]);
|
|
}
|
|
}
|
|
});
|
|
}
|
|
|
|
public function deleteOldOccurrences(): void
|
|
{
|
|
ScheduledJobDelivery::query()
|
|
->where('status', 'claimed')
|
|
->where('updated_at', '<', now()->subDays(2))
|
|
->update([
|
|
'status' => 'failed',
|
|
'updated_at' => now(),
|
|
]);
|
|
|
|
ScheduledJobDelivery::query()
|
|
->whereIn('status', ['failed', 'skipped'])
|
|
->where('created_at', '<', now()->subDays(30))
|
|
->chunkById(100, function ($occurrences): void {
|
|
ScheduledJobDelivery::query()->whereKey($occurrences->modelKeys())->delete();
|
|
});
|
|
}
|
|
|
|
public function claim(string $uuid, string $claimToken): bool
|
|
{
|
|
$claimed = ScheduledJobDelivery::query()
|
|
->where('uuid', $uuid)
|
|
->whereIn('status', ['pending', 'enqueued'])
|
|
->update([
|
|
'status' => 'claimed',
|
|
'claim_token' => $claimToken,
|
|
'started_at' => now(),
|
|
'updated_at' => now(),
|
|
]) === 1
|
|
|| ScheduledJobDelivery::query()
|
|
->where('uuid', $uuid)
|
|
->where('status', 'claimed')
|
|
->where('claim_token', $claimToken)
|
|
->exists();
|
|
|
|
$this->logOccurrence($uuid, $claimed ? 'Scheduled occurrence claimed' : 'Scheduled occurrence claim rejected', $claimed ? 'info' : 'warning');
|
|
|
|
return $claimed;
|
|
}
|
|
|
|
public function complete(string $uuid, string $claimToken): void
|
|
{
|
|
$this->logOccurrence($uuid, 'Scheduled occurrence completed', context: ['status' => 'completed']);
|
|
|
|
ScheduledJobDelivery::query()
|
|
->where('uuid', $uuid)
|
|
->where('claim_token', $claimToken)
|
|
->delete();
|
|
}
|
|
|
|
public function fail(string $uuid, string $claimToken): void
|
|
{
|
|
ScheduledJobDelivery::query()
|
|
->where('uuid', $uuid)
|
|
->where('claim_token', $claimToken)
|
|
->update([
|
|
'status' => 'failed',
|
|
'updated_at' => now(),
|
|
]);
|
|
|
|
$this->logOccurrence($uuid, 'Scheduled occurrence failed', 'warning');
|
|
}
|
|
|
|
/**
|
|
* Log one step of an occurrence. Search scheduled.log for its schedule_key to see when it
|
|
* was due, when a worker started it, and how it ended.
|
|
*
|
|
* @param array<string, mixed> $context
|
|
*/
|
|
private function logOccurrence(ScheduledJobDelivery|string $occurrence, string $message, string $level = 'info', array $context = []): void
|
|
{
|
|
try {
|
|
$uuid = is_string($occurrence) ? $occurrence : $occurrence->uuid;
|
|
if (is_string($occurrence)) {
|
|
$occurrence = ScheduledJobDelivery::query()->where('uuid', $uuid)->first();
|
|
}
|
|
|
|
Log::channel('scheduled')->log($level, $message, [
|
|
'occurrence_uuid' => $uuid,
|
|
'schedule_key' => $occurrence?->schedule_key,
|
|
'status' => $occurrence?->status,
|
|
'scheduled_for' => $occurrence?->scheduled_for?->toIso8601String(),
|
|
'enqueued_at' => $occurrence?->enqueued_at?->toIso8601String(),
|
|
'started_at' => $occurrence?->started_at?->toIso8601String(),
|
|
'seconds_since_due' => $occurrence ? (int) $occurrence->scheduled_for->diffInSeconds(now()) : null,
|
|
'host' => gethostname(),
|
|
'pid' => getmypid(),
|
|
...$context,
|
|
]);
|
|
} catch (\Throwable) {
|
|
// Debug logging must never change the job result.
|
|
}
|
|
}
|
|
|
|
public function publish(ScheduledJobDelivery $occurrence): bool
|
|
{
|
|
if ($occurrence->status !== 'pending') {
|
|
return false;
|
|
}
|
|
|
|
$job = match ($occurrence->job_type) {
|
|
'scheduled-task' => ($task = ScheduledTask::find($occurrence->resource_id))
|
|
? new ScheduledTaskJob($task, $occurrence->uuid)
|
|
: null,
|
|
'database-backup' => ($backup = ScheduledDatabaseBackup::find($occurrence->resource_id))
|
|
? new DatabaseBackupJob($backup, $occurrence->uuid)
|
|
: null,
|
|
'volume-backup' => ($backup = ScheduledVolumeBackup::find($occurrence->resource_id))
|
|
? new VolumeBackupJob($backup, $occurrence->uuid)
|
|
: null,
|
|
'docker-cleanup' => ($server = Server::find($occurrence->resource_id))
|
|
? new DockerCleanupJob(
|
|
$server,
|
|
false,
|
|
data_get($occurrence->payload, 'delete_unused_volumes', false),
|
|
data_get($occurrence->payload, 'delete_unused_networks', false),
|
|
$occurrence->uuid,
|
|
)
|
|
: null,
|
|
default => null,
|
|
};
|
|
|
|
if ($job === null) {
|
|
ScheduledJobDelivery::query()->whereKey($occurrence->id)->update(['status' => 'skipped']);
|
|
Log::channel('scheduled')->warning('Scheduled occurrence skipped: resource not found', [
|
|
'occurrence_uuid' => $occurrence->uuid,
|
|
'schedule_key' => $occurrence->schedule_key,
|
|
'scheduled_for' => $occurrence->scheduled_for->toIso8601String(),
|
|
]);
|
|
|
|
return false;
|
|
}
|
|
|
|
dispatch($job);
|
|
|
|
ScheduledJobDelivery::query()
|
|
->whereKey($occurrence->id)
|
|
->where('status', 'pending')
|
|
->update([
|
|
'status' => 'enqueued',
|
|
'enqueued_at' => now(),
|
|
'updated_at' => now(),
|
|
]);
|
|
|
|
// Log from the loaded row: a fast worker can complete and delete the occurrence already.
|
|
$this->logOccurrence($occurrence, 'Scheduled occurrence enqueued', context: ['status' => 'enqueued', 'enqueued_at' => now()->toIso8601String()]);
|
|
|
|
return true;
|
|
}
|
|
}
|