From 3e0419d90d3d441994c7aa76b037ecda00e3ceb5 Mon Sep 17 00:00:00 2001 From: Richard Petersen <richard.petersen@open-xchange.com> Date: Fri, 25 Nov 2022 12:57:55 +0100 Subject: [PATCH] add: ui-middleware will slowly warm it's caches after version updates --- src/files.js | 18 ++++++++++++++++++ src/index.js | 8 ++++++++ src/version.js | 3 +++ 3 files changed, 29 insertions(+) diff --git a/src/files.js b/src/files.js index c9f27e0..2897905 100644 --- a/src/files.js +++ b/src/files.js @@ -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`) +} diff --git a/src/index.js b/src/index.js index a1419b0..6309e12 100644 --- a/src/index.js +++ b/src/index.js @@ -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) }) diff --git a/src/version.js b/src/version.js index e87b7da..4103c7d 100644 --- a/src/version.js +++ b/src/version.js @@ -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 -- GitLab