Skip to content

Latest commit

 

History

History
86 lines (53 loc) · 2.73 KB

README_STORAGE_LOCK.md

File metadata and controls

86 lines (53 loc) · 2.73 KB

ConstructorOptions

Properties

  • client MongoClient? configured mongo client to use. Can be null if url is set
  • databaseName string? name of the mongodb database
  • connectTimeout number? the connect timeout of the mongo db client if client was not passed

Examples

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
})

ResourceLock

Creates lock documents and allows to execute functions with a lock.

lock

Creates an entry in the locks collection. The context param is a unique identifier. If context already exists, the method will throw.

Parameters

unlock

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.

Parameters

doWhileLocked

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.

Parameters

  • resourceIds Array<string> the resource ids

  • callback Function callback to execute with lock

  • options Object (optional, default {})

    • options.lockTimeout Number max time to wait in milliseconds (optional, default 5_000)
    • options.waitTimeout Number max time to wait in milliseconds (optional, default 5_000)
    • options.retryInterval Number max time to wait in between retries (optional, default 125)
  • 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

close

Closes the database client.

Returns void