client
MongoClient? configured mongo client to use. Can be null if url is setdatabaseName
string? name of the mongodb databaseconnectTimeout
number? the connect timeout of the mongo db client if client was not passed
const { ResourceLock } = require('@discue/mongodb-resource-client')
const url = 'mongodb://127.0.0.1:27017'
const lock = new ResourceLock({
url
})
await lock.doWhileLocked([123], () => {
// do important stuff while lock is being held for 5s by default
})
Creates lock documents and allows to execute functions with a lock.
Creates an entry in the locks
collection. The context param is a unique identifier. If context already
exists, the method will throw.
Deletes an entry from the locks
collection unlocking the document.
The context param is a unique identifier. If context has already been
removed, the method will throw.
Executes the callback only if the appropriate lock document has been created successfully.
Unlocks the document either after completion of the callback or after lockTimeout
millis
have passed.
- Throws Error Unable to establish lock - if unable to establish lock for a document
- Throws Error Lock interrupted by timeout - if callback did not return before lockTimeout
Closes the database client.
Returns void