From 388bd43ba8434ff6458b07868b1ee7a2ec456f08 Mon Sep 17 00:00:00 2001 From: Ting Chien Meng Date: Wed, 12 Feb 2025 00:36:03 +0800 Subject: [PATCH] add retry import --- packages/agent/src/index.ts | 11 ++++++++-- packages/core/src/import.ts | 40 +++++++++++++++++++++++++++++++++++-- 2 files changed, 47 insertions(+), 4 deletions(-) diff --git a/packages/agent/src/index.ts b/packages/agent/src/index.ts index 5307b322b03..4bd8804fbd6 100644 --- a/packages/agent/src/index.ts +++ b/packages/agent/src/index.ts @@ -16,7 +16,8 @@ import { parseBooleanFromText, settings, stringToUuid, - validateCharacterConfig + validateCharacterConfig, + importWithRetry } from "@elizaos/core"; import fs from "node:fs"; import net from "node:net"; @@ -342,13 +343,19 @@ function initializeCache( ); } } + async function findDatabaseAdapter(runtime: IAgentRuntime) { const { adapters } = runtime; let adapter: Adapter | undefined; // if not found, default to sqlite if (adapters.length === 0) { - const sqliteAdapterPlugin = await import('@elizaos-plugins/sqlite'); + const sqliteAdapterPlugin = await importWithRetry( + '@elizaos-plugins/sqlite', + 3, + 1000 + ); + // const sqliteAdapterPlugin = await import('@elizaos-plugins/sqlite'); const sqliteAdapterPluginDefault = sqliteAdapterPlugin.default; adapter = sqliteAdapterPluginDefault.adapters[0]; if (!adapter) { diff --git a/packages/core/src/import.ts b/packages/core/src/import.ts index fd7adb9117f..548ec549a34 100644 --- a/packages/core/src/import.ts +++ b/packages/core/src/import.ts @@ -1,5 +1,33 @@ import logger from "./logger"; +/** + * Attempts to import a module with retry logic. + * @param modulePath - The module path to import. + * @param retries - Number of retry attempts (default: 3). + * @param delay - Delay between retries in milliseconds (default: 1000). + * @returns The imported module. + * @throws An error if all attempts fail. + */ +export async function importWithRetry( + modulePath: string, + retries = 3, + delay = 1000 +): Promise { + for (let attempt = 1; attempt <= retries; attempt++) { + try { + return await import(modulePath); + } catch (error) { + if (attempt === retries) { + throw new Error( + `Failed to import ${modulePath} after ${retries} attempts: ${error}` + ); + } + await new Promise((resolve) => setTimeout(resolve, delay)); + } + } + throw new Error("Unexpected error in importWithRetry"); +} + const registrations = new Map(); export const dynamicImport = async (specifier: string) => { @@ -7,7 +35,11 @@ export const dynamicImport = async (specifier: string) => { if (module !== undefined) { return module; } else { - return await import(specifier); + return await importWithRetry( + specifier, + 3, + 1000 + ); } }; @@ -21,7 +53,11 @@ export async function handlePluginImporting(plugins: string[]) { const importedPlugins = await Promise.all( plugins.map(async (plugin) => { try { - const importedPlugin = await import(plugin); + const importedPlugin = await importWithRetry( + plugin, + 3, + 1000 + ); const functionName = `${plugin .replace("@elizaos/plugin-", "")