improve process lock logic

This commit is contained in:
Erik 2024-03-28 10:16:04 +02:00
parent a79d14bf3f
commit ea505e0474

View File

@ -94,7 +94,7 @@ class SpotifyDownloader extends Downloader
} }
this.logger.info(`Queueing song for download: ${url}`); this.logger.info(`Queueing song for download: ${url}`);
return new Promise((resolve, reject) => const promise = new Promise<DownloaderResult>((resolve, reject) =>
{ {
this.#queue.push({ this.#queue.push({
resolve, reject, id, url resolve, reject, id, url
@ -102,6 +102,12 @@ class SpotifyDownloader extends Downloader
this.#processQueue(); this.#processQueue();
// child.stdout?.on('data', (chunk => this.logger.debug('child stdout:', chunk))); // child.stdout?.on('data', (chunk => this.logger.debug('child stdout:', chunk)));
}); });
promise.finally(() =>
{
this.#processing = false;
this.#processQueue();
});
return promise;
} }
async #processQueue () 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) => childProcess.exec(`"${this.#executable}" --root-path "${this.downloadDir}" ${this.#options.join(' ')} ${url}`, (error, stdout, stderr) =>
{ {
if (error) if (error)
{
this.#processing = false;
return reject(error); return reject(error);
}
if (stderr && !stderr.includes('###')) if (stderr && !stderr.includes('###'))
this.logger.debug('stderr', stderr); this.logger.debug('stderr', stderr);
@ -144,10 +147,7 @@ class SpotifyDownloader extends Downloader
data = this.#parseFilename(file); data = this.#parseFilename(file);
} }
if (!data) if (!data)
{
this.#processing = false;
return reject(new Error('Failed to find file reference')); return reject(new Error('Failed to find file reference'));
}
const { fileName, artist, song, album, ext } = data; const { fileName, artist, song, album, ext } = data;
const filePath = path.join(this.downloadDir!, fileName); const filePath = path.join(this.downloadDir!, fileName);
@ -159,10 +159,6 @@ class SpotifyDownloader extends Downloader
album, album,
ext ext
}); });
this.#processing = false;
if (this.#queue.length)
process.nextTick(this.#processQueue.bind(this));
}); });
} }