diff --git a/src/Appwrite/Platform/Tasks/ScheduleBase.php b/src/Appwrite/Platform/Tasks/ScheduleBase.php index 1213f78924..dfa33fad38 100644 --- a/src/Appwrite/Platform/Tasks/ScheduleBase.php +++ b/src/Appwrite/Platform/Tasks/ScheduleBase.php @@ -244,6 +244,7 @@ abstract class ScheduleBase extends Action } // Only process updated/new schedules, not all schedules + $scheduleIdsToDelete = []; foreach ($updatedSequences as $sequence) { $schedule = $this->schedules[$sequence] ?? null; if ($schedule === null) { @@ -279,6 +280,7 @@ abstract class ScheduleBase extends Action if ($resource->isEmpty()) { Console::error("Resource not found: projectId::{$schedule['projectId']} resourceId::{$schedule['resourceId']}"); + $scheduleIdsToDelete[] = $schedule['$id']; unset($this->schedules[$sequence]); continue; } @@ -286,6 +288,16 @@ abstract class ScheduleBase extends Action $this->schedules[$sequence]['resource'] = $resource; } + if (!empty($scheduleIdsToDelete)) { + Console::info('Deleting ' . count($scheduleIdsToDelete) . ' orphaned schedules'); + + go(function () use ($dbForPlatform, $scheduleIdsToDelete) { + $dbForPlatform->deleteDocuments('schedules', [ + Query::equal('$id', $scheduleIdsToDelete), + ]); + }); + } + $lastSyncUpdate = $time; $duration = microtime(true) - $loadStart; $this->collectSchedulesTelemetryDuration->record($duration, ['initial' => $initialLoad, 'resourceType' => static::getSupportedResource()]);