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:
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:
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:
Type Parameters#
T
objectOptionalParameters#
keys
string | string[]job
() => Promise<T>args
objectOptionalAdditional arguments for the job execution.
args
objectOptionalReturns#
Promise
Promise<T>The result of the job execution.
Promise
Promise<T>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
:
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:
Parameters#
keys
string | string[]args
objectOptionalAdditional arguments for acquiring the lock.
args
objectOptionalReturns#
Promise
Promise<void>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
:
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:
Parameters#
keys
string | string[]args
objectOptionalAdditional arguments for releasing the lock.
args
objectOptionalReturns#
Promise
Promise<boolean>Whether the lock was successfully released. If the lock has a different owner than the one passed, the method returns false
.
Promise
Promise<boolean>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
:
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:
Parameters#
args
objectOptionalAdditional arguments for releasing the locks.
args
objectOptionalReturns#
Promise
Promise<void>medusa-config.ts
) will be used.