Merge pull request #8164 from Budibase/feature/quota-emails
Approaching and Exceeded Usage Limit Notifications
This commit is contained in:
commit
281eb151e2
|
@ -62,6 +62,7 @@
|
|||
]
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/chance": "1.1.3",
|
||||
"@types/jest": "27.5.1",
|
||||
"@types/koa": "2.0.52",
|
||||
"@types/lodash": "4.14.180",
|
||||
|
@ -73,6 +74,7 @@
|
|||
"@types/tar-fs": "2.0.1",
|
||||
"@types/uuid": "8.3.4",
|
||||
"ioredis-mock": "5.8.0",
|
||||
"chance": "1.1.3",
|
||||
"jest": "27.5.1",
|
||||
"koa": "2.7.0",
|
||||
"nodemon": "2.0.16",
|
||||
|
|
|
@ -37,6 +37,7 @@ const core = {
|
|||
db,
|
||||
...dbConstants,
|
||||
redis,
|
||||
locks: redis.redlock,
|
||||
objectStore,
|
||||
utils,
|
||||
users,
|
||||
|
|
|
@ -3,9 +3,11 @@
|
|||
import Client from "../redis"
|
||||
import utils from "../redis/utils"
|
||||
import clients from "../redis/init"
|
||||
import * as redlock from "../redis/redlock"
|
||||
|
||||
export = {
|
||||
Client,
|
||||
utils,
|
||||
clients,
|
||||
redlock,
|
||||
}
|
||||
|
|
|
@ -1,27 +1,23 @@
|
|||
const Client = require("./index")
|
||||
const utils = require("./utils")
|
||||
const { getRedlock } = require("./redlock")
|
||||
|
||||
let userClient, sessionClient, appClient, cacheClient, writethroughClient
|
||||
let migrationsRedlock
|
||||
|
||||
// turn retry off so that only one instance can ever hold the lock
|
||||
const migrationsRedlockConfig = { retryCount: 0 }
|
||||
let userClient,
|
||||
sessionClient,
|
||||
appClient,
|
||||
cacheClient,
|
||||
writethroughClient,
|
||||
lockClient
|
||||
|
||||
async function init() {
|
||||
userClient = await new Client(utils.Databases.USER_CACHE).init()
|
||||
sessionClient = await new Client(utils.Databases.SESSIONS).init()
|
||||
appClient = await new Client(utils.Databases.APP_METADATA).init()
|
||||
cacheClient = await new Client(utils.Databases.GENERIC_CACHE).init()
|
||||
lockClient = await new Client(utils.Databases.LOCKS).init()
|
||||
writethroughClient = await new Client(
|
||||
utils.Databases.WRITE_THROUGH,
|
||||
utils.SelectableDatabases.WRITE_THROUGH
|
||||
).init()
|
||||
// pass the underlying ioredis client to redlock
|
||||
migrationsRedlock = getRedlock(
|
||||
cacheClient.getClient(),
|
||||
migrationsRedlockConfig
|
||||
)
|
||||
}
|
||||
|
||||
process.on("exit", async () => {
|
||||
|
@ -30,6 +26,7 @@ process.on("exit", async () => {
|
|||
if (appClient) await appClient.finish()
|
||||
if (cacheClient) await cacheClient.finish()
|
||||
if (writethroughClient) await writethroughClient.finish()
|
||||
if (lockClient) await lockClient.finish()
|
||||
})
|
||||
|
||||
module.exports = {
|
||||
|
@ -63,10 +60,10 @@ module.exports = {
|
|||
}
|
||||
return writethroughClient
|
||||
},
|
||||
getMigrationsRedlock: async () => {
|
||||
if (!migrationsRedlock) {
|
||||
getLockClient: async () => {
|
||||
if (!lockClient) {
|
||||
await init()
|
||||
}
|
||||
return migrationsRedlock
|
||||
return lockClient
|
||||
},
|
||||
}
|
||||
|
|
|
@ -1,14 +1,37 @@
|
|||
import Redlock from "redlock"
|
||||
import Redlock, { Options } from "redlock"
|
||||
import { getLockClient } from "./init"
|
||||
import { LockOptions, LockType } from "@budibase/types"
|
||||
import * as tenancy from "../tenancy"
|
||||
|
||||
export const getRedlock = (redisClient: any, opts = { retryCount: 10 }) => {
|
||||
return new Redlock([redisClient], {
|
||||
let noRetryRedlock: Redlock | undefined
|
||||
|
||||
const getClient = async (type: LockType): Promise<Redlock> => {
|
||||
switch (type) {
|
||||
case LockType.TRY_ONCE: {
|
||||
if (!noRetryRedlock) {
|
||||
noRetryRedlock = await newRedlock(OPTIONS.TRY_ONCE)
|
||||
}
|
||||
return noRetryRedlock
|
||||
}
|
||||
default: {
|
||||
throw new Error(`Could not get redlock client: ${type}`)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export const OPTIONS = {
|
||||
TRY_ONCE: {
|
||||
// immediately throws an error if the lock is already held
|
||||
retryCount: 0,
|
||||
},
|
||||
DEFAULT: {
|
||||
// the expected clock drift; for more details
|
||||
// see http://redis.io/topics/distlock
|
||||
driftFactor: 0.01, // multiplied by lock ttl to determine drift time
|
||||
|
||||
// the max number of times Redlock will attempt
|
||||
// to lock a resource before erroring
|
||||
retryCount: opts.retryCount,
|
||||
retryCount: 10,
|
||||
|
||||
// the time in ms between attempts
|
||||
retryDelay: 200, // time in ms
|
||||
|
@ -16,6 +39,45 @@ export const getRedlock = (redisClient: any, opts = { retryCount: 10 }) => {
|
|||
// the max time in ms randomly added to retries
|
||||
// to improve performance under high contention
|
||||
// see https://www.awsarchitectureblog.com/2015/03/backoff.html
|
||||
retryJitter: 200, // time in ms
|
||||
})
|
||||
retryJitter: 100, // time in ms
|
||||
},
|
||||
}
|
||||
|
||||
export const newRedlock = async (opts: Options = {}) => {
|
||||
let options = { ...OPTIONS.DEFAULT, ...opts }
|
||||
const redisWrapper = await getLockClient()
|
||||
const client = redisWrapper.getClient()
|
||||
return new Redlock([client], options)
|
||||
}
|
||||
|
||||
export const doWithLock = async (opts: LockOptions, task: any) => {
|
||||
const redlock = await getClient(opts.type)
|
||||
let lock
|
||||
try {
|
||||
// aquire lock
|
||||
let name: string = `${tenancy.getTenantId()}_${opts.name}`
|
||||
if (opts.nameSuffix) {
|
||||
name = name + `_${opts.nameSuffix}`
|
||||
}
|
||||
lock = await redlock.lock(name, opts.ttl)
|
||||
// perform locked task
|
||||
return task()
|
||||
} catch (e: any) {
|
||||
// lock limit exceeded
|
||||
if (e.name === "LockError") {
|
||||
if (opts.type === LockType.TRY_ONCE) {
|
||||
// don't throw for try-once locks, they will always error
|
||||
// due to retry count (0) exceeded
|
||||
return
|
||||
} else {
|
||||
throw e
|
||||
}
|
||||
} else {
|
||||
throw e
|
||||
}
|
||||
} finally {
|
||||
if (lock) {
|
||||
await lock.unlock()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -28,6 +28,7 @@ exports.Databases = {
|
|||
LICENSES: "license",
|
||||
GENERIC_CACHE: "data_cache",
|
||||
WRITE_THROUGH: "writeThrough",
|
||||
LOCKS: "locks",
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -0,0 +1,23 @@
|
|||
import { generator, uuid } from "."
|
||||
import { AuthType, CloudAccount, Hosting } from "@budibase/types"
|
||||
import * as db from "../../../src/db/utils"
|
||||
|
||||
export const cloudAccount = (): CloudAccount => {
|
||||
return {
|
||||
accountId: uuid(),
|
||||
createdAt: Date.now(),
|
||||
verified: true,
|
||||
verificationSent: true,
|
||||
tier: "",
|
||||
email: generator.email(),
|
||||
tenantId: generator.word(),
|
||||
hosting: Hosting.CLOUD,
|
||||
authType: AuthType.PASSWORD,
|
||||
password: generator.word(),
|
||||
tenantName: generator.word(),
|
||||
name: generator.name(),
|
||||
size: "10+",
|
||||
profession: "Software Engineer",
|
||||
budibaseUserId: db.generateGlobalUserID(),
|
||||
}
|
||||
}
|
|
@ -0,0 +1 @@
|
|||
export { v4 as uuid } from "uuid"
|
|
@ -1 +1,8 @@
|
|||
export * from "./common"
|
||||
|
||||
import Chance from "chance"
|
||||
export const generator = new Chance()
|
||||
|
||||
export * as koa from "./koa"
|
||||
export * as accounts from "./accounts"
|
||||
export * as licenses from "./licenses"
|
||||
|
|
|
@ -0,0 +1,18 @@
|
|||
import { AccountPlan, License, PlanType, Quotas } from "@budibase/types"
|
||||
|
||||
const newPlan = (type: PlanType = PlanType.FREE): AccountPlan => {
|
||||
return {
|
||||
type,
|
||||
}
|
||||
}
|
||||
|
||||
export const newLicense = (opts: {
|
||||
quotas: Quotas
|
||||
planType?: PlanType
|
||||
}): License => {
|
||||
return {
|
||||
features: [],
|
||||
quotas: opts.quotas,
|
||||
plan: newPlan(opts.planType),
|
||||
}
|
||||
}
|
|
@ -663,6 +663,11 @@
|
|||
"@types/connect" "*"
|
||||
"@types/node" "*"
|
||||
|
||||
"@types/chance@1.1.3":
|
||||
version "1.1.3"
|
||||
resolved "https://registry.yarnpkg.com/@types/chance/-/chance-1.1.3.tgz#d19fe9391288d60fdccd87632bfc9ab2b4523fea"
|
||||
integrity sha512-X6c6ghhe4/sQh4XzcZWSFaTAUOda38GQHmq9BUanYkOE/EO7ZrkazwKmtsj3xzTjkLWmwULE++23g3d3CCWaWw==
|
||||
|
||||
"@types/connect@*":
|
||||
version "3.4.35"
|
||||
resolved "https://registry.yarnpkg.com/@types/connect/-/connect-3.4.35.tgz#5fcf6ae445e4021d1fc2219a4873cc73a3bb2ad1"
|
||||
|
@ -1555,6 +1560,11 @@ chalk@^4.0.0, chalk@^4.1.0:
|
|||
ansi-styles "^4.1.0"
|
||||
supports-color "^7.1.0"
|
||||
|
||||
chance@1.1.3:
|
||||
version "1.1.3"
|
||||
resolved "https://registry.yarnpkg.com/chance/-/chance-1.1.3.tgz#414f08634ee479c7a316b569050ea20751b82dd3"
|
||||
integrity sha512-XeJsdoVAzDb1WRPRuMBesRSiWpW1uNTo5Fd7mYxPJsAfgX71+jfuCOHOdbyBz2uAUZ8TwKcXgWk3DMedFfJkbg==
|
||||
|
||||
char-regex@^1.0.2:
|
||||
version "1.0.2"
|
||||
resolved "https://registry.yarnpkg.com/char-regex/-/char-regex-1.0.2.tgz#d744358226217f981ed58f479b1d6bcc29545dcf"
|
||||
|
|
|
@ -17,7 +17,6 @@ import {
|
|||
getProdAppDB,
|
||||
getDevAppDB,
|
||||
} from "@budibase/backend-core/context"
|
||||
import { quotas } from "@budibase/pro"
|
||||
import { events } from "@budibase/backend-core"
|
||||
|
||||
// the max time we can wait for an invalidation to complete before considering it failed
|
||||
|
|
|
@ -1,5 +1,11 @@
|
|||
import { migrations, redis } from "@budibase/backend-core"
|
||||
import { Migration, MigrationOptions, MigrationName } from "@budibase/types"
|
||||
import { locks, migrations } from "@budibase/backend-core"
|
||||
import {
|
||||
Migration,
|
||||
MigrationOptions,
|
||||
MigrationName,
|
||||
LockType,
|
||||
LockName,
|
||||
} from "@budibase/types"
|
||||
import env from "../environment"
|
||||
|
||||
// migration functions
|
||||
|
@ -86,33 +92,14 @@ export const migrate = async (options?: MigrationOptions) => {
|
|||
}
|
||||
|
||||
const migrateWithLock = async (options?: MigrationOptions) => {
|
||||
// get a new lock client
|
||||
const redlock = await redis.clients.getMigrationsRedlock()
|
||||
// lock for 15 minutes
|
||||
const ttl = 1000 * 60 * 15
|
||||
|
||||
let migrationLock
|
||||
|
||||
// acquire lock
|
||||
try {
|
||||
migrationLock = await redlock.lock("migrations", ttl)
|
||||
} catch (e: any) {
|
||||
if (e.name === "LockError") {
|
||||
return
|
||||
} else {
|
||||
throw e
|
||||
await locks.doWithLock(
|
||||
{
|
||||
type: LockType.TRY_ONCE,
|
||||
name: LockName.MIGRATIONS,
|
||||
ttl: 1000 * 60 * 15, // auto expire the migration lock after 15 minutes
|
||||
},
|
||||
async () => {
|
||||
await migrations.runMigrations(MIGRATIONS, options)
|
||||
}
|
||||
}
|
||||
|
||||
// run migrations
|
||||
try {
|
||||
await migrations.runMigrations(MIGRATIONS, options)
|
||||
} finally {
|
||||
// release lock
|
||||
try {
|
||||
await migrationLock.unlock()
|
||||
} catch (e) {
|
||||
console.error("unable to release migration lock")
|
||||
}
|
||||
}
|
||||
)
|
||||
}
|
||||
|
|
|
@ -1 +1,2 @@
|
|||
export * from "./user"
|
||||
export * from "./license"
|
||||
|
|
|
@ -0,0 +1,11 @@
|
|||
import { QuotaUsage } from "../../documents"
|
||||
|
||||
export interface GetLicenseRequest {
|
||||
quotaUsage: QuotaUsage
|
||||
}
|
||||
|
||||
export interface QuotaTriggeredRequest {
|
||||
percentage: number
|
||||
name: string
|
||||
resetDate?: string
|
||||
}
|
|
@ -1,4 +1,12 @@
|
|||
import { Feature, Hosting, PlanType, Quotas } from "../../sdk"
|
||||
import {
|
||||
Feature,
|
||||
Hosting,
|
||||
MonthlyQuotaName,
|
||||
PlanType,
|
||||
Quotas,
|
||||
StaticQuotaName,
|
||||
} from "../../sdk"
|
||||
import { MonthlyUsage, QuotaUsage, StaticUsage } from "../global"
|
||||
|
||||
export interface CreateAccount {
|
||||
email: string
|
||||
|
@ -42,6 +50,7 @@ export interface Account extends CreateAccount {
|
|||
licenseKey?: string
|
||||
licenseKeyActivatedAt?: number
|
||||
licenseOverrides?: LicenseOverrides
|
||||
quotaUsage?: QuotaUsage
|
||||
}
|
||||
|
||||
export interface PasswordAccount extends Account {
|
||||
|
|
|
@ -24,19 +24,34 @@ export interface UsageBreakdown {
|
|||
}
|
||||
}
|
||||
|
||||
export type MonthlyUsage = {
|
||||
export type QuotaTriggers = {
|
||||
[key: string]: string | undefined
|
||||
}
|
||||
|
||||
export interface StaticUsage {
|
||||
[StaticQuotaName.APPS]: number
|
||||
[StaticQuotaName.PLUGINS]: number
|
||||
[StaticQuotaName.USER_GROUPS]: number
|
||||
[StaticQuotaName.ROWS]: number
|
||||
triggers: {
|
||||
[key in StaticQuotaName]?: QuotaTriggers
|
||||
}
|
||||
}
|
||||
|
||||
export interface MonthlyUsage {
|
||||
[MonthlyQuotaName.QUERIES]: number
|
||||
[MonthlyQuotaName.AUTOMATIONS]: number
|
||||
[MonthlyQuotaName.DAY_PASSES]: number
|
||||
triggers: {
|
||||
[key in MonthlyQuotaName]?: QuotaTriggers
|
||||
}
|
||||
breakdown?: {
|
||||
[key in BreakdownQuotaName]?: UsageBreakdown
|
||||
}
|
||||
}
|
||||
|
||||
export interface BaseQuotaUsage {
|
||||
usageQuota: {
|
||||
[key in StaticQuotaName]: number
|
||||
}
|
||||
usageQuota: StaticUsage
|
||||
monthly: {
|
||||
[key: string]: MonthlyUsage
|
||||
}
|
||||
|
@ -51,6 +66,13 @@ export interface QuotaUsage extends BaseQuotaUsage {
|
|||
}
|
||||
}
|
||||
|
||||
export type SetUsageValues = {
|
||||
total: number
|
||||
app?: number
|
||||
breakdown?: number
|
||||
triggers?: QuotaTriggers
|
||||
}
|
||||
|
||||
export type UsageValues = {
|
||||
total: number
|
||||
app?: number
|
||||
|
|
|
@ -7,3 +7,4 @@ export * from "./datasources"
|
|||
export * from "./search"
|
||||
export * from "./koa"
|
||||
export * from "./auth"
|
||||
export * from "./locks"
|
||||
|
|
|
@ -61,26 +61,40 @@ export type PlanQuotas = {
|
|||
[PlanType.ENTERPRISE]: Quotas
|
||||
}
|
||||
|
||||
export type MonthlyQuotas = {
|
||||
[MonthlyQuotaName.QUERIES]: Quota
|
||||
[MonthlyQuotaName.AUTOMATIONS]: Quota
|
||||
[MonthlyQuotaName.DAY_PASSES]: Quota
|
||||
}
|
||||
|
||||
export type StaticQuotas = {
|
||||
[StaticQuotaName.ROWS]: Quota
|
||||
[StaticQuotaName.APPS]: Quota
|
||||
[StaticQuotaName.USER_GROUPS]: Quota
|
||||
[StaticQuotaName.PLUGINS]: Quota
|
||||
}
|
||||
|
||||
export type ConstantQuotas = {
|
||||
[ConstantQuotaName.AUTOMATION_LOG_RETENTION_DAYS]: Quota
|
||||
}
|
||||
|
||||
export type Quotas = {
|
||||
[QuotaType.USAGE]: {
|
||||
[QuotaUsageType.MONTHLY]: {
|
||||
[MonthlyQuotaName.QUERIES]: Quota
|
||||
[MonthlyQuotaName.AUTOMATIONS]: Quota
|
||||
[MonthlyQuotaName.DAY_PASSES]: Quota
|
||||
}
|
||||
[QuotaUsageType.STATIC]: {
|
||||
[StaticQuotaName.ROWS]: Quota
|
||||
[StaticQuotaName.APPS]: Quota
|
||||
[StaticQuotaName.USER_GROUPS]: Quota
|
||||
[StaticQuotaName.PLUGINS]: Quota
|
||||
}
|
||||
}
|
||||
[QuotaType.CONSTANT]: {
|
||||
[ConstantQuotaName.AUTOMATION_LOG_RETENTION_DAYS]: Quota
|
||||
[QuotaUsageType.MONTHLY]: MonthlyQuotas
|
||||
[QuotaUsageType.STATIC]: StaticQuotas
|
||||
}
|
||||
[QuotaType.CONSTANT]: ConstantQuotas
|
||||
}
|
||||
|
||||
export interface Quota {
|
||||
name: string
|
||||
value: number
|
||||
/**
|
||||
* Array of whole numbers (1-100) that dictate the percentage that this quota should trigger
|
||||
* at in relation to the corresponding usage inside budibase.
|
||||
*
|
||||
* Triggering results in a budibase installation sending a request to account-portal,
|
||||
* which can have subsequent effects such as sending emails to users.
|
||||
*/
|
||||
triggers: number[]
|
||||
}
|
||||
|
|
|
@ -0,0 +1,31 @@
|
|||
export enum LockType {
|
||||
/**
|
||||
* If this lock is already held the attempted operation will not be performed.
|
||||
* No retries will take place and no error will be thrown.
|
||||
*/
|
||||
TRY_ONCE = "try_once",
|
||||
}
|
||||
|
||||
export enum LockName {
|
||||
MIGRATIONS = "migrations",
|
||||
TRIGGER_QUOTA = "trigger_quota",
|
||||
}
|
||||
|
||||
export interface LockOptions {
|
||||
/**
|
||||
* The lock type determines which client to use
|
||||
*/
|
||||
type: LockType
|
||||
/**
|
||||
* The name for the lock
|
||||
*/
|
||||
name: LockName
|
||||
/**
|
||||
* The ttl to auto-expire the lock if not unlocked manually
|
||||
*/
|
||||
ttl: number
|
||||
/**
|
||||
* The suffix to add to the lock name for additional uniqueness
|
||||
*/
|
||||
nameSuffix?: string
|
||||
}
|
|
@ -1,58 +0,0 @@
|
|||
const { StaticDatabases, doWithDB } = require("@budibase/backend-core/db")
|
||||
const { getTenantId } = require("@budibase/backend-core/tenancy")
|
||||
const { deleteTenant } = require("@budibase/backend-core/deprovision")
|
||||
const { quotas } = require("@budibase/pro")
|
||||
|
||||
exports.exists = async ctx => {
|
||||
const tenantId = ctx.request.params
|
||||
ctx.body = {
|
||||
exists: await doWithDB(StaticDatabases.PLATFORM_INFO.name, async db => {
|
||||
let exists = false
|
||||
try {
|
||||
const tenantsDoc = await db.get(
|
||||
StaticDatabases.PLATFORM_INFO.docs.tenants
|
||||
)
|
||||
if (tenantsDoc) {
|
||||
exists = tenantsDoc.tenantIds.indexOf(tenantId) !== -1
|
||||
}
|
||||
} catch (err) {
|
||||
// if error it doesn't exist
|
||||
}
|
||||
return exists
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
||||
exports.fetch = async ctx => {
|
||||
ctx.body = await doWithDB(StaticDatabases.PLATFORM_INFO.name, async db => {
|
||||
let tenants = []
|
||||
try {
|
||||
const tenantsDoc = await db.get(
|
||||
StaticDatabases.PLATFORM_INFO.docs.tenants
|
||||
)
|
||||
if (tenantsDoc) {
|
||||
tenants = tenantsDoc.tenantIds
|
||||
}
|
||||
} catch (err) {
|
||||
// if error it doesn't exist
|
||||
}
|
||||
return tenants
|
||||
})
|
||||
}
|
||||
|
||||
exports.delete = async ctx => {
|
||||
const tenantId = getTenantId()
|
||||
|
||||
if (ctx.params.tenantId !== tenantId) {
|
||||
ctx.throw(403, "Unauthorized")
|
||||
}
|
||||
|
||||
try {
|
||||
await deleteTenant(tenantId)
|
||||
await quotas.bustCache()
|
||||
ctx.status = 204
|
||||
} catch (err) {
|
||||
ctx.log.error(err)
|
||||
throw err
|
||||
}
|
||||
}
|
|
@ -0,0 +1,66 @@
|
|||
const { StaticDatabases, doWithDB } = require("@budibase/backend-core/db")
|
||||
const { getTenantId } = require("@budibase/backend-core/tenancy")
|
||||
const { deleteTenant } = require("@budibase/backend-core/deprovision")
|
||||
import { quotas } from "@budibase/pro"
|
||||
|
||||
export const exists = async (ctx: any) => {
|
||||
const tenantId = ctx.request.params
|
||||
ctx.body = {
|
||||
exists: await doWithDB(
|
||||
StaticDatabases.PLATFORM_INFO.name,
|
||||
async (db: any) => {
|
||||
let exists = false
|
||||
try {
|
||||
const tenantsDoc = await db.get(
|
||||
StaticDatabases.PLATFORM_INFO.docs.tenants
|
||||
)
|
||||
if (tenantsDoc) {
|
||||
exists = tenantsDoc.tenantIds.indexOf(tenantId) !== -1
|
||||
}
|
||||
} catch (err) {
|
||||
// if error it doesn't exist
|
||||
}
|
||||
return exists
|
||||
}
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
export const fetch = async (ctx: any) => {
|
||||
ctx.body = await doWithDB(
|
||||
StaticDatabases.PLATFORM_INFO.name,
|
||||
async (db: any) => {
|
||||
let tenants = []
|
||||
try {
|
||||
const tenantsDoc = await db.get(
|
||||
StaticDatabases.PLATFORM_INFO.docs.tenants
|
||||
)
|
||||
if (tenantsDoc) {
|
||||
tenants = tenantsDoc.tenantIds
|
||||
}
|
||||
} catch (err) {
|
||||
// if error it doesn't exist
|
||||
}
|
||||
return tenants
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
const _delete = async (ctx: any) => {
|
||||
const tenantId = getTenantId()
|
||||
|
||||
if (ctx.params.tenantId !== tenantId) {
|
||||
ctx.throw(403, "Unauthorized")
|
||||
}
|
||||
|
||||
try {
|
||||
await deleteTenant(tenantId)
|
||||
await quotas.bustCache()
|
||||
ctx.status = 204
|
||||
} catch (err) {
|
||||
ctx.log.error(err)
|
||||
throw err
|
||||
}
|
||||
}
|
||||
|
||||
export { _delete as delete }
|
|
@ -1,5 +1,11 @@
|
|||
import { migrations, redis } from "@budibase/backend-core"
|
||||
import { Migration, MigrationOptions, MigrationName } from "@budibase/types"
|
||||
import { migrations, locks } from "@budibase/backend-core"
|
||||
import {
|
||||
Migration,
|
||||
MigrationOptions,
|
||||
MigrationName,
|
||||
LockType,
|
||||
LockName,
|
||||
} from "@budibase/types"
|
||||
import env from "../environment"
|
||||
|
||||
// migration functions
|
||||
|
@ -42,33 +48,14 @@ export const migrate = async (options?: MigrationOptions) => {
|
|||
}
|
||||
|
||||
const migrateWithLock = async (options?: MigrationOptions) => {
|
||||
// get a new lock client
|
||||
const redlock = await redis.clients.getMigrationsRedlock()
|
||||
// lock for 15 minutes
|
||||
const ttl = 1000 * 60 * 15
|
||||
|
||||
let migrationLock
|
||||
|
||||
// acquire lock
|
||||
try {
|
||||
migrationLock = await redlock.lock("migrations", ttl)
|
||||
} catch (e: any) {
|
||||
if (e.name === "LockError") {
|
||||
return
|
||||
} else {
|
||||
throw e
|
||||
await locks.doWithLock(
|
||||
{
|
||||
type: LockType.TRY_ONCE,
|
||||
name: LockName.MIGRATIONS,
|
||||
ttl: 1000 * 60 * 15, // auto expire the migration lock after 15 minutes
|
||||
},
|
||||
async () => {
|
||||
await migrations.runMigrations(MIGRATIONS, options)
|
||||
}
|
||||
}
|
||||
|
||||
// run migrations
|
||||
try {
|
||||
await migrations.runMigrations(MIGRATIONS, options)
|
||||
} finally {
|
||||
// release lock
|
||||
try {
|
||||
await migrationLock.unlock()
|
||||
} catch (e) {
|
||||
console.error("unable to release migration lock")
|
||||
}
|
||||
}
|
||||
)
|
||||
}
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
import env from "../../environment"
|
||||
import { events, accounts, tenancy } from "@budibase/backend-core"
|
||||
import { User, UserRoles, CloudAccount } from "@budibase/types"
|
||||
import { users as pro } from "@budibase/pro"
|
||||
|
||||
export const handleDeleteEvents = async (user: any) => {
|
||||
await events.user.deleted(user)
|
||||
|
|
Loading…
Reference in New Issue