From ea505e04741621ebf4a7f77c4b350ad340c309c4 Mon Sep 17 00:00:00 2001 From: "Navy.gif" Date: Thu, 28 Mar 2024 10:16:04 +0200 Subject: [PATCH] improve process lock logic --- src/client/components/downloaders/Spotify.ts | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/src/client/components/downloaders/Spotify.ts b/src/client/components/downloaders/Spotify.ts index a7b6328..3494671 100644 --- a/src/client/components/downloaders/Spotify.ts +++ b/src/client/components/downloaders/Spotify.ts @@ -94,7 +94,7 @@ class SpotifyDownloader extends Downloader } this.logger.info(`Queueing song for download: ${url}`); - return new Promise((resolve, reject) => + const promise = new Promise((resolve, reject) => { this.#queue.push({ resolve, reject, id, url @@ -102,6 +102,12 @@ class SpotifyDownloader extends Downloader this.#processQueue(); // child.stdout?.on('data', (chunk => this.logger.debug('child stdout:', chunk))); }); + promise.finally(() => + { + this.#processing = false; + this.#processQueue(); + }); + return promise; } async #processQueue () @@ -119,10 +125,7 @@ class SpotifyDownloader extends Downloader childProcess.exec(`"${this.#executable}" --root-path "${this.downloadDir}" ${this.#options.join(' ')} ${url}`, (error, stdout, stderr) => { if (error) - { - this.#processing = false; return reject(error); - } if (stderr && !stderr.includes('###')) this.logger.debug('stderr', stderr); @@ -144,10 +147,7 @@ class SpotifyDownloader extends Downloader data = this.#parseFilename(file); } if (!data) - { - this.#processing = false; return reject(new Error('Failed to find file reference')); - } const { fileName, artist, song, album, ext } = data; const filePath = path.join(this.downloadDir!, fileName); @@ -159,10 +159,6 @@ class SpotifyDownloader extends Downloader album, ext }); - - this.#processing = false; - if (this.#queue.length) - process.nextTick(this.#processQueue.bind(this)); }); }