2022-07-25 21:45:29 +02:00
|
|
|
import { QueryVariable } from "./definitions"
|
2022-11-22 20:49:59 +01:00
|
|
|
import env from "../environment"
|
2022-11-26 16:10:41 +01:00
|
|
|
import * as db from "../db"
|
2022-11-22 20:49:59 +01:00
|
|
|
import { redis, db as dbCore } from "@budibase/backend-core"
|
2021-12-16 17:58:15 +01:00
|
|
|
|
2021-12-16 23:43:14 +01:00
|
|
|
const VARIABLE_TTL_SECONDS = 3600
|
2022-07-25 21:45:29 +02:00
|
|
|
let client: any
|
2021-12-16 17:58:15 +01:00
|
|
|
|
2021-12-16 23:43:14 +01:00
|
|
|
async function getClient() {
|
2021-12-16 20:33:47 +01:00
|
|
|
if (!client) {
|
|
|
|
client = await new redis.Client(redis.utils.Databases.QUERY_VARS).init()
|
|
|
|
}
|
2021-12-16 23:43:14 +01:00
|
|
|
return client
|
2021-12-16 17:58:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
process.on("exit", async () => {
|
2022-07-25 21:45:29 +02:00
|
|
|
if (client) {
|
|
|
|
await client.finish()
|
|
|
|
}
|
2021-12-16 17:58:15 +01:00
|
|
|
})
|
2021-12-15 12:25:52 +01:00
|
|
|
|
2022-07-25 21:45:29 +02:00
|
|
|
function makeVariableKey(queryId: string, variable: string) {
|
2022-11-22 20:49:59 +01:00
|
|
|
return `${queryId}${dbCore.SEPARATOR}${variable}`
|
2022-07-25 21:45:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export function threadSetup() {
|
2021-12-15 13:26:29 +01:00
|
|
|
// don't run this if not threading
|
|
|
|
if (env.isTest() || env.DISABLE_THREADING) {
|
|
|
|
return
|
|
|
|
}
|
2021-12-15 12:25:52 +01:00
|
|
|
// when thread starts, make sure it is recorded
|
|
|
|
env.setInThread()
|
2022-03-29 17:03:44 +02:00
|
|
|
db.init()
|
2021-12-15 12:25:52 +01:00
|
|
|
}
|
2021-12-16 17:58:15 +01:00
|
|
|
|
2022-07-25 21:45:29 +02:00
|
|
|
export async function checkCacheForDynamicVariable(
|
|
|
|
queryId: string,
|
|
|
|
variable: string
|
|
|
|
) {
|
2021-12-16 23:43:14 +01:00
|
|
|
const cache = await getClient()
|
|
|
|
return cache.get(makeVariableKey(queryId, variable))
|
|
|
|
}
|
|
|
|
|
2022-07-25 21:45:29 +02:00
|
|
|
export async function invalidateDynamicVariables(cachedVars: QueryVariable[]) {
|
2022-01-05 17:54:59 +01:00
|
|
|
const cache = await getClient()
|
2021-12-17 18:56:28 +01:00
|
|
|
let promises = []
|
|
|
|
for (let variable of cachedVars) {
|
|
|
|
promises.push(
|
2022-01-05 17:54:59 +01:00
|
|
|
cache.delete(makeVariableKey(variable.queryId, variable.name))
|
2021-12-17 18:56:28 +01:00
|
|
|
)
|
|
|
|
}
|
|
|
|
await Promise.all(promises)
|
|
|
|
}
|
|
|
|
|
2022-07-25 21:45:29 +02:00
|
|
|
export async function storeDynamicVariable(
|
|
|
|
queryId: string,
|
|
|
|
variable: string,
|
|
|
|
value: any
|
|
|
|
) {
|
2021-12-16 23:43:14 +01:00
|
|
|
const cache = await getClient()
|
|
|
|
await cache.store(
|
|
|
|
makeVariableKey(queryId, variable),
|
|
|
|
value,
|
|
|
|
VARIABLE_TTL_SECONDS
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-07-25 21:45:29 +02:00
|
|
|
export function formatResponse(resp: any) {
|
2021-12-16 23:43:14 +01:00
|
|
|
if (typeof resp === "string") {
|
|
|
|
try {
|
|
|
|
resp = JSON.parse(resp)
|
|
|
|
} catch (err) {
|
|
|
|
resp = { response: resp }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return resp
|
|
|
|
}
|
|
|
|
|
2022-07-25 21:45:29 +02:00
|
|
|
export function hasExtraData(response: any) {
|
2021-12-16 23:43:14 +01:00
|
|
|
return (
|
|
|
|
typeof response === "object" &&
|
|
|
|
!Array.isArray(response) &&
|
2022-03-31 16:44:06 +02:00
|
|
|
response &&
|
2021-12-16 23:43:14 +01:00
|
|
|
response.data != null &&
|
|
|
|
response.info != null
|
|
|
|
)
|
|
|
|
}
|
2022-07-25 21:45:29 +02:00
|
|
|
|
|
|
|
export default {
|
|
|
|
hasExtraData,
|
|
|
|
formatResponse,
|
|
|
|
storeDynamicVariable,
|
|
|
|
invalidateDynamicVariables,
|
|
|
|
checkCacheForDynamicVariable,
|
|
|
|
threadSetup,
|
|
|
|
}
|