feat(medusa): Event bus db failure handling (#4963)
* feat(medusa): Event bus db failure handling * naming * Create olive-years-heal.md
This commit is contained in:
1 parent
9781089ca3
commit
b71776738c
2 files changed
+40
-28
No files matched your search
@@ -0,0 +1,5 @@
|
|||||||
|
---
|
||||||
|
"@medusajs/medusa": patch
|
||||||
|
---
|
||||||
|
|
||||||
|
feat(medusa): Event bus db failure handling
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
import { EventBusTypes } from "@medusajs/types"
|
import { EventBusTypes, Logger } from "@medusajs/types"
|
||||||
import { DatabaseErrorCode, EventBusUtils } from "@medusajs/utils"
|
import { DatabaseErrorCode, EventBusUtils } from "@medusajs/utils"
|
||||||
import { EntityManager } from "typeorm"
|
import { EntityManager } from "typeorm"
|
||||||
import { TransactionBaseService } from "../interfaces"
|
import { TransactionBaseService } from "../interfaces"
|
||||||
@@ -7,10 +7,13 @@ import { ConfigModule } from "../types/global"
|
|||||||
import { isString } from "../utils"
|
import { isString } from "../utils"
|
||||||
import { sleep } from "../utils/sleep"
|
import { sleep } from "../utils/sleep"
|
||||||
import StagedJobService from "./staged-job"
|
import StagedJobService from "./staged-job"
|
||||||
|
import { FindConfig } from "../types/common"
|
||||||
|
import { EOL } from "os"
|
||||||
|
|
||||||
type InjectedDependencies = {
|
type InjectedDependencies = {
|
||||||
stagedJobService: StagedJobService
|
stagedJobService: StagedJobService
|
||||||
eventBusModuleService: EventBusUtils.AbstractEventBusModuleService
|
eventBusModuleService: EventBusUtils.AbstractEventBusModuleService
|
||||||
|
logger: Logger
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -25,18 +28,20 @@ export default class EventBusService
|
|||||||
protected readonly stagedJobService_: StagedJobService
|
protected readonly stagedJobService_: StagedJobService
|
||||||
// eslint-disable-next-line max-len
|
// eslint-disable-next-line max-len
|
||||||
protected readonly eventBusModuleService_: EventBusTypes.IEventBusModuleService
|
protected readonly eventBusModuleService_: EventBusTypes.IEventBusModuleService
|
||||||
|
protected readonly logger_: Logger
|
||||||
|
|
||||||
protected shouldEnqueuerRun: boolean
|
protected shouldEnqueuerRun: boolean
|
||||||
protected enqueue_: Promise<void>
|
protected enqueue_: Promise<void>
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
{ stagedJobService, eventBusModuleService }: InjectedDependencies,
|
{ stagedJobService, eventBusModuleService, logger }: InjectedDependencies,
|
||||||
config,
|
config,
|
||||||
isSingleton = true
|
isSingleton = true
|
||||||
) {
|
) {
|
||||||
// eslint-disable-next-line prefer-rest-params
|
// eslint-disable-next-line prefer-rest-params
|
||||||
super(arguments[0])
|
super(arguments[0])
|
||||||
|
|
||||||
|
this.logger_ = logger
|
||||||
this.config_ = config
|
this.config_ = config
|
||||||
this.eventBusModuleService_ = eventBusModuleService
|
this.eventBusModuleService_ = eventBusModuleService
|
||||||
this.stagedJobService_ = stagedJobService
|
this.stagedJobService_ = stagedJobService
|
||||||
@@ -190,35 +195,37 @@ export default class EventBusService
|
|||||||
}
|
}
|
||||||
|
|
||||||
while (this.shouldEnqueuerRun) {
|
while (this.shouldEnqueuerRun) {
|
||||||
try {
|
await sleep(3000)
|
||||||
const jobs = await this.stagedJobService_.list(listConfig)
|
|
||||||
|
|
||||||
if (!jobs.length) {
|
const jobs = await this.listJobs(listConfig)
|
||||||
await sleep(3000)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
const eventsData = jobs.map((job) => {
|
if (!jobs.length) {
|
||||||
return {
|
continue
|
||||||
eventName: job.event_name,
|
|
||||||
data: job.data,
|
|
||||||
options: { jobId: job.id, ...job.options },
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
await this.eventBusModuleService_.emit(eventsData).then(async () => {
|
|
||||||
return await this.stagedJobService_.delete(jobs.map((j) => j.id))
|
|
||||||
})
|
|
||||||
|
|
||||||
await sleep(3000)
|
|
||||||
} catch (err) {
|
|
||||||
if (DatabaseErrorCode.connectionFailure === err.code) {
|
|
||||||
await sleep(3000)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
throw err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const eventsData = jobs.map((job) => {
|
||||||
|
return {
|
||||||
|
eventName: job.event_name,
|
||||||
|
data: job.data,
|
||||||
|
options: { jobId: job.id, ...job.options },
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
await this.eventBusModuleService_.emit(eventsData).then(async () => {
|
||||||
|
return await this.stagedJobService_.delete(jobs.map((j) => j.id))
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected async listJobs(listConfig: FindConfig<StagedJob>) {
|
||||||
|
return await this.stagedJobService_.list(listConfig).catch((err) => {
|
||||||
|
if (DatabaseErrorCode.connectionFailure === err.code) {
|
||||||
|
this.logger_.warn(`Database connection failure:${EOL}${err.message}`)
|
||||||
|
} else {
|
||||||
|
this.logger_.warn(`Failed to fetch jobs:${EOL}${err.message}`)
|
||||||
|
}
|
||||||
|
|
||||||
|
return []
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user