Skip to content

Commit

Permalink
feat: make it possible to use subscribers in v2 (#6731)
Browse files Browse the repository at this point in the history
  • Loading branch information
srindom authored Mar 19, 2024
1 parent db9c460 commit 873c213
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 22 deletions.
5 changes: 5 additions & 0 deletions .changeset/polite-olives-brake.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@medusajs/medusa": patch
---

fix(medusa): make subscribers work in v2
2 changes: 1 addition & 1 deletion packages/event-bus-redis/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"@medusajs/modules-sdk": "^1.12.8",
"@medusajs/utils": "^1.11.5",
"awilix": "^8.0.0",
"bullmq": "^3.5.6",
"bullmq": "^5.4.2",
"ioredis": "^5.2.5"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ describe("RedisEventBusService", () => {
completedSubscriberIds: ["1"],
},
attemptsMade: 2,
update: (data) => data,
updateData: (data) => data,
opts: { attempts: 3 },
})
.catch((err) => void 0)
Expand Down
2 changes: 1 addition & 1 deletion packages/event-bus-redis/src/services/event-bus-redis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ export default class RedisEventBusService extends AbstractEventBusModuleService

job.data.completedSubscriberIds = updatedCompletedSubscribers

await job.update(job.data)
await job.updateData(job.data)

const errorMessage = `One or more subscribers of ${eventName} failed. Retrying...`

Expand Down
2 changes: 1 addition & 1 deletion packages/medusa/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
"awilix": "^8.0.0",
"body-parser": "^1.19.0",
"boxen": "^5.0.1",
"bullmq": "^3.5.6",
"bullmq": "^5.4.2",
"chokidar": "^3.4.2",
"class-transformer": "^0.5.1",
"class-validator": "^0.14.0",
Expand Down
12 changes: 9 additions & 3 deletions packages/medusa/src/loaders/helpers/subscribers/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
import { MedusaContainer, Subscriber } from "@medusajs/types"
import { kebabCase } from "@medusajs/utils"
import { readdir } from "fs/promises"
Expand All @@ -15,6 +16,7 @@ type SubscriberModule<T> = {
}

export class SubscriberLoader {
protected isV2_: boolean
protected container_: MedusaContainer
protected pluginOptions_: Record<string, unknown>
protected activityId_: string
Expand All @@ -32,12 +34,14 @@ export class SubscriberLoader {
rootDir: string,
container: MedusaContainer,
options: Record<string, unknown> = {},
activityId: string
activityId: string,
isV2: boolean = false
) {
this.rootDir_ = rootDir
this.pluginOptions_ = options
this.container_ = container
this.activityId_ = activityId
this.isV2_ = isV2
}

private validateSubscriber(
Expand Down Expand Up @@ -181,8 +185,10 @@ export class SubscriberLoader {
config: SubscriberConfig
handler: SubscriberHandler<T>
}) {
const eventBusService: EventBusService =
this.container_.resolve("eventBusService")
const resName = this.isV2_
? ModuleRegistrationName.EVENT_BUS
: "eventBusService"
const eventBusService: EventBusService = this.container_.resolve(resName)

const { event } = config

Expand Down
3 changes: 2 additions & 1 deletion packages/medusa/src/loaders/load-medusa-project-apis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,8 @@ async function registerSubscribers(
path.join(pluginDetails.resolve, "subscribers"),
container,
pluginDetails.options,
activityId
activityId,
true
).load()
}

Expand Down
28 changes: 14 additions & 14 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -8209,7 +8209,7 @@ __metadata:
"@medusajs/types": ^1.11.12
"@medusajs/utils": ^1.11.5
awilix: ^8.0.0
bullmq: ^3.5.6
bullmq: ^5.4.2
cross-env: ^5.2.1
ioredis: ^5.2.5
jest: ^29.6.3
Expand Down Expand Up @@ -8488,7 +8488,7 @@ __metadata:
awilix: ^8.0.0
body-parser: ^1.19.0
boxen: ^5.0.1
bullmq: ^3.5.6
bullmq: ^5.4.2
chokidar: ^3.4.2
class-transformer: ^0.5.1
class-validator: ^0.14.0
Expand Down Expand Up @@ -22283,36 +22283,36 @@ __metadata:
languageName: node
linkType: hard

"bullmq@npm:^3.5.6":
version: 3.15.8
resolution: "bullmq@npm:3.15.8"
"bullmq@npm:^5.1.3":
version: 5.1.3
resolution: "bullmq@npm:5.1.3"
dependencies:
cron-parser: ^4.6.0
glob: ^8.0.3
ioredis: ^5.3.2
lodash: ^4.17.21
msgpackr: ^1.6.2
semver: ^7.3.7
msgpackr: ^1.10.1
node-abort-controller: ^3.1.1
semver: ^7.5.4
tslib: ^2.0.0
uuid: ^9.0.0
checksum: e41429c526ec44c24891cc4533fb34e358a6ad996a001b14869b7495189c47395f9539def423e3a950f5e11a43711fdcea73592994a6387a7fcbf720d76b0442
checksum: dc2177dfd736b2d008ccab1ba9f77f80cc730ce6197c9ffa0f37327e1cf34bd8b97d83ee9f9008253ef0c0854bbd04f8c925889a3370a0899e8f5c7a34fd3ab3
languageName: node
linkType: hard

"bullmq@npm:^5.1.3":
version: 5.1.3
resolution: "bullmq@npm:5.1.3"
"bullmq@npm:^5.4.2":
version: 5.4.2
resolution: "bullmq@npm:5.4.2"
dependencies:
cron-parser: ^4.6.0
glob: ^8.0.3
ioredis: ^5.3.2
lodash: ^4.17.21
msgpackr: ^1.10.1
node-abort-controller: ^3.1.1
semver: ^7.5.4
tslib: ^2.0.0
uuid: ^9.0.0
checksum: dc2177dfd736b2d008ccab1ba9f77f80cc730ce6197c9ffa0f37327e1cf34bd8b97d83ee9f9008253ef0c0854bbd04f8c925889a3370a0899e8f5c7a34fd3ab3
checksum: 01687a41bbacb646ab9cac181181c0cf18362f23b250c332125e0a701dacefb48ff20b030e095f24bd5e471f29d514bcc02779fd1f1c3cb830a3a5f1d0a39d75
languageName: node
linkType: hard

Expand Down Expand Up @@ -39253,7 +39253,7 @@ __metadata:
languageName: node
linkType: hard

"msgpackr@npm:^1.5.4, msgpackr@npm:^1.6.2":
"msgpackr@npm:^1.5.4":
version: 1.9.5
resolution: "msgpackr@npm:1.9.5"
dependencies:
Expand Down

0 comments on commit 873c213

Please sign in to comment.