Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(server/core): generalize caching mechanism, and update getServerInfoFactory to rely on getServerConfig dependency #3791

Draft
wants to merge 18 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions packages/server/healthchecks/postgres.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Knex } from 'knex'
import { getServerInfoFactory } from '@/modules/core/repositories/server'
import { getServerConfigFactory } from '@/modules/core/repositories/server'
import { getAllRegisteredDbClients } from '@/modules/multiregion/utils/dbSelector'
import type { CheckResponse, MultiDBCheck } from '@/healthchecks/types'
import { ensureErrorOrWrapAsCause } from '@/modules/shared/errors/ensureError'
Expand All @@ -9,10 +9,10 @@ type DBCheck = (params: { db: Knex }) => Promise<CheckResponse>

export const isPostgresAlive: DBCheck = async (params) => {
const { db } = params
const getServerInfo = getServerInfoFactory({ db })
const getServerConfig = getServerConfigFactory({ db })

try {
await getServerInfo()
await getServerConfig({ bustCache: true }) // we always want this to hit the database, so it should not be cached
} catch (err) {
return { isAlive: false, err: ensureError(err, 'Unknown Postgres error.') }
}
Expand Down
37 changes: 35 additions & 2 deletions packages/server/modules/core/repositories/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
getServerOrigin,
getServerVersion
} from '@/modules/shared/helpers/envHelper'
import Redis from 'ioredis'
import { Knex } from 'knex'

const ServerConfig = buildTableHelper('server_config', [
Expand All @@ -38,15 +39,47 @@ const tables = {
scopes: (db: Knex) => db<ScopeRecord>(Scopes.name)
}

const SERVER_CONFIG_CACHE_KEY = 'server_config'

export type GetServerConfig = (params: {
bustCache?: boolean
}) => Promise<ServerConfigRecord>

export const getServerConfigFactory =
iainsproat marked this conversation as resolved.
Show resolved Hide resolved
(deps: { db: Knex; cache?: Redis }): GetServerConfig =>
async (params) => {
const { cache, db } = deps
const { bustCache } = params
if (cache && !bustCache) {
const cachedResult = await cache.get(SERVER_CONFIG_CACHE_KEY)
if (cachedResult) return JSON.parse(cachedResult) as ServerConfigRecord
}
if (cache && bustCache) {
await cache.del(SERVER_CONFIG_CACHE_KEY)
}
const result = await tables.serverConfig(db).select('*').first()
if (cache) {
await cache.setex(
SERVER_CONFIG_CACHE_KEY,
60 /* seconds */,
JSON.stringify(result)
)
}
// An entry should always exist as it is placed there by database migrations
return result!
}

export const getServerInfoFactory =
(deps: { db: Knex }): GetServerInfo =>
(deps: { getServerConfig: GetServerConfig }): GetServerInfo =>
async () => {
const movedTo = getServerMovedTo()
const movedFrom = getServerMovedFrom()

const serverConfig = await deps.getServerConfig({ bustCache: false })

// An entry should always exist from migrations
const serverInfo: ServerInfo = {
...(await tables.serverConfig(deps.db).select('*').first())!,
...serverConfig,
version: getServerVersion(),
canonicalUrl: getServerOrigin(),
configuration: {
Expand Down