How to Use Locking Module

In this document, you’ll learn about the different methods in the Locking Module's service and how to use them.


Resolve Locking Module's Service#

In your workflow's step, you can resolve the Locking Module's service from the Medusa container:

Code
1import { Modules } from "@medusajs/framework/utils"2import { createStep } from "@medusajs/framework/workflows-sdk"3
4const step1 = createStep(5  "step-1",6  async ({}, { container }) => {7    const lockingModuleService = container.resolve(8      Modules.LOCKING9    )10    11    // TODO use lockingModuleService12  } 13)

You can then use the Locking Module's service's methods in the step. The rest of this guide details these methods.


execute#

This method executes a giuven asynchronous job with a lock on the given keys. You can optionally pass a provider name to be used for locking. If no provider is passed, the default provider (in-memory or the provider configuerd in medusa-config.ts) will be used.

Example#

For example, to use the lock module when deleting a product:

Code
1await lockingModuleService.execute("prod_123", async () => {2   // assuming you've resolved the product service from the container3   await productModuleService.delete("prod_123")4})

In the above example, the product of ID prod_123 is locked while it's being deleted.

To specify the provider to use for locking, you can pass the provider name in the args argument:

Code
1await lockingModuleService.execute("prod_123", async () => {2  // assuming you've resolved the product service from the container3  await productModuleService.delete("prod_123")4}, {5  provider: "lp_my-lock"6})

Type Parameters#

TobjectOptional
The type of the job's result.

Parameters#

keysstring | string[]
The keys to lock durng the job's execution.
job() => Promise<T>
The asynchronous job to execute while the keys are locked.
argsobjectOptional
Additional arguments for the job execution.
sharedContextContextOptional
A context used to share resources, such as transaction manager, between the application and the module.

Returns#

PromisePromise<T>
The result of the job execution.

acquire#

This method acquires a lock on the given keys. You can optionally pass a provider name to be used for locking. If no provider is passed, the default provider (in-memory or the provider configuerd in medusa-config.ts) will be used.

You can pass an owner for the lock, which limits who can extend or release the acquired lock. Then, if you use this method again passing the same owner, the lock's expiration time is extended with the value passed in the expire argument. Otherwise, if you pass a different owner, the method throws an error.

Example#

For example, to acquire a lock on a product with ID prod_123 for a user with ID user_123:

Code
1await lockingModuleService.acquire("prod_123", {2  ownerId: "user_123",3  expire: 604})

In this example, you acquire a lock on the product with ID prod_123 for the user with ID user_123. You extend the lock's expiration time by 60 seconds.

To specify the provider to use for locking, you can pass the provider name in the args argument:

Code
1await lockingModuleService.acquire("prod_123", {2  ownerId: "user_123",3  expire: 60,4  provider: "lp_my-lock"5})

Parameters#

keysstring | string[]
The keys to acquire the lock on.
argsobjectOptional
Additional arguments for acquiring the lock.
sharedContextContextOptional
A context used to share resources, such as transaction manager, between the application and the module.

Returns#

PromisePromise<void>
Resolves when the lock is acquired.

release#

This method releases a lock on the given keys. You can optionally pass a provider name to be used for locking. If no provider is passed, the default provider (in-memory or the provider configuerd in medusa-config.ts) will be used.

If the lock has an owner, you must pass the same owner to release the lock.

Example#

For example, to release a lock on a product with ID prod_123 for a user with ID user_123:

Code
1await lockingModuleService.release("prod_123", {2  ownerId: "user_123"3})

In this example, you release the lock on the product with ID prod_123 for the user with ID user_123.

To specify the provider to use for locking, you can pass the provider name in the args argument:

Code
1await lockingModuleService.release("prod_123", {2  ownerId: "user_123",3  provider: "lp_my-lock"4})

Parameters#

keysstring | string[]
The keys to release the lock from.
argsobjectOptional
Additional arguments for releasing the lock.
sharedContextContextOptional
A context used to share resources, such as transaction manager, between the application and the module.

Returns#

PromisePromise<boolean>
Whether the lock was successfully released. If the lock has a different owner than the one passed, the method returns false.

releaseAll#

This method releases all locks. If you specify an owner ID, then all locks that the owner has acquired are released.

You can also pass a provider name to be used for locking. If no provider is passed, the default provider (in-memory or the provider configuerd in medusa-config.ts) will be used.

Example#

For example, to release all locks for a user with ID user_123:

Code
1await lockingModuleService.releaseAll({2  ownerId: "user_123"3})

In this example, you release all locks for the user with ID user_123.

To specify the provider to use for locking, you can pass the provider name in the args argument:

Code
1await lockingModuleService.releaseAll({2  ownerId: "user_123",3  provider: "lp_my-lock"4})

Parameters#

argsobjectOptional
Additional arguments for releasing the locks.
sharedContextContextOptional
A context used to share resources, such as transaction manager, between the application and the module.

Returns#

PromisePromise<void>
This method releases all locks. If you specify an owner ID, then all locks that the owner has acquired are released. You can also pass a provider name to be used for locking. If no provider is passed, the default provider (in-memory or the provider configuerd in medusa-config.ts) will be used.
Was this page helpful?
Ask Anything
FAQ
What is Medusa?
How can I create a module?
How can I create a data model?
How do I create a workflow?
How can I extend a data model in the Product Module?
Recipes
How do I build a marketplace with Medusa?
How do I build digital products with Medusa?
How do I build subscription-based purchases with Medusa?
What other recipes are available in the Medusa documentation?
Chat is cleared on refresh
Line break