feat(locking): Locking module (#9524)

**What**
- Locking Module to manage concurrency
- Default `in-memory` provider
This commit is contained in:
Carlos R. L. Rodrigues
2024-10-11 13:30:06 -03:00
committed by GitHub
parent 5c9e289c4d
commit c8b375ae2d
28 changed files with 806 additions and 39 deletions

View File

@@ -0,0 +1,2 @@
export { default as LockingModuleService } from "./locking-module"
export { default as LockingProviderService } from "./locking-provider"

View File

@@ -0,0 +1,90 @@
import {
Context,
ILockingModule,
InternalModuleDeclaration,
} from "@medusajs/types"
import { EntityManager } from "@mikro-orm/core"
import { LockingDefaultProvider } from "@types"
import LockingProviderService from "./locking-provider"
type InjectedDependencies = {
manager: EntityManager
lockingProviderService: LockingProviderService
[LockingDefaultProvider]: string
}
export default class LockingModuleService implements ILockingModule {
protected manager: EntityManager
protected providerService_: LockingProviderService
protected defaultProviderId: string
constructor(
container: InjectedDependencies,
protected readonly moduleDeclaration: InternalModuleDeclaration
) {
this.manager = container.manager
this.providerService_ = container.lockingProviderService
this.defaultProviderId = container[LockingDefaultProvider]
}
async execute<T>(
keys: string | string[],
job: () => Promise<T>,
args?: {
timeout?: number
provider?: string
},
sharedContext: Context = {}
): Promise<T> {
const providerId = args?.provider ?? this.defaultProviderId
const provider =
this.providerService_.retrieveProviderRegistration(providerId)
return provider.execute(keys, job, args, sharedContext)
}
async acquire(
keys: string | string[],
args?: {
ownerId?: string | null
expire?: number
provider?: string
},
sharedContext: Context = {}
): Promise<void> {
const providerId = args?.provider ?? this.defaultProviderId
const provider =
this.providerService_.retrieveProviderRegistration(providerId)
await provider.acquire(keys, args, sharedContext)
}
async release(
keys: string | string[],
args?: {
ownerId?: string | null
provider?: string
},
sharedContext: Context = {}
): Promise<boolean> {
const providerId = args?.provider ?? this.defaultProviderId
const provider =
this.providerService_.retrieveProviderRegistration(providerId)
return await provider.release(keys, args, sharedContext)
}
async releaseAll(
args?: {
ownerId?: string | null
provider?: string
},
sharedContext: Context = {}
): Promise<void> {
const providerId = args?.provider ?? this.defaultProviderId
const provider =
this.providerService_.retrieveProviderRegistration(providerId)
return await provider.releaseAll(args, sharedContext)
}
}

View File

@@ -0,0 +1,40 @@
import { Constructor, ILockingProvider } from "@medusajs/framework/types"
import { MedusaError } from "@medusajs/framework/utils"
import { LockingProviderRegistrationPrefix } from "../types"
type InjectedDependencies = {
[key: `lp_${string}`]: ILockingProvider
}
export default class LockingProviderService {
protected __container__: InjectedDependencies
constructor(container: InjectedDependencies) {
this.__container__ = container
}
static getRegistrationIdentifier(
providerClass: Constructor<ILockingProvider>
) {
if (!(providerClass as any).identifier) {
throw new MedusaError(
MedusaError.Types.INVALID_ARGUMENT,
`Trying to register a locking provider without an identifier.`
)
}
return `${(providerClass as any).identifier}`
}
public retrieveProviderRegistration(providerId: string): ILockingProvider {
try {
return this.__container__[
`${LockingProviderRegistrationPrefix}${providerId}`
]
} catch (err) {
throw new MedusaError(
MedusaError.Types.NOT_FOUND,
`Could not find a locking provider with id: ${providerId}`
)
}
}
}