Feat: cron jobs are executed based on the server timezone

This commit is contained in:
ayntk-ai 2024-08-16 12:58:19 +02:00
parent a478ebef2e
commit 1892ce4e12
No known key found for this signature in database
2 changed files with 13 additions and 5 deletions

View File

@ -139,10 +139,9 @@ private function check_scheduled_tasks($schedule)
$service = $scheduled_task->service;
$application = $scheduled_task->application;
if (! $application && ! $service) {
if (!$application && !$service) {
ray('application/service attached to scheduled task does not exist');
$scheduled_task->delete();
continue;
}
if ($application) {
@ -158,9 +157,12 @@ private function check_scheduled_tasks($schedule)
if (isset(VALID_CRON_STRINGS[$scheduled_task->frequency])) {
$scheduled_task->frequency = VALID_CRON_STRINGS[$scheduled_task->frequency];
}
$server_timezone = $application ? $application->destination->server->settings->server_timezone : $service->destination->server->settings->server_timezone;
$server_timezone = $server_timezone;
ray($server_timezone);
$schedule->job(new ScheduledTaskJob(
task: $scheduled_task
))->cron($scheduled_task->frequency)->onOneServer();
))->cron($scheduled_task->frequency)->timezone($server_timezone)->onOneServer();
}
}
@ -170,4 +172,4 @@ protected function commands(): void
require base_path('routes/console.php');
}
}
}

View File

@ -36,6 +36,8 @@ class ScheduledTaskJob implements ShouldQueue
public array $containers = [];
public string $server_timezone;
public function __construct($task)
{
$this->task = $task;
@ -47,6 +49,7 @@ public function __construct($task)
throw new \RuntimeException('ScheduledTaskJob failed: No resource found.');
}
$this->team = Team::find($task->team_id);
$this->server_timezone = $this->resource->destination->server->settings->server_timezone;
}
public function middleware(): array
@ -61,6 +64,7 @@ public function uniqueId(): int
public function handle(): void
{
try {
$this->task_log = ScheduledTaskExecution::create([
'scheduled_task_id' => $this->task->id,
@ -121,6 +125,8 @@ public function handle(): void
$this->team?->notify(new TaskFailed($this->task, $e->getMessage()));
// send_internal_notification('ScheduledTaskJob failed with: ' . $e->getMessage());
throw $e;
} finally {
}
}
}
}