|
1
|
import { Modules } from "@medusajs/framework/utils" |
|
2
|
import { ProviderLoaderOptions } from "@medusajs/types" |
|
3
|
import { RedisCacheModuleOptions } from "@types" |
|
4
|
import { asValue } from "@medusajs/framework/awilix" |
|
5
|
import Redis from "ioredis" |
|
6
|
|
|
7
|
export default async ({ |
|
8
|
container, |
|
9
|
logger, |
|
10
|
options, |
|
11
|
moduleOptions, |
|
12
|
}: ProviderLoaderOptions): Promise<void> => { |
|
13
|
const { redisUrl, redisOptions, namespace } = |
|
14
|
options as RedisCacheModuleOptions |
|
15
|
|
|
16
|
if (!redisUrl) { |
|
17
|
throw Error( |
|
18
|
`No "redisUrl" provided in "${Modules.LOCKING}" module, "locking-redis" provider options. It is required for the "locking-redis" Module provider.` |
|
19
|
) |
|
20
|
} |
|
21
|
|
|
22
|
const connection = new Redis(redisUrl, { |
|
23
|
// Lazy connect to properly handle connection errors |
|
24
|
lazyConnect: true, |
|
25
|
...(redisOptions ?? {}), |
|
26
|
}) |
|
27
|
|
|
28
|
try { |
|
29
|
await connection.connect() |
|
30
|
logger?.info(`Connection to Redis in "locking-redis" provider established`) |
|
31
|
} catch (err) { |
|
32
|
logger?.error( |
|
33
|
`An error occurred while connecting to Redis in provider "locking-redis": ${err}` |
|
34
|
) |
|
35
|
} |
|
36
|
|
|
37
|
container.register({ |
|
38
|
redisClient: asValue(connection), |
|
39
|
prefix: asValue(namespace ?? "medusa_lock:"), |
|
40
|
}) |
|
41
|
} |