lock changes (#9047)
This commit is contained in:
parent
a8b0dc4646
commit
07d5ddf3bb
|
@ -13,6 +13,18 @@ const getClient = async (type: LockType): Promise<Redlock> => {
|
||||||
}
|
}
|
||||||
return noRetryRedlock
|
return noRetryRedlock
|
||||||
}
|
}
|
||||||
|
case LockType.DEFAULT: {
|
||||||
|
if (!noRetryRedlock) {
|
||||||
|
noRetryRedlock = await newRedlock(OPTIONS.DEFAULT)
|
||||||
|
}
|
||||||
|
return noRetryRedlock
|
||||||
|
}
|
||||||
|
case LockType.DELAY_500: {
|
||||||
|
if (!noRetryRedlock) {
|
||||||
|
noRetryRedlock = await newRedlock(OPTIONS.DELAY_500)
|
||||||
|
}
|
||||||
|
return noRetryRedlock
|
||||||
|
}
|
||||||
default: {
|
default: {
|
||||||
throw new Error(`Could not get redlock client: ${type}`)
|
throw new Error(`Could not get redlock client: ${type}`)
|
||||||
}
|
}
|
||||||
|
@ -41,6 +53,9 @@ export const OPTIONS = {
|
||||||
// see https://www.awsarchitectureblog.com/2015/03/backoff.html
|
// see https://www.awsarchitectureblog.com/2015/03/backoff.html
|
||||||
retryJitter: 100, // time in ms
|
retryJitter: 100, // time in ms
|
||||||
},
|
},
|
||||||
|
DELAY_500: {
|
||||||
|
retryDelay: 500,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
export const newRedlock = async (opts: Options = {}) => {
|
export const newRedlock = async (opts: Options = {}) => {
|
||||||
|
@ -55,19 +70,17 @@ export const doWithLock = async (opts: LockOptions, task: any) => {
|
||||||
let lock
|
let lock
|
||||||
try {
|
try {
|
||||||
// aquire lock
|
// aquire lock
|
||||||
let name: string
|
let name: string = `lock:${tenancy.getTenantId()}_${opts.name}`
|
||||||
if (opts.systemLock) {
|
|
||||||
name = opts.name
|
|
||||||
} else {
|
|
||||||
name = `${tenancy.getTenantId()}_${opts.name}`
|
|
||||||
}
|
|
||||||
if (opts.nameSuffix) {
|
if (opts.nameSuffix) {
|
||||||
name = name + `_${opts.nameSuffix}`
|
name = name + `_${opts.nameSuffix}`
|
||||||
}
|
}
|
||||||
lock = await redlock.lock(name, opts.ttl)
|
lock = await redlock.lock(name, opts.ttl)
|
||||||
// perform locked task
|
// perform locked task
|
||||||
return task()
|
// need to await to ensure completion before unlocking
|
||||||
|
const result = await task()
|
||||||
|
return result
|
||||||
} catch (e: any) {
|
} catch (e: any) {
|
||||||
|
console.log("lock error")
|
||||||
// lock limit exceeded
|
// lock limit exceeded
|
||||||
if (e.name === "LockError") {
|
if (e.name === "LockError") {
|
||||||
if (opts.type === LockType.TRY_ONCE) {
|
if (opts.type === LockType.TRY_ONCE) {
|
||||||
|
|
|
@ -3,6 +3,7 @@ import {
|
||||||
Hosting,
|
Hosting,
|
||||||
MonthlyQuotaName,
|
MonthlyQuotaName,
|
||||||
PlanType,
|
PlanType,
|
||||||
|
PriceDuration,
|
||||||
Quotas,
|
Quotas,
|
||||||
StaticQuotaName,
|
StaticQuotaName,
|
||||||
} from "../../sdk"
|
} from "../../sdk"
|
||||||
|
@ -46,6 +47,7 @@ export interface Account extends CreateAccount {
|
||||||
tier: string // deprecated
|
tier: string // deprecated
|
||||||
planType?: PlanType
|
planType?: PlanType
|
||||||
planTier?: number
|
planTier?: number
|
||||||
|
planDuration?: PriceDuration
|
||||||
stripeCustomerId?: string
|
stripeCustomerId?: string
|
||||||
licenseKey?: string
|
licenseKey?: string
|
||||||
licenseKeyActivatedAt?: number
|
licenseKeyActivatedAt?: number
|
||||||
|
|
|
@ -4,11 +4,14 @@ export enum LockType {
|
||||||
* No retries will take place and no error will be thrown.
|
* No retries will take place and no error will be thrown.
|
||||||
*/
|
*/
|
||||||
TRY_ONCE = "try_once",
|
TRY_ONCE = "try_once",
|
||||||
|
DEFAULT = "default",
|
||||||
|
DELAY_500 = "delay_500",
|
||||||
}
|
}
|
||||||
|
|
||||||
export enum LockName {
|
export enum LockName {
|
||||||
MIGRATIONS = "migrations",
|
MIGRATIONS = "migrations",
|
||||||
TRIGGER_QUOTA = "trigger_quota",
|
TRIGGER_QUOTA = "trigger_quota",
|
||||||
|
SYNC_ACCOUNT_LICENSE = "sync_account_license",
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface LockOptions {
|
export interface LockOptions {
|
||||||
|
|
Loading…
Reference in New Issue