fix(medusa): add support for retrying failed event bus jobs (#2566)

Closes CORE-770
This commit is contained in:
Sebastian Rindom
2022-11-09 12:24:26 +00:00
committed by GitHub
parent 5ea4b728e7
commit 8069ed5e99
2 changed files with 22 additions and 2 deletions
+5
View File
@@ -0,0 +1,5 @@
---
"@medusajs/medusa": patch
---
fix(medusa): add support for retrying failed event bus jobs
+17 -2
View File
@@ -16,6 +16,15 @@ type InjectedDependencies = {
type Subscriber<T = unknown> = (data: T, eventName: string) => Promise<void>
type EmitOptions = {
delay?: number
attempts?: number
backoff?: {
type: "fixed" | "exponential"
delay: number
}
}
/**
* Can keep track of multiple subscribers to different events and run the
* subscribers when events happen. Events will run asynchronously.
@@ -179,7 +188,7 @@ export default class EventBusService {
async emit<T>(
eventName: string,
data: T,
options: { delay?: number } = {}
options: EmitOptions = {}
): Promise<StagedJob | void> {
if (this.transactionManager_) {
const stagedJobRepository = this.transactionManager_.getCustomRepository(
@@ -192,9 +201,15 @@ export default class EventBusService {
})
return await stagedJobRepository.save(stagedJobInstance)
} else {
const opts: { removeOnComplete: boolean; delay?: number } = {
const opts: { removeOnComplete: boolean } & EmitOptions = {
removeOnComplete: true,
}
if (typeof options.attempts === "number") {
opts.attempts = options.attempts
if (typeof options.backoff !== "undefined") {
opts.backoff = options.backoff
}
}
if (typeof options.delay === "number") {
opts.delay = options.delay
}