From fa7dfc0264c5327d8afc08aa64ff8af36fc2fa5d Mon Sep 17 00:00:00 2001 From: Zack Pollard Date: Fri, 25 Oct 2024 12:09:58 +0100 Subject: [PATCH] fix: library scanning cron job created on every instance instead of just one --- server/src/services/library.service.spec.ts | 9 +++++++++ server/src/services/library.service.ts | 14 ++++++++------ 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/server/src/services/library.service.spec.ts b/server/src/services/library.service.spec.ts index 5258c8d035b52..3675b68c5309e 100644 --- a/server/src/services/library.service.spec.ts +++ b/server/src/services/library.service.spec.ts @@ -117,6 +117,15 @@ describe(LibraryService.name, () => { expect(storageMock.watch).not.toHaveBeenCalled(); }); + + it('should not initialize library scan cron job when lock is taken', async () => { + systemMock.get.mockResolvedValue(systemConfigStub.libraryWatchEnabled); + databaseMock.tryLock.mockResolvedValue(false); + + await sut.onBootstrap(); + + expect(jobMock.addCronJob).not.toHaveBeenCalled(); + }); }); describe('onConfigUpdateEvent', () => { diff --git a/server/src/services/library.service.ts b/server/src/services/library.service.ts index e319983d3b3a1..e99fd9159003b 100644 --- a/server/src/services/library.service.ts +++ b/server/src/services/library.service.ts @@ -51,12 +51,14 @@ export class LibraryService extends BaseService { this.watchLibraries = this.watchLock && watch.enabled; - this.jobRepository.addCronJob( - 'libraryScan', - scan.cronExpression, - () => handlePromiseError(this.jobRepository.queue({ name: JobName.LIBRARY_QUEUE_SYNC_ALL }), this.logger), - scan.enabled, - ); + if (this.watchLock) { + this.jobRepository.addCronJob( + 'libraryScan', + scan.cronExpression, + () => handlePromiseError(this.jobRepository.queue({ name: JobName.LIBRARY_QUEUE_SYNC_ALL }), this.logger), + scan.enabled, + ); + } if (this.watchLibraries) { await this.watchAll();