Skip to content

Commit

Permalink
Read the cache keys to be updated from a ts file
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
hugotiburtino committed Oct 14, 2020
1 parent c73b8c2 commit 998243d
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 17 deletions.
25 changes: 15 additions & 10 deletions __tests__/cache-worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,17 +74,16 @@ 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(
serloApi.mutation('_updateCache', () => {
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'
)
Expand All @@ -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([])
})
})
2 changes: 1 addition & 1 deletion cache-worker/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
5 changes: 5 additions & 0 deletions cache-worker/cache-keys.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export const cacheKeys: string[] = [
'de.serlo.org/example',
'de.serlo.org/anotherexample',
'spreadsheet-randomTokenOfTheSpreadSheetId-bla10921pçklj-sheet1!A:A-ROWS',
]
12 changes: 6 additions & 6 deletions cache-worker/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
*/
import { CacheWorker } from './src/cache-worker'
import { Service } from './src/lib'
import { cacheKeys } from './cache-keys'

start()

Expand All @@ -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)
Expand All @@ -48,7 +48,7 @@ async function start() {

type Config = {
cacheWorker: CacheWorker
cacheKeys: string
cacheKeys: string[]
MAX_RETRIES: number
}

Expand All @@ -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[]) {
Expand All @@ -77,7 +77,7 @@ function declareFailure(errors: Error[]) {
}

function checkSuccess(errLog: Error[]) {
if(errLog == []) {
if (errLog == []) {
console.log('Cache successfully updated')
return true
}
Expand Down

0 comments on commit 998243d

Please sign in to comment.