refactor(server): filter on-event handlers by worker (#14085)

This commit is contained in:
Jason Rasmussen
2024-11-20 14:08:42 -05:00
committed by GitHub
parent c6e1dbec5c
commit 876893c823
10 changed files with 24 additions and 70 deletions

View File

@@ -14,16 +14,12 @@ import { handlePromiseError } from 'src/utils/misc';
export class BackupService extends BaseService {
private backupLock = false;
@OnEvent({ name: 'config.init' })
@OnEvent({ name: 'config.init', workers: [ImmichWorker.MICROSERVICES] })
async onConfigInit({
newConfig: {
backup: { database },
},
}: ArgOf<'config.init'>) {
if (this.worker !== ImmichWorker.API) {
return;
}
this.backupLock = await this.databaseRepository.tryLock(DatabaseLock.BackupDatabase);
if (this.backupLock) {

View File

@@ -38,12 +38,8 @@ const asJobItem = (dto: JobCreateDto): JobItem => {
@Injectable()
export class JobService extends BaseService {
@OnEvent({ name: 'config.init' })
@OnEvent({ name: 'config.init', workers: [ImmichWorker.MICROSERVICES] })
onConfigInit({ newConfig: config }: ArgOf<'config.init'>) {
if (this.worker !== ImmichWorker.MICROSERVICES) {
return;
}
this.logger.debug(`Updating queue concurrency settings`);
for (const queueName of Object.values(QueueName)) {
let concurrency = 1;

View File

@@ -122,13 +122,6 @@ describe(LibraryService.name, () => {
expect(cronMock.create).not.toHaveBeenCalled();
});
it('should not initialize watcher or library scan job when running on api', async () => {
configMock.getWorker.mockReturnValue(ImmichWorker.API);
await sut.onConfigInit({ newConfig: systemConfigStub.libraryScan as SystemConfig });
expect(cronMock.create).not.toHaveBeenCalled();
});
});
describe('onConfigUpdateEvent', () => {

View File

@@ -31,16 +31,12 @@ export class LibraryService extends BaseService {
private lock = false;
private watchers: Record<string, () => Promise<void>> = {};
@OnEvent({ name: 'config.init' })
@OnEvent({ name: 'config.init', workers: [ImmichWorker.MICROSERVICES] })
async onConfigInit({
newConfig: {
library: { watch, scan },
},
}: ArgOf<'config.init'>) {
if (this.worker !== ImmichWorker.MICROSERVICES) {
return;
}
// This ensures that library watching only occurs in one microservice
this.lock = await this.databaseRepository.tryLock(DatabaseLock.Library);

View File

@@ -94,15 +94,6 @@ describe(MetadataService.name, () => {
expect(mapMock.init).toHaveBeenCalledTimes(1);
expect(jobMock.resume).toHaveBeenCalledTimes(1);
});
it('should return if running on api', async () => {
configMock.getWorker.mockReturnValue(ImmichWorker.API);
await sut.onBootstrap();
expect(jobMock.pause).not.toHaveBeenCalled();
expect(mapMock.init).not.toHaveBeenCalled();
expect(jobMock.resume).not.toHaveBeenCalled();
});
});
describe('handleLivePhotoLinking', () => {

View File

@@ -68,11 +68,8 @@ const validateRange = (value: number | undefined, min: number, max: number): Non
@Injectable()
export class MetadataService extends BaseService {
@OnEvent({ name: 'app.bootstrap' })
@OnEvent({ name: 'app.bootstrap', workers: [ImmichWorker.MICROSERVICES] })
async onBootstrap() {
if (this.worker !== ImmichWorker.MICROSERVICES) {
return;
}
this.logger.log('Bootstrapping metadata service');
await this.init();
}

View File

@@ -67,19 +67,6 @@ describe(SmartInfoService.name, () => {
});
describe('onConfigInit', () => {
it('should return if not microservices', async () => {
configMock.getWorker.mockReturnValue(ImmichWorker.API);
await sut.onConfigInit({ newConfig: systemConfigStub.machineLearningEnabled as SystemConfig });
expect(searchMock.getDimensionSize).not.toHaveBeenCalled();
expect(searchMock.setDimensionSize).not.toHaveBeenCalled();
expect(searchMock.deleteAllSearchEmbeddings).not.toHaveBeenCalled();
expect(jobMock.getQueueStatus).not.toHaveBeenCalled();
expect(jobMock.pause).not.toHaveBeenCalled();
expect(jobMock.waitForQueueCompletion).not.toHaveBeenCalled();
expect(jobMock.resume).not.toHaveBeenCalled();
});
it('should return if machine learning is disabled', async () => {
await sut.onConfigInit({ newConfig: systemConfigStub.machineLearningDisabled as SystemConfig });
@@ -136,22 +123,6 @@ describe(SmartInfoService.name, () => {
});
describe('onConfigUpdateEvent', () => {
it('should return if not microservices', async () => {
configMock.getWorker.mockReturnValue(ImmichWorker.API);
await sut.onConfigUpdate({
newConfig: systemConfigStub.machineLearningEnabled as SystemConfig,
oldConfig: systemConfigStub.machineLearningEnabled as SystemConfig,
});
expect(searchMock.getDimensionSize).not.toHaveBeenCalled();
expect(searchMock.setDimensionSize).not.toHaveBeenCalled();
expect(searchMock.deleteAllSearchEmbeddings).not.toHaveBeenCalled();
expect(jobMock.getQueueStatus).not.toHaveBeenCalled();
expect(jobMock.pause).not.toHaveBeenCalled();
expect(jobMock.waitForQueueCompletion).not.toHaveBeenCalled();
expect(jobMock.resume).not.toHaveBeenCalled();
});
it('should return if machine learning is disabled', async () => {
systemMock.get.mockResolvedValue(systemConfigStub.machineLearningDisabled);

View File

@@ -13,12 +13,12 @@ import { usePagination } from 'src/utils/pagination';
@Injectable()
export class SmartInfoService extends BaseService {
@OnEvent({ name: 'config.init' })
@OnEvent({ name: 'config.init', workers: [ImmichWorker.MICROSERVICES] })
async onConfigInit({ newConfig }: ArgOf<'config.init'>) {
await this.init(newConfig);
}
@OnEvent({ name: 'config.update', server: true })
@OnEvent({ name: 'config.update', workers: [ImmichWorker.MICROSERVICES], server: true })
async onConfigUpdate({ oldConfig, newConfig }: ArgOf<'config.update'>) {
await this.init(newConfig, oldConfig);
}
@@ -35,7 +35,7 @@ export class SmartInfoService extends BaseService {
}
private async init(newConfig: SystemConfig, oldConfig?: SystemConfig) {
if (this.worker !== ImmichWorker.MICROSERVICES || !isSmartSearchEnabled(newConfig.machineLearning)) {
if (!isSmartSearchEnabled(newConfig.machineLearning)) {
return;
}