1 {"slug":"/references/locking-module-provider","title":"How to Create a Locking Module Provider","frontmatter":{"slug":"/references/locking-module-provider","tags":["locking","server","how to"],"sidebar_label":"Create Locking Provider","keywords":["locking","provider","integration"]},"toc":[{"title":"Implementation Example","id":"implementation-example","level":2},{"title":"1. Create Module Provider Directory","id":"1-create-module-provider-directory","level":2},{"title":"2. Create the Locking Module Provider Service","id":"2-create-the-locking-module-provider-service","level":2},{"title":"constructor","id":"constructor","level":3},{"title":"Example","id":"example","level":4},{"title":"Identifier","id":"identifier","level":3},{"title":"acquire","id":"acquire","level":3},{"title":"Example","id":"example-1","level":4},{"title":"Parameters","id":"acquire-parameters","level":4},{"title":"Returns","id":"acquire-returns","level":4},{"title":"execute","id":"execute","level":3},{"title":"Example","id":"example-2","level":4},{"title":"Type Parameters","id":"execute-type-parameters","level":4},{"title":"Parameters","id":"execute-parameters","level":4},{"title":"Returns","id":"execute-returns","level":4},{"title":"release","id":"release","level":3},{"title":"Example","id":"example-3","level":4},{"title":"Parameters","id":"release-parameters","level":4},{"title":"Returns","id":"release-returns","level":4},{"title":"releaseAll","id":"releaseall","level":3},{"title":"Example","id":"example-4","level":4},{"title":"Parameters","id":"releaseall-parameters","level":4},{"title":"Returns","id":"releaseall-returns","level":4},{"title":"3. Create Module Definition File","id":"3-create-module-definition-file","level":2},{"title":"4. Use Module Provider","id":"4-use-module-provider","level":2},{"title":"5. Test it Out","id":"5-test-it-out","level":2},{"title":"Useful Guides","id":"useful-guides","level":2}],"blocks":[{"kind":"markdown","html":"In this guide, you’ll learn how to create a Locking Module Provider and the methods you must implement in its main service."},{"kind":"markdown","html":"---"},{"kind":"heading","level":2,"text":"Implementation Example","id":"implementation-example"},{"kind":"markdown","html":"As you implement your Locking Module Provider, it can be useful to refer to an existing provider and how it's implemeted.\n\nIf you need to refer to an existing implementation as an example, check the [Redis Locking Module Provider in the Medusa repository](https://github.com/medusajs/medusa/tree/develop/packages/modules/providers/locking-redis).\n\n---"},{"kind":"heading","level":2,"text":"1. Create Module Provider Directory","id":"1-create-module-provider-directory"},{"kind":"markdown","html":"Start by creating a new directory for your module provider.\n\nIf you're creating the module provider in a Medusa application, create it under the `src/modules` directory. For example, `src/modules/my-locking`.\n\nIf you're creating the module provider in a plugin, create it under the `src/providers` directory. For example, `src/providers/my-locking`."},{"kind":"note","variant":"note","html":"The rest of this guide always uses the `src/modules/my-locking` directory as an example."},{"kind":"markdown","html":"---"},{"kind":"heading","level":2,"text":"2. Create the Locking Module Provider Service","id":"2-create-the-locking-module-provider-service"},{"kind":"markdown","html":"Create the file `src/modules/my-locking/service.ts` that holds the module provider's main service. It must implement the `ILockingProvider` interface imported from `@medusajs/framework/types`:\n\n```ts title=\"src/modules/my-locking/service.ts\"\nimport { ILockingProvider } from \"@medusajs/framework/types\"\n\ntype Options = {\n url: string\n}\n\nclass MyLockingProviderService implements ILockingProvider {\n // TODO implement methods\n}\n\nexport default MyLockingProviderService\n```"},{"kind":"heading","level":3,"text":"constructor","id":"constructor"},{"kind":"markdown","html":"The constructor allows you to access resources from the module's container using the first parameter,\nand the module's options using the second parameter.\n\nIf you're creating a client or establishing a connection with a third-party service, do it in the constructor."},{"kind":"heading","level":4,"text":"Example","id":"example"},{"kind":"markdown","html":"```ts\nimport { ILockingProvider } from \"@medusajs/framework/types\"\nimport { Logger } from \"@medusajs/framework/types\"\n\ntype InjectedDependencies = {\n logger: Logger\n}\n\ntype Options = {\n url: string\n}\n\nclass MyLockingProviderService implements ILockingProvider {\n static identifier = \"my-lock\"\n protected logger_: Logger\n protected options_: Options\n // assuming you're initializing a client\n protected client\n\n constructor (\n { logger }: InjectedDependencies,\n options: Options\n ) {\n this.logger_ = logger\n this.options_ = options\n\n // assuming you're initializing a client\n this.client = new Client(options)\n }\n\n // ...\n}\n\nexport default MyLockingProviderService\n```"},{"kind":"heading","level":3,"text":"Identifier","id":"identifier"},{"kind":"markdown","html":"Every locking module provider must have an `identifier` static property. The provider's ID\nwill be stored as `lp_{identifier}`.\n\nFor example:\n\n```ts\nclass MyLockingProviderService implements ILockingProvider {\n static identifier = \"my-lock\"\n // ...\n}\n```"},{"kind":"heading","level":3,"text":"acquire","id":"acquire"},{"kind":"markdown","html":"This method acquires a lock on the given keys. The Locking Module uses this method when you call its `acquire` method and your provider is the default provider,\nor you pass your provider's identifier to its `acquire` method.\n\nIn this method, you should only aquire the lock if the timeout hasn't passed. As explained in the [execute](/references/locking-module-provider#execute) method's example,\nyou can use a `cancellationToken` object to determine if the lock acquisition has timed out.\n\nIf the lock aquisition isn't canceled, you should aquire the lock, setting its expiry and owner. You should account for the following scenarios:\n\n- The lock doesn't have an owner and you don't pass an owner, in which case the lock can be extended or released by anyone.\n- The lock doesn't have an owner or has the same owner that you pass, in which case you can extend the lock's expiration time and set the owner.\n- The lock has an owner, but you pass a different owner, in which case the method should throw an error."},{"kind":"heading","level":4,"text":"Example","id":"example-1"},{"kind":"markdown","html":"An example of how to implement the `acquire` method:\n\n```ts\ntype ResolvablePromise = {\n promise: Promise<any>\n resolve: () => void\n}\n\nclass MyLockingProviderService implements ILockingProvider {\n // ...\n async acquire(\n keys: string | string[],\n args?: {\n ownerId?: string | null\n expire?: number\n awaitQueue?: boolean\n }\n ): Promise<void> {\n return this.acquire_(keys, args)\n }\n\n async acquire_(\n keys: string | string[],\n args?: {\n ownerId?: string | null\n expire?: number\n awaitQueue?: boolean\n },\n cancellationToken?: { cancelled: boolean }\n ): Promise<void> {\n keys = Array.isArray(keys) ? keys : [keys]\n const { ownerId, expire } = args ?? {}\n\n for (const key of keys) {\n if (cancellationToken?.cancelled) {\n throw new Error(\"Timed-out acquiring lock.\")\n }\n\n // assuming your client has this method and it validates the owner and expiration\n const result = await this.client.acquireLock(key, ownerId, expire)\n\n if (result !== 1) {\n throw new Error(`Failed to acquire lock for key \"${key}\"`)\n }\n }\n }\n}\n```\n\nIn this example, you add a private `acquire_` method that you use to acquire the lock. This method accepts an additional `cancellationToken` argument that you can use to determine if the lock acquisition has timed out.\nYou can then use this method in other methods, such as the `execute` method.\n\nIn the `acquire_` method, you loop through the keys and try to acquire the lock on each key if the lock acquisition hasn't timed out. If the lock acquisition fails, you throw an error.\nThis method assumes that the client you're integrating has a method called `acquireLock` that validates the owner and expiration time, and returns `1` if the lock is successfully acquired."},{"kind":"heading","level":4,"text":"Parameters","id":"acquire-parameters"},{"kind":"typeList","sectionTitle":"acquire","types":[{"name":"keys","type":"`string` \\| `string`[]","description":"The keys to acquire the lock on.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"args","type":"`object`","description":"Additional arguments for acquiring the lock.","optional":true,"defaultValue":"","expandable":false,"children":[{"name":"ownerId","type":"`null` \\| `string`","description":"The ID of the lock's owner. If specified, only the owner can release the lock or extend its expiration time.","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"expire","type":"`number`","description":"The expiration time (in seconds) for the lock. If the lock is already acquired and the owner is the same, the expiration time is extended\nby the value passed. If not specified, the lock does not expire.","optional":true,"defaultValue":"","expandable":false,"children":[]}]},{"name":"sharedContext","type":"[Context](/references/types/interfaces/types.Context)","description":"A context used to share resources, such as transaction manager, between the application and the module.","optional":true,"defaultValue":"","expandable":false,"children":[{"name":"transactionManager","type":"TManager","description":"An instance of a transaction manager of type `TManager`, which is a typed parameter passed to the context to specify the type of the `transactionManager`.","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"manager","type":"TManager","description":"An instance of a manager, typically an entity manager, of type `TManager`, which is a typed parameter passed to the context to specify the type of the `manager`.","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"isolationLevel","type":"`string`","description":"A string indicating the isolation level of the context. Possible values are `READ UNCOMMITTED`, `READ COMMITTED`, `REPEATABLE READ`, or `SERIALIZABLE`.","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"enableNestedTransactions","type":"`boolean`","description":"A boolean value indicating whether nested transactions are enabled.","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"eventGroupId","type":"`string`","description":"A string indicating the ID of the group to aggregate the events to be emitted at a later point.","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"transactionId","type":"`string`","description":"A string indicating the ID of the current transaction.","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"runId","type":"`string`","description":"A string indicating the ID of the current run.","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"messageAggregator","type":"[IMessageAggregator](/references/types/interfaces/types.IMessageAggregator)","description":"An instance of a message aggregator, which is used to aggregate messages to be emitted at a later point.","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"requestId","type":"`string`","description":"A string indicating the ID of the current request.","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"idempotencyKey","type":"`string`","description":"A string indicating the idempotencyKey of the current workflow execution.","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"parentStepIdempotencyKey","type":"`string`","description":"A string indicating the idempotencyKey of the parent workflow execution.","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"preventReleaseEvents","type":"`boolean`","description":"preventReleaseEvents","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"isCancelling","type":"`boolean`","description":"A boolean value indicating whether the current workflow execution is being cancelled.","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"cancelingFromParentStep","type":"`boolean`","description":"Weither or not a sub workflow cancellation is being triggered from a parent step.\nIf true, the parent step will not be triggered by the sub workflow.","optional":true,"defaultValue":"","expandable":false,"children":[]}]}]},{"kind":"heading","level":4,"text":"Returns","id":"acquire-returns"},{"kind":"typeList","sectionTitle":"acquire","types":[{"name":"Promise","type":"Promise&#60;void&#62;","optional":false,"defaultValue":"","description":"Resolves when the lock is acquired.","expandable":false,"children":[]}]},{"kind":"heading","level":3,"text":"execute","id":"execute"},{"kind":"markdown","html":"This method executes a given asynchronous job with a lock on the given keys. The Locking Module uses this method\nwhen you call its `execute` method and your provider is the default provider, or you pass your provider's identifier to its `execute` method.\n\nIn the method, you should first try to acquire the lock on the given keys before the specified timeout passes.\nThen, once the lock is acquired, you execute the job. Otherwise, if the timeout passes before the lock is acquired, you cancel the job.\n\nIf your provider's locks expire, acquire the lock with an owner that is unique to this call, and release it\nwith that same owner once the job is done. Otherwise, a job whose lock expired while it was still running\nreleases whichever lock is on the keys by then, which is the next job's.\n\nYou should also keep the lock alive while the job runs by extending its expiration periodically.\nIf you find that the lock has been lost, abort the signal you pass to the job and throw, since the job no longer has exclusive access."},{"kind":"heading","level":4,"text":"Example","id":"example-2"},{"kind":"markdown","html":"An example of how to implement the `execute` method:\n\n```ts\n// other imports...\nimport { Context } from \"@medusajs/framework/types\"\nimport { randomUUID } from \"node:crypto\"\nimport { setTimeout } from \"node:timers/promises\"\n\nclass MyLockingProviderService implements ILockingProvider {\n // ...\nasync execute<T>(\n keys: string | string[],\n job: (signal?: AbortSignal) => Promise<T>,\n args?: { timeout?: number, expire?: number },\n sharedContext?: Context\n ): Promise<T> {\n // TODO you can add actions using the third-party client you initialized in the constructor\n const timeout = Math.max(args?.timeout ?? 5, 1)\n const timeoutSeconds = Number.isNaN(timeout) ? 1 : timeout\n const expire = Math.max(args?.expire ?? 60, 1)\n const expireSeconds = Number.isNaN(expire) ? 60 : expire\n const ownerId = `execute:${randomUUID()}`\n const cancellationToken = { cancelled: false }\n const promises: Promise<any>[] = []\n\n if (timeoutSeconds > 0) {\n promises.push(this.getTimeout(timeoutSeconds, cancellationToken))\n }\n\n promises.push(\n this.acquire_(\n keys,\n {\n ownerId,\n expire: expireSeconds,\n },\n cancellationToken\n )\n )\n\n await Promise.race(promises)\n\n // TODO extend the lock's expiration every `expireSeconds / 3` seconds while\n // the job runs, and abort `renewal.signal` if the lock has been lost\n const renewal = this.renewUntilStopped(keys, ownerId, expireSeconds)\n\n try {\n const result = await job(renewal.signal)\n\n if (renewal.signal.aborted) {\n throw renewal.signal.reason\n }\n\n return result\n } finally {\n renewal.stop()\n await this.release(keys, { ownerId })\n }\n }\n\n private async getTimeout(\n seconds: number,\n cancellationToken: { cancelled: boolean }\n ): Promise<void> {\n return new Promise(async (_, reject) => {\n await setTimeout(seconds * 1000)\n cancellationToken.cancelled = true\n reject(new Error(\"Timed-out acquiring lock.\"))\n })\n }\n}\n```\n\nIn this example, you first determine the timeout for acquiring the lock and how long the lock should live once\nacquired. You also create an owner ID that is unique to this call, and a `cancellationToken` object that you'll\nuse to determine if the lock aquisition has timed out.\n\nYou then create an array of the following promises:\n\n- A timeout promise that, if the lock acquisition takes longer than the timeout, sets the `cancelled` property of the `cancellationToken` object to `true`.\n- A promise that acquires the lock. You use a private `acquire_` method which you can find its implementation in the `aquire` method's example. If the first promise\nresolves and cancels the lock acquisition, the lock will not be acquired.\n\nFinally, if the lock is acquired, you start renewing it, execute the job, and release the lock with the same owner\nID you acquired it with once the job is done, using the `release` method."},{"kind":"heading","level":4,"text":"Type Parameters","id":"execute-type-parameters"},{"kind":"typeList","sectionTitle":"execute","types":[{"name":"T","type":"`object`","description":"The type of the job's result.","optional":true,"defaultValue":"","expandable":false,"children":[]}]},{"kind":"heading","level":4,"text":"Parameters","id":"execute-parameters"},{"kind":"typeList","sectionTitle":"execute","types":[{"name":"keys","type":"`string` \\| `string`[]","description":"The keys to lock during the job's execution.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"job","type":"(`signal?`: `AbortSignal`) => Promise&#60;T&#62;","description":"The asynchronous job to execute while the keys are locked. Pass it a signal that you abort if\nthe lock is lost while the job is running.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"args","type":"`object`","description":"Additional arguments for the job execution.","optional":true,"defaultValue":"","expandable":false,"children":[{"name":"timeout","type":"`number`","description":"The timeout (in seconds) to wait for the lock to be acquired. If the timeout passes before the lock\nis acquired, the job isn't executed and an error is thrown.","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"expire","type":"`number`","description":"How long (in seconds) the lock is held before it expires, if your provider's locks can expire. The\nlock should be renewed while the job is running, so this only decides how long the keys stay locked\nwhen the process holding them dies without releasing them.","optional":true,"defaultValue":"","expandable":false,"children":[]}]},{"name":"sharedContext","type":"[Context](/references/types/interfaces/types.Context)","description":"A context used to share resources, such as transaction manager, between the application and the module.","optional":true,"defaultValue":"","expandable":false,"children":[{"name":"transactionManager","type":"TManager","description":"An instance of a transaction manager of type `TManager`, which is a typed parameter passed to the context to specify the type of the `transactionManager`.","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"manager","type":"TManager","description":"An instance of a manager, typically an entity manager, of type `TManager`, which is a typed parameter passed to the context to specify the type of the `manager`.","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"isolationLevel","type":"`string`","description":"A string indicating the isolation level of the context. Possible values are `READ UNCOMMITTED`, `READ COMMITTED`, `REPEATABLE READ`, or `SERIALIZABLE`.","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"enableNestedTransactions","type":"`boolean`","description":"A boolean value indicating whether nested transactions are enabled.","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"eventGroupId","type":"`string`","description":"A string indicating the ID of the group to aggregate the events to be emitted at a later point.","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"transactionId","type":"`string`","description":"A string indicating the ID of the current transaction.","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"runId","type":"`string`","description":"A string indicating the ID of the current run.","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"messageAggregator","type":"[IMessageAggregator](/references/types/interfaces/types.IMessageAggregator)","description":"An instance of a message aggregator, which is used to aggregate messages to be emitted at a later point.","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"requestId","type":"`string`","description":"A string indicating the ID of the current request.","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"idempotencyKey","type":"`string`","description":"A string indicating the idempotencyKey of the current workflow execution.","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"parentStepIdempotencyKey","type":"`string`","description":"A string indicating the idempotencyKey of the parent workflow execution.","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"preventReleaseEvents","type":"`boolean`","description":"preventReleaseEvents","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"isCancelling","type":"`boolean`","description":"A boolean value indicating whether the current workflow execution is being cancelled.","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"cancelingFromParentStep","type":"`boolean`","description":"Weither or not a sub workflow cancellation is being triggered from a parent step.\nIf true, the parent step will not be triggered by the sub workflow.","optional":true,"defaultValue":"","expandable":false,"children":[]}]}]},{"kind":"heading","level":4,"text":"Returns","id":"execute-returns"},{"kind":"typeList","sectionTitle":"execute","types":[{"name":"Promise","type":"Promise&#60;T&#62;","optional":false,"defaultValue":"","description":"The result of the job.","expandable":false,"children":[]}]},{"kind":"heading","level":3,"text":"release","id":"release"},{"kind":"markdown","html":"This method releases a lock on the given keys. The Locking Module uses this method when you call its `release` method and your provider is the default provider,\nor you pass your provider's identifier to its `release` method.\n\nIn this method, you should release the lock on the given keys. If the lock has an owner, you should only release the lock if the owner is the same as the one passed."},{"kind":"heading","level":4,"text":"Example","id":"example-3"},{"kind":"markdown","html":"An example of how to implement the `release` method:\n\n```ts\n// other imports...\nimport { promiseAll } from \"@medusajs/framework/utils\"\n\nclass MyLockingProviderService implements ILockingProvider {\n // ...\n async release(\n keys: string | string[],\n args?: { ownerId?: string | null },\n sharedContext?: Context\n ): Promise<boolean> {\n const ownerId = args?.ownerId ?? \"*\"\n keys = Array.isArray(keys) ? keys : [keys]\n\n const releasePromises = keys.map(async (key) => {\n // assuming your client has this method and it validates the owner\n const result = await this.client.releaseLock(key, ownerId)\n return result === 1\n })\n\n const results = await promiseAll(releasePromises)\n\n return results.every((released) => released)\n }\n}\n```\n\nIn this example, you loop through the keys and try to release the lock on each key using the client you're integrating. This implementation assumes that the client validates\nownership of the lock and returns a result of `1` if the lock is successfully released."},{"kind":"heading","level":4,"text":"Parameters","id":"release-parameters"},{"kind":"typeList","sectionTitle":"release","types":[{"name":"keys","type":"`string` \\| `string`[]","description":"The keys to release the lock from.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"args","type":"`object`","description":"Additional arguments for releasing the lock.","optional":true,"defaultValue":"","expandable":false,"children":[{"name":"ownerId","type":"`null` \\| `string`","description":"The ID of the lock's owner. The lock can be released either if it doesn't have an owner, or\nif its owner ID matches the one passed in this property.","optional":true,"defaultValue":"","expandable":false,"children":[]}]},{"name":"sharedContext","type":"[Context](/references/types/interfaces/types.Context)","description":"A context used to share resources, such as transaction manager, between the application and the module.","optional":true,"defaultValue":"","expandable":false,"children":[{"name":"transactionManager","type":"TManager","description":"An instance of a transaction manager of type `TManager`, which is a typed parameter passed to the context to specify the type of the `transactionManager`.","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"manager","type":"TManager","description":"An instance of a manager, typically an entity manager, of type `TManager`, which is a typed parameter passed to the context to specify the type of the `manager`.","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"isolationLevel","type":"`string`","description":"A string indicating the isolation level of the context. Possible values are `READ UNCOMMITTED`, `READ COMMITTED`, `REPEATABLE READ`, or `SERIALIZABLE`.","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"enableNestedTransactions","type":"`boolean`","description":"A boolean value indicating whether nested transactions are enabled.","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"eventGroupId","type":"`string`","description":"A string indicating the ID of the group to aggregate the events to be emitted at a later point.","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"transactionId","type":"`string`","description":"A string indicating the ID of the current transaction.","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"runId","type":"`string`","description":"A string indicating the ID of the current run.","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"messageAggregator","type":"[IMessageAggregator](/references/types/interfaces/types.IMessageAggregator)","description":"An instance of a message aggregator, which is used to aggregate messages to be emitted at a later point.","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"requestId","type":"`string`","description":"A string indicating the ID of the current request.","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"idempotencyKey","type":"`string`","description":"A string indicating the idempotencyKey of the current workflow execution.","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"parentStepIdempotencyKey","type":"`string`","description":"A string indicating the idempotencyKey of the parent workflow execution.","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"preventReleaseEvents","type":"`boolean`","description":"preventReleaseEvents","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"isCancelling","type":"`boolean`","description":"A boolean value indicating whether the current workflow execution is being cancelled.","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"cancelingFromParentStep","type":"`boolean`","description":"Weither or not a sub workflow cancellation is being triggered from a parent step.\nIf true, the parent step will not be triggered by the sub workflow.","optional":true,"defaultValue":"","expandable":false,"children":[]}]}]},{"kind":"heading","level":4,"text":"Returns","id":"release-returns"},{"kind":"typeList","sectionTitle":"release","types":[{"name":"Promise","type":"Promise&#60;boolean&#62;","optional":false,"defaultValue":"","description":"Whether the lock was successfully released. If the lock has a different owner than the one passed, the method returns `false`.","expandable":false,"children":[{"name":"boolean","type":"`boolean`","optional":false,"defaultValue":"","description":"","expandable":false,"children":[]}]}]},{"kind":"heading","level":3,"text":"releaseAll","id":"releaseall"},{"kind":"markdown","html":"This method releases all locks. The Locking Module uses this method when you call its `releaseAll` method and your provider is the default provider,\nor you pass your provider's identifier to its `releaseAll` method.\n\nIn this method, you should release all locks if no owner is passed. If an owner is passed, you should only release the locks that the owner has acquired."},{"kind":"heading","level":4,"text":"Example","id":"example-4"},{"kind":"markdown","html":"An example of how to implement the `releaseAll` method:\n\n```ts\nclass MyLockingProviderService implements ILockingProvider {\n // ...\n async releaseAll(\n args?: { ownerId?: string | null },\n sharedContext?: Context\n ): Promise<void> {\n const ownerId = args?.ownerId ?? \"*\"\n\n await this.client.releaseAllLock(ownerId)\n }\n}\n```\n\nIn this example, you release all locks either of all owners or the owner passed as an argument. This implementation assumes that the client you're integrating has a method called `releaseAllLock` that releases all locks\nfor all owners or a specific owner."},{"kind":"heading","level":4,"text":"Parameters","id":"releaseall-parameters"},{"kind":"typeList","sectionTitle":"releaseAll","types":[{"name":"args","type":"`object`","description":"Additional arguments for releasing the locks.","optional":true,"defaultValue":"","expandable":false,"children":[{"name":"ownerId","type":"`null` \\| `string`","description":"The ID of a lock owner. If specified, all locks that the owner has acquired are released.","optional":true,"defaultValue":"","expandable":false,"children":[]}]},{"name":"sharedContext","type":"[Context](/references/types/interfaces/types.Context)","description":"A context used to share resources, such as transaction manager, between the application and the module.","optional":true,"defaultValue":"","expandable":false,"children":[{"name":"transactionManager","type":"TManager","description":"An instance of a transaction manager of type `TManager`, which is a typed parameter passed to the context to specify the type of the `transactionManager`.","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"manager","type":"TManager","description":"An instance of a manager, typically an entity manager, of type `TManager`, which is a typed parameter passed to the context to specify the type of the `manager`.","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"isolationLevel","type":"`string`","description":"A string indicating the isolation level of the context. Possible values are `READ UNCOMMITTED`, `READ COMMITTED`, `REPEATABLE READ`, or `SERIALIZABLE`.","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"enableNestedTransactions","type":"`boolean`","description":"A boolean value indicating whether nested transactions are enabled.","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"eventGroupId","type":"`string`","description":"A string indicating the ID of the group to aggregate the events to be emitted at a later point.","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"transactionId","type":"`string`","description":"A string indicating the ID of the current transaction.","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"runId","type":"`string`","description":"A string indicating the ID of the current run.","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"messageAggregator","type":"[IMessageAggregator](/references/types/interfaces/types.IMessageAggregator)","description":"An instance of a message aggregator, which is used to aggregate messages to be emitted at a later point.","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"requestId","type":"`string`","description":"A string indicating the ID of the current request.","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"idempotencyKey","type":"`string`","description":"A string indicating the idempotencyKey of the current workflow execution.","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"parentStepIdempotencyKey","type":"`string`","description":"A string indicating the idempotencyKey of the parent workflow execution.","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"preventReleaseEvents","type":"`boolean`","description":"preventReleaseEvents","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"isCancelling","type":"`boolean`","description":"A boolean value indicating whether the current workflow execution is being cancelled.","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"cancelingFromParentStep","type":"`boolean`","description":"Weither or not a sub workflow cancellation is being triggered from a parent step.\nIf true, the parent step will not be triggered by the sub workflow.","optional":true,"defaultValue":"","expandable":false,"children":[]}]}]},{"kind":"heading","level":4,"text":"Returns","id":"releaseall-returns"},{"kind":"typeList","sectionTitle":"releaseAll","types":[{"name":"Promise","type":"Promise&#60;void&#62;","optional":false,"defaultValue":"","description":"This method releases all locks. The Locking Module uses this method when you call its `releaseAll` method and your provider is the default provider,\nor you pass your provider's identifier to its `releaseAll` method.\n\nIn this method, you should release all locks if no owner is passed. If an owner is passed, you should only release the locks that the owner has acquired.","expandable":false,"children":[]}]},{"kind":"markdown","html":"---"},{"kind":"heading","level":2,"text":"3. Create Module Definition File","id":"3-create-module-definition-file"},{"kind":"markdown","html":"Create the file `src/modules/my-locking/index.ts` with the following content:\n\n```ts title=\"src/modules/my-locking/index.ts\"\nimport { ModuleProvider, Modules } from \"@medusajs/framework/utils\"\nimport MyLockingProviderService from \"./service\"\n\nexport default ModuleProvider(Modules.LOCKING, {\n services: [MyLockingProviderService],\n})\n```\n\nThis exports the module provider's definition, indicating that the `MyLockingProviderService` is the module provider's service.\n\n---"},{"kind":"heading","level":2,"text":"4. Use Module Provider","id":"4-use-module-provider"},{"kind":"markdown","html":"To use your Locking Module Provider, add it to the `providers` array of the Locking Module in `medusa-config.ts`:\n\n```ts title=\"medusa-config.ts\"\nmodule.exports = defineConfig({\n // ...\n modules: [\n {\n resolve: \"@medusajs/medusa/locking\",\n options: {\n providers: [\n {\n // if module provider is in a plugin, use `plugin-name/providers/my-locking`\n resolve: \"./src/modules/my-locking\",\n id: \"my-lock\",\n // set this if you want this provider to be used by default\n // and you have other Locking Module Providers registered.\n is_default: true,\n options: {\n url: \"http://example.com\",\n // provider options...\n }\n },\n ]\n }\n }\n ]\n})\n```\n\n\n---"},{"kind":"heading","level":2,"text":"5. Test it Out","id":"5-test-it-out"},{"kind":"markdown","html":"When you start the Medusa application, if your Locking Module Provider is the only registered provider without enabling `is_default`, you'll see the following message:\n\n```bash\ninfo: Locking module: Using \"my-lock\" as default.\n```\n\nThis indicates that your Locking Module Provider is being used as the default provider.\n\nThe Locking Module will now use your provider to handle all locking operations.\n\n\n---"},{"kind":"heading","level":2,"text":"Useful Guides","id":"useful-guides"},{"kind":"markdown","html":"- [How to Use Locking Module](/references/locking-service)"}]}