chore(): Add retry strategy to database connection (#12713)

RESOLVES FRMW-2978

**What**
Add retry mechanism to database connection management to prevent failing when the server start faster than what makes the connection available
This commit is contained in:
Adrien de Peretti
2025-06-13 09:18:49 +02:00
committed by GitHub
parent 44d1d18689
commit cbf3644eb7
9 changed files with 333 additions and 45 deletions

View File

@@ -1,13 +1,19 @@
import { ContainerRegistrationKeys, ModulesSdkUtils } from "@medusajs/utils"
import {
ContainerRegistrationKeys,
ModulesSdkUtils,
retryExecution,
stringifyCircular,
} from "@medusajs/utils"
import { asValue } from "awilix"
import { container } from "../container"
import { configManager } from "../config"
import { container } from "../container"
import { logger } from "../logger"
/**
* Initialize a knex connection that can then be shared to any resources if needed
*/
export function pgConnectionLoader(): ReturnType<
typeof ModulesSdkUtils.createPgConnection
export async function pgConnectionLoader(): Promise<
ReturnType<typeof ModulesSdkUtils.createPgConnection>
> {
if (container.hasRegistration(ContainerRegistrationKeys.PG_CONNECTION)) {
return container.resolve(
@@ -45,6 +51,31 @@ export function pgConnectionLoader(): ReturnType<
},
})
const maxRetries = process.env.__MEDUSA_DB_CONNECTION_MAX_RETRIES
? parseInt(process.env.__MEDUSA_DB_CONNECTION_MAX_RETRIES)
: 5
const retryDelay = process.env.__MEDUSA_DB_CONNECTION_RETRY_DELAY
? parseInt(process.env.__MEDUSA_DB_CONNECTION_RETRY_DELAY)
: 1000
await retryExecution(
async () => {
await pgConnection.raw("SELECT 1")
},
{
maxRetries,
retryDelay,
onRetry: (error) => {
logger.warn(
`Pg connection failed to connect to the database. Retrying...\n${stringifyCircular(
error
)}`
)
},
}
)
container.register(
ContainerRegistrationKeys.PG_CONNECTION,
asValue(pgConnection)

View File

@@ -24,7 +24,6 @@ import {
isPresent,
upperCaseFirst,
} from "@medusajs/utils"
import { pgConnectionLoader } from "./database"
import type { Knex } from "@mikro-orm/knex"
import { aliasTo, asValue } from "awilix"
@@ -125,11 +124,8 @@ export class MedusaAppLoader {
const sharedResourcesConfig: ModuleServiceInitializeOptions = {
database: {
clientUrl:
(
injectedDependencies[
ContainerRegistrationKeys.PG_CONNECTION
] as ReturnType<typeof pgConnectionLoader>
)?.client?.config?.connection?.connectionString ??
injectedDependencies[ContainerRegistrationKeys.PG_CONNECTION]?.client
?.config?.connection?.connectionString ??
configManager.config.projectConfig.databaseUrl,
driverOptions: configManager.config.projectConfig.databaseDriverOptions,
pool: pool,