From d209816619317dfec504715d28f671edd66a68f9 Mon Sep 17 00:00:00 2001 From: "Patrik J. Braun" Date: Sat, 10 Dec 2022 10:14:25 +0100 Subject: [PATCH] Making jobs and particularly preview filling job non-blocking --- src/backend/model/database/sql/AlbumManager.ts | 3 +++ src/backend/model/jobs/jobs/Job.ts | 3 +++ 2 files changed, 6 insertions(+) diff --git a/src/backend/model/database/sql/AlbumManager.ts b/src/backend/model/database/sql/AlbumManager.ts index 0416ff03..c67c78bb 100644 --- a/src/backend/model/database/sql/AlbumManager.ts +++ b/src/backend/model/database/sql/AlbumManager.ts @@ -106,6 +106,9 @@ export class AlbumManager implements IAlbumManager { for (const a of albums) { await AlbumManager.updateAlbum(a as SavedSearchEntity); + // giving back the control to the main event loop (Macrotask queue) + // https://blog.insiderattack.net/promises-next-ticks-and-immediates-nodejs-event-loop-part-3-9226cbe7a6aa + await new Promise(setImmediate); } this.isDBValid = true; } diff --git a/src/backend/model/jobs/jobs/Job.ts b/src/backend/model/jobs/jobs/Job.ts index 7ebb7478..5538c4ec 100644 --- a/src/backend/model/jobs/jobs/Job.ts +++ b/src/backend/model/jobs/jobs/Job.ts @@ -144,6 +144,9 @@ export abstract class Job implements IJob { this.onFinish(); return; } + // giving back the control to the main event loop (Macrotask queue) + // https://blog.insiderattack.net/promises-next-ticks-and-immediates-nodejs-event-loop-part-3-9226cbe7a6aa + await new Promise(setImmediate); this.run(); } catch (e) { Logger.error(LOG_TAG, e);