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
2 changed files with 13 additions and 5 deletions

View File

@@ -139,10 +139,9 @@ class Kernel extends ConsoleKernel
$service = $scheduled_task->service; $service = $scheduled_task->service;
$application = $scheduled_task->application; $application = $scheduled_task->application;
if (! $application && ! $service) { if (!$application && !$service) {
ray('application/service attached to scheduled task does not exist'); ray('application/service attached to scheduled task does not exist');
$scheduled_task->delete(); $scheduled_task->delete();
continue; continue;
} }
if ($application) { if ($application) {
@@ -158,9 +157,12 @@ class Kernel extends ConsoleKernel
if (isset(VALID_CRON_STRINGS[$scheduled_task->frequency])) { if (isset(VALID_CRON_STRINGS[$scheduled_task->frequency])) {
$scheduled_task->frequency = 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( $schedule->job(new ScheduledTaskJob(
task: $scheduled_task task: $scheduled_task
))->cron($scheduled_task->frequency)->onOneServer(); ))->cron($scheduled_task->frequency)->timezone($server_timezone)->onOneServer();
} }
} }

View File

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