1 import {
2 InternalModuleDeclaration,
3 LoaderOptions,
4 } from "@medusajs/framework/types"
5 import { asValue } from "@medusajs/framework/awilix"
6 import Redis from "ioredis"
7 import { RedisWorkflowsOptions } from "../types"
8
9 export default async (
10 { container, logger, options, dataLoaderOnly }: LoaderOptions,
11 moduleDeclaration: InternalModuleDeclaration
12 ): Promise<void> => {
13 const {
14 url,
15 redisUrl,
16 options: deprecatedRedisOptions,
17 redisOptions: newRedisOptions,
18 jobQueueName,
19 queueName,
20 // Shared options
21 queueOptions,
22 workerOptions,
23 // Per-queue options
24 mainQueueOptions,
25 mainWorkerOptions,
26 jobQueueOptions,
27 jobWorkerOptions,
28 cleanerQueueOptions,
29 cleanerWorkerOptions,
30 pubsub,
31 } = options?.redis as RedisWorkflowsOptions
32
33 // Handle backward compatibility for deprecated options
34 const resolvedUrl = redisUrl ?? url
35 const redisOptions = newRedisOptions ?? deprecatedRedisOptions
36
37 // Log deprecation warnings
38 if (url && !redisUrl) {
39 logger?.warn(
40 "[Workflow-engine-redis] The `url` option is deprecated. Please use `redisUrl` instead for consistency with other modules."
41 )
42 }
43 if (deprecatedRedisOptions && !newRedisOptions) {
44 logger?.warn(
45 "[Workflow-engine-redis] The `options` option is deprecated. Please use `redisOptions` instead for consistency with other modules."
46 )
47 }
48
49 // TODO: get default from ENV VAR
50 if (!resolvedUrl) {
51 throw Error(
52 "No `redis.redisUrl` (or deprecated `redis.url`) provided in `workflowOrchestrator` module options. It is required for the Workflow Orchestrator Redis."
53 )
54 }
55
56 const cnnPubSub = pubsub ?? { url: resolvedUrl, options: redisOptions }
57
58 const queueName_ = queueName ?? "medusa-workflows"
59 const jobQueueName_ = jobQueueName ?? "medusa-workflows-jobs"
60
61 // Resolve per-queue options by merging shared defaults with per-queue overrides
62 const resolvedMainQueueOptions = {
63 ...(queueOptions ?? {}),
64 ...(mainQueueOptions ?? {}),
65 }
66 const resolvedMainWorkerOptions = {
67 ...(workerOptions ?? {}),
68 ...(mainWorkerOptions ?? {}),
69 }
70 const resolvedJobQueueOptions = {
71 ...(queueOptions ?? {}),
72 ...(jobQueueOptions ?? {}),
73 }
74 const resolvedJobWorkerOptions = {
75 ...(workerOptions ?? {}),
76 ...(jobWorkerOptions ?? {}),
77 }
78 const resolvedCleanerQueueOptions = {
79 ...(queueOptions ?? {}),
80 ...(cleanerQueueOptions ?? {}),
81 }
82 const resolvedCleanerWorkerOptions = {
83 ...(workerOptions ?? {}),
84 ...(cleanerWorkerOptions ?? {}),
85 }
86
87 let connection
88 let redisPublisher
89 let redisSubscriber
90 let workerConnection
91
92 try {
93 connection = await getConnection(resolvedUrl, redisOptions)
94 workerConnection = await getConnection(resolvedUrl, {
95 ...(redisOptions ?? {}),
96 maxRetriesPerRequest: null,
97 })
98 logger?.info(
99 `[Workflow-engine-redis] Connection to Redis in module 'workflow-engine-redis' established`
100 )
101 } catch (err) {
102 logger?.error(
103 `[Workflow-engine-redis] An error occurred while connecting to Redis in module 'workflow-engine-redis': ${err}`
104 )
105 }
106
107 try {
108 redisPublisher = await getConnection(cnnPubSub.url, cnnPubSub.options)
109 redisSubscriber = await getConnection(cnnPubSub.url, cnnPubSub.options)
110 logger?.info(
111 `[Workflow-engine-redis] Connection to Redis PubSub in module 'workflow-engine-redis' established`
112 )
113 } catch (err) {
114 logger?.error(
115 `[Workflow-engine-redis] An error occurred while connecting to Redis PubSub in module 'workflow-engine-redis': ${err}`
116 )
117 }
118
119 container.register({
120 isWorkerMode: asValue(moduleDeclaration.worker_mode !== "server"),
121 partialLoading: asValue(true),
122 redisConnection: asValue(connection),
123 redisWorkerConnection: asValue(workerConnection),
124 redisPublisher: asValue(redisPublisher),
125 redisSubscriber: asValue(redisSubscriber),
126 redisQueueName: asValue(queueName_),
127 redisJobQueueName: asValue(jobQueueName_),
128 // Per-queue resolved options
129 redisMainQueueOptions: asValue(resolvedMainQueueOptions),
130 redisMainWorkerOptions: asValue(resolvedMainWorkerOptions),
131 redisJobQueueOptions: asValue(resolvedJobQueueOptions),
132 redisJobWorkerOptions: asValue(resolvedJobWorkerOptions),
133 redisCleanerQueueOptions: asValue(resolvedCleanerQueueOptions),
134 redisCleanerWorkerOptions: asValue(resolvedCleanerWorkerOptions),
135 redisDisconnectHandler: asValue(async () => {
136 connection.disconnect()
137 workerConnection.disconnect()
138 redisPublisher.disconnect()
139 redisSubscriber.disconnect()
140 }),
141 })
142 }
143
144 async function getConnection(url, redisOptions) {
145 const connection = new Redis(url, {
146 lazyConnect: true,
147 ...(redisOptions ?? {}),
148 })
149
150 await new Promise(async (resolve) => {
151 await connection.connect(resolve)
152 })
153
154 return connection
155 }