From 5edbb93a4db07761d9c4e1cd4d0c80d47ec8fdf5 Mon Sep 17 00:00:00 2001 From: Jason Rasmussen Date: Tue, 5 Nov 2024 08:07:51 -0500 Subject: [PATCH] refactor(server): get worker (#13927) --- server/src/interfaces/config.interface.ts | 1 + server/src/repositories/config.repository.ts | 10 ++++++++-- server/src/services/base.service.ts | 10 ++++++---- server/src/services/job.service.spec.ts | 8 +++++--- server/test/repositories/config.repository.mock.ts | 1 + server/test/utils.ts | 5 +---- 6 files changed, 22 insertions(+), 13 deletions(-) diff --git a/server/src/interfaces/config.interface.ts b/server/src/interfaces/config.interface.ts index ec5397cc2cdd4..300b55f27b6fd 100644 --- a/server/src/interfaces/config.interface.ts +++ b/server/src/interfaces/config.interface.ts @@ -93,4 +93,5 @@ export interface EnvData { export interface IConfigRepository { getEnv(): EnvData; + getWorker(): ImmichWorker | undefined; } diff --git a/server/src/repositories/config.repository.ts b/server/src/repositories/config.repository.ts index 76b0bb0c83b18..a8a1c9972b3d5 100644 --- a/server/src/repositories/config.repository.ts +++ b/server/src/repositories/config.repository.ts @@ -1,10 +1,10 @@ -import { Injectable } from '@nestjs/common'; +import { Inject, Injectable, Optional } from '@nestjs/common'; import { plainToInstance } from 'class-transformer'; import { validateSync } from 'class-validator'; import { Request, Response } from 'express'; import { CLS_ID } from 'nestjs-cls'; import { join, resolve } from 'node:path'; -import { citiesFile, excludePaths } from 'src/constants'; +import { citiesFile, excludePaths, IWorker } from 'src/constants'; import { Telemetry } from 'src/decorators'; import { EnvDto } from 'src/dtos/env.dto'; import { ImmichEnvironment, ImmichHeader, ImmichTelemetry, ImmichWorker } from 'src/enum'; @@ -228,6 +228,8 @@ let cached: EnvData | undefined; @Injectable() @Telemetry({ enabled: false }) export class ConfigRepository implements IConfigRepository { + constructor(@Inject(IWorker) @Optional() private worker?: ImmichWorker) {} + getEnv(): EnvData { if (!cached) { cached = getEnv(); @@ -235,6 +237,10 @@ export class ConfigRepository implements IConfigRepository { return cached; } + + getWorker() { + return this.worker; + } } export const clearEnvCache = () => (cached = undefined); diff --git a/server/src/services/base.service.ts b/server/src/services/base.service.ts index 124e45b62cf58..dc7dab102c69d 100644 --- a/server/src/services/base.service.ts +++ b/server/src/services/base.service.ts @@ -1,10 +1,9 @@ -import { BadRequestException, Inject, Optional } from '@nestjs/common'; +import { BadRequestException, Inject } from '@nestjs/common'; import sanitize from 'sanitize-filename'; import { SystemConfig } from 'src/config'; -import { IWorker, SALT_ROUNDS } from 'src/constants'; +import { SALT_ROUNDS } from 'src/constants'; import { StorageCore } from 'src/cores/storage.core'; import { UserEntity } from 'src/entities/user.entity'; -import { ImmichWorker } from 'src/enum'; import { IAccessRepository } from 'src/interfaces/access.interface'; import { IActivityRepository } from 'src/interfaces/activity.interface'; import { IAlbumUserRepository } from 'src/interfaces/album-user.interface'; @@ -50,7 +49,6 @@ export class BaseService { protected storageCore: StorageCore; constructor( - @Inject(IWorker) @Optional() protected worker: ImmichWorker | undefined, @Inject(ILoggerRepository) protected logger: ILoggerRepository, @Inject(IAccessRepository) protected accessRepository: IAccessRepository, @Inject(IActivityRepository) protected activityRepository: IActivityRepository, @@ -103,6 +101,10 @@ export class BaseService { ); } + get worker() { + return this.configRepository.getWorker(); + } + private get configRepos() { return { configRepo: this.configRepository, diff --git a/server/src/services/job.service.spec.ts b/server/src/services/job.service.spec.ts index 0e7607f27a9b4..b4599917857f1 100644 --- a/server/src/services/job.service.spec.ts +++ b/server/src/services/job.service.spec.ts @@ -2,6 +2,7 @@ import { BadRequestException } from '@nestjs/common'; import { defaults } from 'src/config'; import { ImmichWorker } from 'src/enum'; import { IAssetRepository } from 'src/interfaces/asset.interface'; +import { IConfigRepository } from 'src/interfaces/config.interface'; import { IJobRepository, JobCommand, JobItem, JobName, JobStatus, QueueName } from 'src/interfaces/job.interface'; import { ILoggerRepository } from 'src/interfaces/logger.interface'; import { ITelemetryRepository } from 'src/interfaces/telemetry.interface'; @@ -13,14 +14,15 @@ import { Mocked } from 'vitest'; describe(JobService.name, () => { let sut: JobService; let assetMock: Mocked; + let configMock: Mocked; let jobMock: Mocked; let loggerMock: Mocked; let telemetryMock: Mocked; beforeEach(() => { - ({ sut, assetMock, jobMock, loggerMock, telemetryMock } = newTestService(JobService, { - worker: ImmichWorker.MICROSERVICES, - })); + ({ sut, assetMock, configMock, jobMock, loggerMock, telemetryMock } = newTestService(JobService, {})); + + configMock.getWorker.mockReturnValue(ImmichWorker.MICROSERVICES); }); it('should work', () => { diff --git a/server/test/repositories/config.repository.mock.ts b/server/test/repositories/config.repository.mock.ts index 55a890fa4a663..df26f7f72565f 100644 --- a/server/test/repositories/config.repository.mock.ts +++ b/server/test/repositories/config.repository.mock.ts @@ -96,5 +96,6 @@ export const mockEnvData = (config: Partial) => ({ ...envData, ...confi export const newConfigRepositoryMock = (): Mocked => { return { getEnv: vitest.fn().mockReturnValue(mockEnvData({})), + getWorker: vitest.fn().mockReturnValue(ImmichWorker.API), }; }; diff --git a/server/test/utils.ts b/server/test/utils.ts index a5a61190338b7..d37af5118d323 100644 --- a/server/test/utils.ts +++ b/server/test/utils.ts @@ -58,9 +58,7 @@ export const newTestService = ( Service: Constructor, overrides?: Overrides, ) => { - const { metadataRepository, worker: workerOverride } = overrides || {}; - - const worker = workerOverride || ImmichWorker.API; + const { metadataRepository } = overrides || {}; const accessMock = newAccessRepositoryMock(); const loggerMock = newLoggerRepositoryMock(); @@ -102,7 +100,6 @@ export const newTestService = ( const viewMock = newViewRepositoryMock(); const sut = new Service( - worker, loggerMock, accessMock, activityMock,