Repository: appwrite/appwrite
File: src/Appwrite/Platform/Tasks/ScheduleBase.php (PHP)

Code before:

                if ($localDocument !== null) {
                    if (!$document['active']) {
                        Console::info("Removing: {$document['resourceType']}::{$document['resourceId']}");
                        unset($this->schedules[$document->getInternalId()]);
                    } elseif (strtotime($localDocument['resourceUpdatedAt']) !== strtotime($document['resourceUpdatedAt'])) {
                        Console::info("Updating: {$document['resourceType']}::{$document['resourceId']}");
                        $this->schedules[$document->getInternalId()] = $getSchedule($document);
                    }
                } else {
                    try {
                        $this->schedules[$document->getInternalId()] = $getSchedule($document);
                    } catch (\Throwable $th) {
                        $collectionId = static::getCollectionId();
                        Console::error("Failed to load schedule for project {$document['projectId']} {$collectionId} {$document['resourceId']}");
                        Console::error($th->getMessage());
                    }
                }
            }

            $latestDocument = \end($results);
        }

        $lastSyncUpdate = $time;
        $duration = microtime(true) - $loadStart;
        $this->collectSchedulesTelemetryDuration->record($duration, ['initial' => $initialLoad, 'resourceType' => static::getSupportedResource()]);
        $this->collectSchedulesTelemetryCount->record($total, ['initial' => $initialLoad, 'resourceType' => static::getSupportedResource()]);
        Console::success("{$total} resources were loaded in " . $duration . " seconds");
    }

    protected function recordEnqueueDelay(string $expectedExecutionSchedule): void
    {
        $now = strtotime('now');
        $scheduledAt = strtotime($expectedExecutionSchedule);
        $this->enqueueDelayTelemetry->record($now - $scheduledAt, ['resourceType' => static::getSupportedResource()]);
    }
}


Review comment (suggestion):
the getProjectDB() call consumes one connection from the pool and does not return it back. So it will lead to the pool is empty errors. 

Code after:
                if ($localDocument !== null) {
                    if (!$document['active']) {
                        Console::info("Removing: {$document['resourceType']}::{$document['resourceId']}");
                        unset($this->schedules[$document->getInternalId()]);
                    } elseif (strtotime($localDocument['resourceUpdatedAt']) !== strtotime($document['resourceUpdatedAt'])) {
                        Console::info("Updating: {$document['resourceType']}::{$document['resourceId']}");
                        $this->schedules[$document->getInternalId()] = $getSchedule($document);
                    }
                } else {
                    try {
                        $this->schedules[$document->getInternalId()] = $getSchedule($document);
                    } catch (\Throwable $th) {
                        $collectionId = static::getCollectionId();
                        Console::error("Failed to load schedule for project {$document['projectId']} {$collectionId} {$document['resourceId']}");
                        Console::error($th->getMessage());
                    }
                }
            }

            $latestDocument = \end($results);
        }

        $lastSyncUpdate = $time;
        $duration = microtime(true) - $loadStart;
        $this->collectSchedulesTelemetryDuration->record($duration, ['initial' => $initialLoad, 'resourceType' => static::getSupportedResource()]);
        $this->collectSchedulesTelemetryCount->record($total, ['initial' => $initialLoad, 'resourceType' => static::getSupportedResource()]);
        Console::success("{$total} resources were loaded in " . $duration . " seconds");
    }

    protected function recordEnqueueDelay(string $expectedExecutionSchedule): void
    {
        $now = strtotime('now');
        $scheduledAt = strtotime($expectedExecutionSchedule);
        $this->enqueueDelayTelemetry->record($now - $scheduledAt, ['resourceType' => static::getSupportedResource()]);
    }
}