From 998243ddafa5d48a915e800798b87634d2722527 Mon Sep 17 00:00:00 2001 From: Hugo B Tiburtino Date: Wed, 14 Oct 2020 00:30:35 +0200 Subject: [PATCH] Read the cache keys to be updated from a ts file TS file is easier to manipulate than a json one: it can be simply imported, no need of reading from a file and parsing; and it accepts comments. --- __tests__/cache-worker.ts | 25 +++++++++++++++---------- cache-worker/Dockerfile | 2 +- cache-worker/cache-keys.ts | 5 +++++ cache-worker/index.ts | 12 ++++++------ 4 files changed, 27 insertions(+), 17 deletions(-) create mode 100644 cache-worker/cache-keys.ts diff --git a/__tests__/cache-worker.ts b/__tests__/cache-worker.ts index 98b1562dd..f5555db91 100644 --- a/__tests__/cache-worker.ts +++ b/__tests__/cache-worker.ts @@ -74,9 +74,8 @@ beforeEach(async () => { describe('Update-cache worker', () => { test('successfully calls _cacheKeys and _updateCache', async () => { - await worker.updateCache('all') + await worker.updateCache([...mockKeysValues.keys()]) expect(worker.errLog).toEqual([]) - expect(worker.okLog.length).toEqual(3) // Three rounds are needed to update 25 keys, 10 each time }) test('does not fail if _updateCache does not work', async () => { global.server.use( @@ -84,7 +83,7 @@ describe('Update-cache worker', () => { throw new Error('Something went wrong at _updateCache, but be cool') }) ) - await worker.updateCache('all') + await worker.updateCache([...mockKeysValues.keys()]) expect(worker.errLog[0].message).toContain( 'Something went wrong at _updateCache, but be cool' ) @@ -107,21 +106,27 @@ describe('Update-cache worker', () => { ) }) ) - await worker.updateCache('all') + await worker.updateCache([...mockKeysValues.keys()]) expect(worker.errLog[0].message).toContain( 'Something went wrong while updating value of "de.serlo.org/api/key20", but keep calm' ) }) test('successfully updates only some values', async () => { - await worker.updateCache( - 'de.serlo.org/api/key0,de.serlo.org/api/key7,de.serlo.org/api/key10,de.serlo.org/api/key20' - ) + await worker.updateCache([ + 'de.serlo.org/api/key0', + 'de.serlo.org/api/key7', + 'de.serlo.org/api/key10', + 'de.serlo.org/api/key20', + ]) expect(worker.errLog).toEqual([]) }) test('does not crash even though it had a problem with some values', async () => { - await worker.updateCache( - 'de.serlo.org/api/key0,de.serlo.org/api/keyInexistent,de.serlo.org/api/key10,de.serlo.org/api/keyWrong' - ) + await worker.updateCache([ + 'de.serlo.org/api/key0', + 'de.serlo.org/api/keyInexistent', + 'de.serlo.org/api/key10', + 'de.serlo.org/api/keyWrong', + ]) expect(worker.errLog).not.toEqual([]) }) }) diff --git a/cache-worker/Dockerfile b/cache-worker/Dockerfile index 3c604bb91..24976d1d8 100644 --- a/cache-worker/Dockerfile +++ b/cache-worker/Dockerfile @@ -14,10 +14,10 @@ RUN yarn --frozen-lockfile --production=true --silent FROM dev-dependencies as build COPY src src COPY index.ts . +COPY cache-keys.ts . RUN yarn build FROM prod-dependencies as release -ENV CACHE_KEYS all ENV CACHE_KEYS_RETRIES 2 COPY --from=build /usr/src/app/dist dist ENTRYPOINT ["node", "dist"] \ No newline at end of file diff --git a/cache-worker/cache-keys.ts b/cache-worker/cache-keys.ts new file mode 100644 index 000000000..ed27228a0 --- /dev/null +++ b/cache-worker/cache-keys.ts @@ -0,0 +1,5 @@ +export const cacheKeys: string[] = [ + 'de.serlo.org/example', + 'de.serlo.org/anotherexample', + 'spreadsheet-randomTokenOfTheSpreadSheetId-bla10921pçklj-sheet1!A:A-ROWS', +] diff --git a/cache-worker/index.ts b/cache-worker/index.ts index ffbc0fe92..280f4cf11 100644 --- a/cache-worker/index.ts +++ b/cache-worker/index.ts @@ -21,6 +21,7 @@ */ import { CacheWorker } from './src/cache-worker' import { Service } from './src/lib' +import { cacheKeys } from './cache-keys' start() @@ -31,7 +32,6 @@ async function start() { service: process.env.SERLO_SERVICE as Service, }) - const cacheKeys = process.env.CACHE_KEYS as string const MAX_RETRIES = parseInt(process.env.CACHE_KEYS_RETRIES!) console.log('Updating cache values of the following keys:', cacheKeys) @@ -48,7 +48,7 @@ async function start() { type Config = { cacheWorker: CacheWorker - cacheKeys: string + cacheKeys: string[] MAX_RETRIES: number } @@ -64,9 +64,9 @@ async function run(config: Config, retries: number = 2) { } function retry(config: Config, retries: number) { - config.cacheWorker.errLog = [] - console.log('Retrying to update cache') - setTimeout(async () => await run(config, ++retries), 5000) + config.cacheWorker.errLog = [] + console.log('Retrying to update cache') + setTimeout(async () => await run(config, ++retries), 5000) } function declareFailure(errors: Error[]) { @@ -77,7 +77,7 @@ function declareFailure(errors: Error[]) { } function checkSuccess(errLog: Error[]) { - if(errLog == []) { + if (errLog == []) { console.log('Cache successfully updated') return true }