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

add: ui-middleware will slowly warm it's caches after version updates

parent 28a9f92d
No related branches found
No related tags found
No related merge requests found
......@@ -84,3 +84,21 @@ export function getFile ({ version, path }) {
return fetchFileWithHeaders({ version, path })
})
}
export async function warmCache ({ version }) {
const start = +new Date()
logger.info('[File] start warming up the cache')
const viteManifests = await getViteManifests({ version })
for (const key of Object.keys(viteManifests)) {
const path = `/${viteManifests[key].file}`
if (!path) continue
try {
await getFile({ version, path })
} catch (err) {
logger.info(`[File] could not prefetch file ${path}`)
}
}
logger.info(`[File] finished warming up the cache in ${Math.floor((+new Date() - start) / 1000)}s`)
}
......@@ -8,6 +8,7 @@ import { configMap } from './configMap.js'
import * as redis from './redis.js'
import lightshipCjs from 'lightship'
import { createMetricsServer } from './metrics.js'
import { warmCache } from './files.js'
config()
......@@ -26,6 +27,13 @@ app.addHook('onReady', () => {
lightship.signalReady()
})
app.addHook('onReady', () => {
// don't block the onReady hook
getLatestVersion()
.then(version => warmCache({ version }))
.catch(err => logger.error(err))
})
// Binds and listens for connections on the specified host and port
app.listen({ host: '::', port: Number(process.env.PORT) })
......
......@@ -5,6 +5,7 @@ import * as cache from './cache.js'
import * as redis from './redis.js'
import { Gauge } from 'prom-client'
import { getViteManifests } from './manifests.js'
import { warmCache } from './files.js'
let latestVersion
......@@ -80,6 +81,7 @@ export function registerLatestVersionListener (client) {
await configMap.load()
versionUpdateGauge.setToCurrentTime({ version })
cache.clear()
warmCache({ version }).catch(err => logger.error(err))
latestVersion = version
})
}
......@@ -121,6 +123,7 @@ export async function updateVersionProcessor () {
// if redis is disabled, this will only be trigger by a setInterval and not from a redis event
logger.info('[Version] Clear local cache due to version update.')
cache.clear()
warmCache({ version: fetchedVersion }).catch(err => logger.error(err))
latestVersion = fetchedVersion
}
return latestVersion
......
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