From 40642b2aad0b76160a64aa362741bbf20acc3482 Mon Sep 17 00:00:00 2001 From: Fabian Gruber <1951610+basert@users.noreply.github.com> Date: Tue, 13 May 2025 13:03:09 +0200 Subject: [PATCH] fix(schedules): enqueue delay telemetry in wrong format (#9749) --- src/Appwrite/Platform/Tasks/ScheduleBase.php | 6 ++---- src/Appwrite/Platform/Tasks/ScheduleExecutions.php | 4 ++-- src/Appwrite/Platform/Tasks/ScheduleFunctions.php | 11 ++++++----- src/Appwrite/Platform/Tasks/ScheduleMessages.php | 4 ++-- 4 files changed, 12 insertions(+), 13 deletions(-) diff --git a/src/Appwrite/Platform/Tasks/ScheduleBase.php b/src/Appwrite/Platform/Tasks/ScheduleBase.php index 093c2740ba..d9de41ea64 100644 --- a/src/Appwrite/Platform/Tasks/ScheduleBase.php +++ b/src/Appwrite/Platform/Tasks/ScheduleBase.php @@ -202,10 +202,8 @@ abstract class ScheduleBase extends Action Console::success("{$total} resources were loaded in " . $duration . " seconds"); } - protected function recordEnqueueDelay(string $expectedExecutionSchedule): void + protected function recordEnqueueDelay(\DateTime $expectedExecutionSchedule): void { - $now = strtotime('now'); - $scheduledAt = strtotime($expectedExecutionSchedule); - $this->enqueueDelayTelemetry->record($now - $scheduledAt, ['resourceType' => static::getSupportedResource()]); + $this->enqueueDelayTelemetry->record(time() - $expectedExecutionSchedule->getTimestamp(), ['resourceType' => static::getSupportedResource()]); } } diff --git a/src/Appwrite/Platform/Tasks/ScheduleExecutions.php b/src/Appwrite/Platform/Tasks/ScheduleExecutions.php index 79e983f0c3..89d1609a33 100644 --- a/src/Appwrite/Platform/Tasks/ScheduleExecutions.php +++ b/src/Appwrite/Platform/Tasks/ScheduleExecutions.php @@ -59,7 +59,7 @@ class ScheduleExecutions extends ScheduleBase $this->updateProjectAccess($schedule['project'], $dbForPlatform); - \go(function () use ($queueForFunctions, $schedule, $delay, $data) { + \go(function () use ($queueForFunctions, $schedule, $scheduledAt, $delay, $data) { Co::sleep($delay); $queueForFunctions->setType('schedule') @@ -75,7 +75,7 @@ class ScheduleExecutions extends ScheduleBase ->setUserId($data['userId'] ?? '') ->trigger(); - $this->recordEnqueueDelay($schedule['schedule']); + $this->recordEnqueueDelay($scheduledAt); }); $dbForPlatform->deleteDocument( diff --git a/src/Appwrite/Platform/Tasks/ScheduleFunctions.php b/src/Appwrite/Platform/Tasks/ScheduleFunctions.php index abcfe132e3..689ba831b8 100644 --- a/src/Appwrite/Platform/Tasks/ScheduleFunctions.php +++ b/src/Appwrite/Platform/Tasks/ScheduleFunctions.php @@ -66,17 +66,18 @@ class ScheduleFunctions extends ScheduleBase $delayedExecutions[$delay] = []; } - $delayedExecutions[$delay][] = $key; + $delayedExecutions[$delay][] = ['key' => $key, 'nextDate' => $nextDate]; } - foreach ($delayedExecutions as $delay => $scheduleKeys) { - \go(function () use ($delay, $scheduleKeys, $pools, $dbForPlatform) { + foreach ($delayedExecutions as $delay => $schedules) { + \go(function () use ($delay, $schedules, $pools, $dbForPlatform) { \sleep($delay); // in seconds $queue = $pools->get('publisher')->pop(); $connection = $queue->getResource(); - foreach ($scheduleKeys as $scheduleKey) { + foreach ($schedules as $delayConfig) { + $scheduleKey = $delayConfig['key']; // Ensure schedule was not deleted if (!\array_key_exists($scheduleKey, $this->schedules)) { return; @@ -96,7 +97,7 @@ class ScheduleFunctions extends ScheduleBase ->setProject($schedule['project']) ->trigger(); - $this->recordEnqueueDelay($schedule['schedule']); + $this->recordEnqueueDelay($delayConfig['nextDate']); } $queue->reclaim(); diff --git a/src/Appwrite/Platform/Tasks/ScheduleMessages.php b/src/Appwrite/Platform/Tasks/ScheduleMessages.php index 9b962c99ee..a15df6ed5b 100644 --- a/src/Appwrite/Platform/Tasks/ScheduleMessages.php +++ b/src/Appwrite/Platform/Tasks/ScheduleMessages.php @@ -40,7 +40,7 @@ class ScheduleMessages extends ScheduleBase continue; } - \go(function () use ($schedule, $pools, $dbForPlatform) { + \go(function () use ($schedule, $scheduledAt, $pools, $dbForPlatform) { $queue = $pools->get('publisher')->pop(); $connection = $queue->getResource(); $queueForMessaging = new Messaging($connection); @@ -59,7 +59,7 @@ class ScheduleMessages extends ScheduleBase ); $queue->reclaim(); - $this->recordEnqueueDelay($schedule['schedule']); + $this->recordEnqueueDelay($scheduledAt); unset($this->schedules[$schedule['$internalId']]); }); }