Allow locks without TTL
This commit is contained in:
parent
4326ee17d9
commit
f71e1ac03a
|
@ -4,6 +4,7 @@ import { LockOptions, LockType } from "@budibase/types"
|
|||
import * as context from "../context"
|
||||
import env from "../environment"
|
||||
import { logWarn } from "../logging"
|
||||
import { Duration } from "../utils"
|
||||
|
||||
async function getClient(
|
||||
type: LockType,
|
||||
|
@ -105,12 +106,21 @@ export async function doWithLock<T>(
|
|||
task: () => Promise<T>
|
||||
): Promise<RedlockExecution<T>> {
|
||||
const redlock = await getClient(opts.type, opts.customOptions)
|
||||
let lock
|
||||
let lock: Redlock.Lock | undefined
|
||||
let interval
|
||||
try {
|
||||
const name = getLockName(opts)
|
||||
|
||||
let ttl = opts.ttl || Duration.fromSeconds(15).toMs()
|
||||
|
||||
// create the lock
|
||||
lock = await redlock.lock(name, opts.ttl)
|
||||
lock = await redlock.lock(name, ttl)
|
||||
|
||||
if (!opts.ttl) {
|
||||
interval = setInterval(() => {
|
||||
lock!.extend(ttl)
|
||||
}, ttl / 2)
|
||||
}
|
||||
|
||||
// perform locked task
|
||||
// need to await to ensure completion before unlocking
|
||||
|
@ -134,5 +144,8 @@ export async function doWithLock<T>(
|
|||
if (lock) {
|
||||
await lock.unlock()
|
||||
}
|
||||
if (interval) {
|
||||
clearInterval(interval)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -36,9 +36,9 @@ export interface LockOptions {
|
|||
*/
|
||||
name: LockName
|
||||
/**
|
||||
* The ttl to auto-expire the lock if not unlocked manually
|
||||
* The ttl to auto-expire the lock if not unlocked manually. If undefined, the lock will be autoextending while the process is running.
|
||||
*/
|
||||
ttl: number
|
||||
ttl?: number
|
||||
/**
|
||||
* The individual resource to lock. This is useful for locking around very specific identifiers, e.g. a document that is prone to conflicts
|
||||
*/
|
||||
|
|
Loading…
Reference in New Issue