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
Loading
import { logger } from './logger.js'
import * as redis from './redis.js' import * as redis from './redis.js'
const cache = {} const cache = {}
export async function setAsync (key, asyncValue) { export async function setAsync (key, asyncValue) {
logger.trace(`[Cache] Set async ${key}`)
cache[key] = asyncValue cache[key] = asyncValue
const value = await asyncValue const value = await asyncValue
await set(key, value) await set(key, value)
...@@ -10,6 +12,7 @@ export async function setAsync (key, asyncValue) { ...@@ -10,6 +12,7 @@ export async function setAsync (key, asyncValue) {
} }
export function set (key, value) { export function set (key, value) {
logger.trace(`[Cache] Set ${key}`)
if (cache[key] === value) return if (cache[key] === value) return
cache[key] = value cache[key] = value
if (redis.isEnabled()) { if (redis.isEnabled()) {
...@@ -28,11 +31,17 @@ export async function clear () { ...@@ -28,11 +31,17 @@ export async function clear () {
} }
export async function get (key, { method = 'get' } = {}) { 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()) { if (redis.isEnabled()) {
const result = await redis.client[method]?.(key) const result = await redis.client[method]?.(key)
cache[key] = result if (result) {
logger.trace(`[Cache] Resolve "${key}" from redis`)
cache[key] = result
}
return result return result
} }
} }
import fs from 'fs/promises' import fs from 'fs/promises'
import yaml from 'js-yaml' import yaml from 'js-yaml'
import { logger } from './logger.js'
class Config { class Config {
async load () { async load () {
const urlsSource = await fs.readFile('./config/config.yaml', 'utf8') const urlsSource = await fs.readFile('./config/config.yaml', 'utf8')
this._urls = yaml.load(urlsSource).baseUrls this._urls = yaml.load(urlsSource).baseUrls
logger.trace('[Config] Config has been loaded')
} }
get urls () { get urls () {
......
...@@ -43,7 +43,7 @@ export async function fetchFileWithHeaders ({ path, version }) { ...@@ -43,7 +43,7 @@ export async function fetchFileWithHeaders ({ path, version }) {
try { try {
return fetchFileWithHeadersFromBaseUrl(path, module.meta.baseUrl, version) return fetchFileWithHeadersFromBaseUrl(path, module.meta.baseUrl, version)
} catch (err) { } 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() ...@@ -14,7 +14,7 @@ createQueues()
// Binds and listens for connections on the specified host and port // Binds and listens for connections on the specified host and port
root.listen(process.env.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 => { process.on('uncaughtException', err => {
......
...@@ -14,7 +14,7 @@ export async function fetchViteManifests () { ...@@ -14,7 +14,7 @@ export async function fetchViteManifests () {
try { try {
const manifest = await result.json() const manifest = await result.json()
for (const file in manifest) { 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 = manifest[file].meta || {}
manifest[file].meta.baseUrl = baseUrl manifest[file].meta.baseUrl = baseUrl
} }
......
...@@ -20,8 +20,8 @@ const createClient = (type, options = {}) => { ...@@ -20,8 +20,8 @@ const createClient = (type, options = {}) => {
password: process.env.REDIS_PASSWORD, password: process.env.REDIS_PASSWORD,
...options ...options
}) })
client.on('ready', () => logger.info(`Connected ${type} to redis on ${process.env.REDIS_HOST}`)) 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}`)) client.on('error', (err) => logger.error(`[Redis] Connect error: ${err}`))
return client return client
} }
......
...@@ -16,7 +16,7 @@ export const fetchLatestVersion = async () => { ...@@ -16,7 +16,7 @@ export const fetchLatestVersion = async () => {
if (!version) throw new Error() if (!version) throw new Error()
return version return version
} catch (err) { } 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 { try {
const response = await fetch(new URL('manifest.json', baseUrl)) const response = await fetch(new URL('manifest.json', baseUrl))
...@@ -24,7 +24,7 @@ export const fetchLatestVersion = async () => { ...@@ -24,7 +24,7 @@ export const fetchLatestVersion = async () => {
const manifest = await response.json() const manifest = await response.json()
return hash(manifest) return hash(manifest)
} catch (err) { } 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) return hash(infos)
...@@ -51,7 +51,7 @@ export async function getLatestVersion () { ...@@ -51,7 +51,7 @@ export async function getLatestVersion () {
export function registerLatestVersionListener (client) { export function registerLatestVersionListener (client) {
if (redis.isEnabled()) { if (redis.isEnabled()) {
const key = getRedisKey({ name: 'updateLatestVersion' }) 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) => { client.on('message', async (channel, message) => {
if (channel !== key) return if (channel !== key) return
await config.load() 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