From 07d5ddf3bb837ced7bb6e0ba9a684b6d9047964f Mon Sep 17 00:00:00 2001 From: Martin McKeaveney Date: Thu, 15 Dec 2022 12:45:53 +0000 Subject: [PATCH] lock changes (#9047) --- packages/backend-core/src/redis/redlock.ts | 27 ++++++++++++++----- .../types/src/documents/account/account.ts | 2 ++ packages/types/src/sdk/locks.ts | 3 +++ 3 files changed, 25 insertions(+), 7 deletions(-) diff --git a/packages/backend-core/src/redis/redlock.ts b/packages/backend-core/src/redis/redlock.ts index 586302c9b1..54b2c0a8d1 100644 --- a/packages/backend-core/src/redis/redlock.ts +++ b/packages/backend-core/src/redis/redlock.ts @@ -13,6 +13,18 @@ const getClient = async (type: LockType): Promise => { } 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: { 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 retryJitter: 100, // time in ms }, + DELAY_500: { + retryDelay: 500, + }, } export const newRedlock = async (opts: Options = {}) => { @@ -55,19 +70,17 @@ export const doWithLock = async (opts: LockOptions, task: any) => { let lock try { // aquire lock - let name: string - if (opts.systemLock) { - name = opts.name - } else { - name = `${tenancy.getTenantId()}_${opts.name}` - } + let name: string = `lock:${tenancy.getTenantId()}_${opts.name}` if (opts.nameSuffix) { name = name + `_${opts.nameSuffix}` } lock = await redlock.lock(name, opts.ttl) // perform locked task - return task() + // need to await to ensure completion before unlocking + const result = await task() + return result } catch (e: any) { + console.log("lock error") // lock limit exceeded if (e.name === "LockError") { if (opts.type === LockType.TRY_ONCE) { diff --git a/packages/types/src/documents/account/account.ts b/packages/types/src/documents/account/account.ts index 70c3061c3f..cf92002744 100644 --- a/packages/types/src/documents/account/account.ts +++ b/packages/types/src/documents/account/account.ts @@ -3,6 +3,7 @@ import { Hosting, MonthlyQuotaName, PlanType, + PriceDuration, Quotas, StaticQuotaName, } from "../../sdk" @@ -46,6 +47,7 @@ export interface Account extends CreateAccount { tier: string // deprecated planType?: PlanType planTier?: number + planDuration?: PriceDuration stripeCustomerId?: string licenseKey?: string licenseKeyActivatedAt?: number diff --git a/packages/types/src/sdk/locks.ts b/packages/types/src/sdk/locks.ts index e2f1902762..e6809319b1 100644 --- a/packages/types/src/sdk/locks.ts +++ b/packages/types/src/sdk/locks.ts @@ -4,11 +4,14 @@ export enum LockType { * No retries will take place and no error will be thrown. */ TRY_ONCE = "try_once", + DEFAULT = "default", + DELAY_500 = "delay_500", } export enum LockName { MIGRATIONS = "migrations", TRIGGER_QUOTA = "trigger_quota", + SYNC_ACCOUNT_LICENSE = "sync_account_license", } export interface LockOptions {