1
0
Fork 0
mirror of synced 2024-09-25 13:51:40 +12:00
budibase/packages/server/src/utilities/redis.ts

106 lines
3.1 KiB
TypeScript
Raw Normal View History

import { redis } from "@budibase/backend-core"
import { getGlobalIDFromUserMetadataID } from "../db/utils"
import { ContextUser } from "@budibase/types"
2021-05-13 04:37:09 +12:00
const APP_DEV_LOCK_SECONDS = 600
const AUTOMATION_TEST_FLAG_SECONDS = 60
let devAppClient: any, debounceClient: any, flagClient: any, socketClient: any
2021-05-13 04:37:09 +12:00
// We need to maintain a duplicate client for socket.io pub/sub
let socketSubClient: any
// We init this as we want to keep the connection open all the time
2021-05-13 04:37:09 +12:00
// reduces the performance hit
export async function init() {
devAppClient = new redis.Client(redis.utils.Databases.DEV_LOCKS)
debounceClient = new redis.Client(redis.utils.Databases.DEBOUNCE)
flagClient = new redis.Client(redis.utils.Databases.FLAGS)
socketClient = new redis.Client(redis.utils.Databases.SOCKET_IO)
await devAppClient.init()
await debounceClient.init()
await flagClient.init()
await socketClient.init()
// Duplicate the socket client for pub/sub
socketSubClient = socketClient.getClient().duplicate()
2021-05-13 04:37:09 +12:00
}
export async function shutdown() {
2021-05-28 21:09:32 +12:00
if (devAppClient) await devAppClient.finish()
if (debounceClient) await debounceClient.finish()
if (flagClient) await flagClient.finish()
if (socketClient) await socketClient.finish()
if (socketSubClient) socketSubClient.disconnect()
// shutdown core clients
await redis.clients.shutdown()
2022-08-31 22:05:49 +12:00
console.log("Redis shutdown")
}
export async function doesUserHaveLock(devAppId: string, user: ContextUser) {
2021-05-13 04:37:09 +12:00
const value = await devAppClient.get(devAppId)
if (!value) {
return true
}
// make sure both IDs are global
const expected = getGlobalIDFromUserMetadataID(value._id)
const userId = getGlobalIDFromUserMetadataID(user._id!)
return expected === userId
2021-05-13 04:37:09 +12:00
}
export async function getLocksById(appIds: string[]) {
return await devAppClient.bulkGet(appIds)
}
export async function updateLock(devAppId: string, user: ContextUser) {
// make sure always global user ID
const globalId = getGlobalIDFromUserMetadataID(user._id!)
const inputUser = {
...user,
userId: globalId,
_id: globalId,
2022-05-05 23:52:17 +12:00
lockedAt: new Date().getTime(),
}
2022-05-05 23:52:17 +12:00
await devAppClient.store(devAppId, inputUser, APP_DEV_LOCK_SECONDS)
2021-05-13 04:37:09 +12:00
}
export async function clearLock(devAppId: string, user: ContextUser) {
2021-05-13 04:37:09 +12:00
const value = await devAppClient.get(devAppId)
if (!value) {
return
}
const userId = getGlobalIDFromUserMetadataID(user._id!)
2021-05-14 01:24:55 +12:00
if (value._id !== userId) {
2021-05-13 04:37:09 +12:00
throw "User does not hold lock, cannot clear it."
}
await devAppClient.delete(devAppId)
2021-05-13 04:37:09 +12:00
}
export async function checkDebounce(id: string) {
return debounceClient.get(id)
}
export async function setDebounce(id: string, seconds: number) {
await debounceClient.store(id, "debouncing", seconds)
}
export async function setTestFlag(id: string) {
await flagClient.store(id, { testing: true }, AUTOMATION_TEST_FLAG_SECONDS)
}
export async function checkTestFlag(id: string) {
const flag = await flagClient.get(id)
return !!(flag && flag.testing)
}
export async function clearTestFlag(id: string) {
await devAppClient.delete(id)
}
export function getSocketPubSubClients() {
return {
pub: socketClient.getClient(),
sub: socketSubClient,
}
}