From ae501048f59646fc688fe0373f938096dab5bd7f Mon Sep 17 00:00:00 2001 From: Pagan Gazzard Date: Tue, 18 Jan 2022 11:44:40 +0000 Subject: [PATCH 1/2] Ensure the `finish` event is always reported when fetching images Change-type: patch --- src/compose/images.ts | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/compose/images.ts b/src/compose/images.ts index e57185c6..d8ca7aaf 100644 --- a/src/compose/images.ts +++ b/src/compose/images.ts @@ -172,7 +172,7 @@ export function imageFromService(service: ServiceInfo): Image { export async function triggerFetch( image: Image, opts: FetchOptions, - onFinish = _.noop, + onFinish: (success: boolean) => void, serviceName: string, ): Promise { const appUpdatePollInterval = await config.get('appUpdatePollInterval'); @@ -202,8 +202,7 @@ export async function triggerFetch( let success: boolean; try { const imageName = normalise(image.name); - image = _.clone(image); - image.name = imageName; + image = { ...image, name: imageName }; // Look for a matching image on the engine const img = await inspectByName(image.name); @@ -214,8 +213,7 @@ export async function triggerFetch( // Create image on the database if it already exists on the engine await markAsSupervised({ ...image, dockerImageId: img.Id }); - onFinish(true); - return; + success = true; } catch (e) { if (!NotFoundError(e)) { if (!(e instanceof ImageDownloadBackoffError)) { From fa0e28de6d5db0329c8298ce1c8632dcd24b4d7a Mon Sep 17 00:00:00 2001 From: Felipe Lalanne Date: Tue, 18 Jan 2022 14:49:03 +0000 Subject: [PATCH 2/2] Clean up image event reporting --- src/compose/images.ts | 35 ++++++++++++++++------------------- 1 file changed, 16 insertions(+), 19 deletions(-) diff --git a/src/compose/images.ts b/src/compose/images.ts index d8ca7aaf..67f8bcb6 100644 --- a/src/compose/images.ts +++ b/src/compose/images.ts @@ -125,29 +125,26 @@ const runningTasks: { [imageName: string]: ImageTask } = {}; function reportEvent(event: 'start' | 'update' | 'finish', state: Image) { const { name: imageName } = state; - // Emit by default if a start event is reported - let emitChange = event === 'start'; - // Get the current task and update it in memory - const currentTask = - event === 'start' ? createTask(state) : runningTasks[imageName]; + const currentTask = runningTasks[imageName] ?? createTask(state); runningTasks[imageName] = currentTask; - // TODO: should we assert that the current task exists at this point? - // On update, update the corresponding task with the new state if it exists - if (event === 'update' && currentTask) { - const [updatedTask, changed] = currentTask.update(state); - runningTasks[imageName] = updatedTask; - emitChange = changed; - } + const stateChanged = (() => { + switch (event) { + case 'start': + return true; // always report change on start + case 'update': + const [updatedTask, changedAfterUpdate] = currentTask.update(state); + runningTasks[imageName] = updatedTask; + return changedAfterUpdate; // report change only if the task context changed + case 'finish': + const [, changedAfterFinish] = currentTask.finish(); + delete runningTasks[imageName]; + return changedAfterFinish; // report change depending on the state of the task + } + })(); - // On update, update the corresponding task with the new state if it exists - if (event === 'finish' && currentTask) { - [, emitChange] = currentTask.finish(); - delete runningTasks[imageName]; - } - - if (emitChange) { + if (stateChanged) { events.emit('change'); } }