Skip to content
Snippets Groups Projects
Commit c4fcec5f authored by richard.petersen's avatar richard.petersen :sailboat:
Browse files

Add trace logging for the cache

parent 43b4dd6a
No related branches found
No related tags found
No related merge requests found
import { logger } from './logger.js'
import * as redis from './redis.js'
const cache = {}
export async function setAsync (key, asyncValue) {
logger.trace(`[Cache] Set async ${key}`)
cache[key] = asyncValue
const value = await asyncValue
await set(key, value)
......@@ -10,6 +12,7 @@ export async function setAsync (key, asyncValue) {
}
export function set (key, value) {
logger.trace(`[Cache] Set ${key}`)
if (cache[key] === value) return
cache[key] = value
if (redis.isEnabled()) {
......@@ -28,11 +31,17 @@ export async function clear () {
}
export async function get (key, { method = 'get' } = {}) {
if (cache[key]) return cache[key]
if (cache[key]) {
logger.trace(`[Cache] Resolve "${key}" from memory`)
return cache[key]
}
if (redis.isEnabled()) {
const result = await redis.client[method]?.(key)
cache[key] = result
if (result) {
logger.trace(`[Cache] Resolve "${key}" from redis`)
cache[key] = result
}
return result
}
}
import fs from 'fs/promises'
import yaml from 'js-yaml'
import { logger } from './logger.js'
class Config {
async load () {
const urlsSource = await fs.readFile('./config/config.yaml', 'utf8')
this._urls = yaml.load(urlsSource).baseUrls
logger.trace('[Config] Config has been loaded')
}
get urls () {
......
......@@ -43,7 +43,7 @@ export async function fetchFileWithHeaders ({ path, version }) {
try {
return fetchFileWithHeadersFromBaseUrl(path, module.meta.baseUrl, version)
} catch (err) {
logger.debug(`File ${path} had a baseUrl but could not be found on that server: ${err}`)
logger.debug(`[Files] File ${path} had a baseUrl but could not be found on that server: ${err}`)
}
}
......
......@@ -14,7 +14,7 @@ createQueues()
// Binds and listens for connections on the specified host and port
root.listen(process.env.PORT, () => {
logger.info(`ui-middleware listening on port ${process.env.PORT}`)
logger.info(`[Server] ui-middleware listening on port ${process.env.PORT}`)
})
process.on('uncaughtException', err => {
......
......@@ -14,7 +14,7 @@ export async function fetchViteManifests () {
try {
const manifest = await result.json()
for (const file in manifest) {
logger.debug(`retrieved ${file} from ${baseUrl}`)
logger.debug(`[Manifest] Retrieved ${file} from ${baseUrl}`)
manifest[file].meta = manifest[file].meta || {}
manifest[file].meta.baseUrl = baseUrl
}
......
......@@ -20,8 +20,8 @@ const createClient = (type, options = {}) => {
password: process.env.REDIS_PASSWORD,
...options
})
client.on('ready', () => logger.info(`Connected ${type} to redis on ${process.env.REDIS_HOST}`))
client.on('error', (err) => logger.error(`Redis connect error: ${err}`))
client.on('ready', () => logger.info(`[Redis] Connected ${type} to redis on ${process.env.REDIS_HOST}`))
client.on('error', (err) => logger.error(`[Redis] Connect error: ${err}`))
return client
}
......
......@@ -16,7 +16,7 @@ export const fetchLatestVersion = async () => {
if (!version) throw new Error()
return version
} catch (err) {
logger.debug(`UI container at ${baseUrl} does not have meta.json. Fall back to version hash based on manifest.`)
logger.debug(`[Version] UI container at ${baseUrl} does not have meta.json. Fall back to version hash based on manifest.`)
}
try {
const response = await fetch(new URL('manifest.json', baseUrl))
......@@ -24,7 +24,7 @@ export const fetchLatestVersion = async () => {
const manifest = await response.json()
return hash(manifest)
} catch (err) {
logger.error(`Cannot fetch manifest from ${baseUrl}. Version info will not be correct.`)
logger.error(`[Version] Cannot fetch manifest from ${baseUrl}. Version info will not be correct.`)
}
}))
return hash(infos)
......@@ -51,7 +51,7 @@ export async function getLatestVersion () {
export function registerLatestVersionListener (client) {
if (redis.isEnabled()) {
const key = getRedisKey({ name: 'updateLatestVersion' })
client.subscribe(key, (errs, count) => logger.info(`Subscribed to ${key}.`))
client.subscribe(key, (errs, count) => logger.info(`[Redis] Subscribed to ${key}.`))
client.on('message', async (channel, message) => {
if (channel !== key) return
await config.load()
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment