From d51664e7a07b5aac0fb3b97e1b9aae662dbd32a7 Mon Sep 17 00:00:00 2001 From: skjnldsv Date: Tue, 10 Dec 2024 18:00:20 +0100 Subject: [PATCH] fix(uploader): only monitor the queue being idle when we kow we are finishing the upload Signed-off-by: skjnldsv --- lib/uploader.ts | 19 ++++++++++++++++--- lib/utils/upload.ts | 4 ++++ 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/lib/uploader.ts b/lib/uploader.ts index 08c387f3..310c77e3 100644 --- a/lib/uploader.ts +++ b/lib/uploader.ts @@ -88,9 +88,6 @@ export class Uploader { } this.destination = destinationFolder - // Reset when upload queue is done - this._jobQueue.addListener('idle', () => this.reset()) - logger.debug('Upload workspace initialized', { destination: this.destination, root: this.root, @@ -157,12 +154,18 @@ export class Uploader { } private reset() { + // If there is no upload in the queue and no job in the queue + if (this._uploadQueue.length === 0 && this._jobQueue.size === 0) { + return + } + // Reset upload queue but keep the reference this._uploadQueue.splice(0, this._uploadQueue.length) this._jobQueue.clear() this._queueSize = 0 this._queueProgress = 0 this._queueStatus = Status.IDLE + logger.debug('Uploader state reset') } /** @@ -595,6 +598,16 @@ export class Uploader { this._jobQueue.add(request) this.updateStats() } + + // Reset when upload queue is done + // Only when we know we're closing on the last chunks + // and/or assembling we can reset the uploader. + // Otherwise he queue might be idle for a short time + // and clear the Upload queue before we're done. + this._jobQueue.onIdle() + .then(() => this.reset()) + + // Finally return the Upload return upload }) as PCancelable diff --git a/lib/utils/upload.ts b/lib/utils/upload.ts index 433db526..dfc33a7c 100644 --- a/lib/utils/upload.ts +++ b/lib/utils/upload.ts @@ -8,6 +8,8 @@ import { getCurrentUser } from '@nextcloud/auth' import axios from '@nextcloud/axios' import axiosRetry, { exponentialDelay } from 'axios-retry' +import logger from './logger' + axiosRetry(axios, { retries: 0 }) type UploadData = Blob | (() => Promise) @@ -102,5 +104,7 @@ export const initChunkWorkspace = async function(destinationFile: string | undef }, }) + logger.debug('Created temporary upload workspace', { url }) + return url }