diff --git a/integration/update-version_test.js b/integration/update-version_test.js index 82475fbadf53e6b474ce8988a77b92c289ab9abe..c1bb9e3d0dc6e32a95600cf905f435ff5fdc7f41 100644 --- a/integration/update-version_test.js +++ b/integration/update-version_test.js @@ -21,6 +21,7 @@ describe('Updates the version', function () { '/index.html': () => new Response('<html><head></head><body>it\'s me</body></html>', { headers: { 'content-type': 'text/html' } }), '/meta.json': td.when(td.func()(td.matchers.anything())).thenReturn( new Response(JSON.stringify({ commitSha: '1' }), { headers: { 'Content-Type': 'application/json' } }), + new Response(JSON.stringify({ commitSha: '2' }), { headers: { 'Content-Type': 'application/json' } }), new Response(JSON.stringify({ commitSha: '2' }), { headers: { 'Content-Type': 'application/json' } }) ) } @@ -46,10 +47,10 @@ describe('Updates the version', function () { expect(responseBeforeUpdate.statusCode).to.equal(200) expect(responseBeforeUpdate.headers.version).to.equal('85101541') - const job = await getQueue('update-version').add({}) - - const newVersion = await job.finished() - expect(newVersion).to.equal('85102502') + // update has only been registered but not executed yet + expect(await getQueue('update-version').add({}).then(job => job.finished())).to.equal('85101541') + // update is executed with the second iteration + expect(await getQueue('update-version').add({}).then(job => job.finished())).to.equal('85102502') const responseAfterUpdate = await request(app).get('/index.html') expect(responseAfterUpdate.statusCode).to.equal(200) @@ -66,9 +67,12 @@ describe('Updates the version', function () { // need to do this with dynamic import such that the mocked config is used await import('../src/create-queues.js').then(({ default: createQueues }) => createQueues()) - // pause the queue to prevent any further updates const queue = getQueue('update-version') + let count = 0 await new Promise(resolve => queue.on('global:completed', (jobId, result) => { + // only resolve when the second job has been completed as the "update" job needs to be executed twice + if (++count === 1) return + // pause the queue to prevent any further updates queue.pause() resolve() })) diff --git a/spec/file-depencies_test.js b/spec/file-depencies_test.js index 64a77c3ae80afbb22f6ab98fd02597b310d9de06..b46e50521edb71aa9d08647b792b43f217fa6a36 100644 --- a/spec/file-depencies_test.js +++ b/spec/file-depencies_test.js @@ -59,7 +59,11 @@ describe('JS files with dependencies contain events', function () { } }) - await import('../src/version.js').then(({ updateVersionProcessor }) => updateVersionProcessor()) + await import('../src/version.js').then(async ({ updateVersionProcessor }) => { + // need to process two times to actually trigger the update + await updateVersionProcessor() + await updateVersionProcessor() + }) const r2 = await request(app).get('/index.html.js') expect(r2.headers.dependencies).to.equal('other.css') diff --git a/spec/meta_test.js b/spec/meta_test.js index 5b57dd23dc8f86c490c96c4f93c065912e74d2f5..de4a2a8e0f8ba958c2da4befffb0f34574b71d24 100644 --- a/spec/meta_test.js +++ b/spec/meta_test.js @@ -62,7 +62,11 @@ describe('Responses contain custom headers', function () { expect(response.body).to.have.length(2) config.urls = [] - await import('../src/version.js').then(({ updateVersionProcessor }) => updateVersionProcessor()) + await import('../src/version.js').then(async ({ updateVersionProcessor }) => { + // need to process two times to actually trigger the update + await updateVersionProcessor() + await updateVersionProcessor() + }) const response2 = await request(app).get('/meta') expect(response2.body).to.have.length(1) diff --git a/src/version.js b/src/version.js index 87fe1bd8e91e5782ac18a6d46667ebf48a240c45..87c7eb71a2a624bdd58a13da197a3296d47eb61f 100644 --- a/src/version.js +++ b/src/version.js @@ -81,15 +81,19 @@ export async function updateVersionProcessor () { const prevProcessedVersion = await redis.client.get(getRedisKey({ name: 'prevProcessedVersion' })) // that means, that between the previous update processing and this one, there was no version change if (prevProcessedVersion === fetchedVersion) { + logger.info('[Version] publish update to other nodes.') redis.pubClient.publish(getRedisKey({ name: 'updateLatestVersion' }), fetchedVersion) await redis.client.set(getRedisKey({ name: 'latestVersion' }), fetchedVersion) + latestVersion = fetchedVersion } else { + logger.info(`[Version] do not execute update yet. Store version ${fetchedVersion} as previous version.`) await redis.client.set(getRedisKey({ name: 'prevProcessedVersion' }), fetchedVersion) } } else { // 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() + latestVersion = fetchedVersion } - return (latestVersion = fetchedVersion) + return latestVersion }