1
0
Fork 0
mirror of https://github.com/immich-app/immich.git synced 2024-12-29 15:11:58 +00:00

refactor(server): get worker (#13927)

This commit is contained in:
Jason Rasmussen 2024-11-05 08:07:51 -05:00 committed by GitHub
parent 1d55b5bfc0
commit 5edbb93a4d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 22 additions and 13 deletions

View file

@ -93,4 +93,5 @@ export interface EnvData {
export interface IConfigRepository { export interface IConfigRepository {
getEnv(): EnvData; getEnv(): EnvData;
getWorker(): ImmichWorker | undefined;
} }

View file

@ -1,10 +1,10 @@
import { Injectable } from '@nestjs/common'; import { Inject, Injectable, Optional } from '@nestjs/common';
import { plainToInstance } from 'class-transformer'; import { plainToInstance } from 'class-transformer';
import { validateSync } from 'class-validator'; import { validateSync } from 'class-validator';
import { Request, Response } from 'express'; import { Request, Response } from 'express';
import { CLS_ID } from 'nestjs-cls'; import { CLS_ID } from 'nestjs-cls';
import { join, resolve } from 'node:path'; 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 { Telemetry } from 'src/decorators';
import { EnvDto } from 'src/dtos/env.dto'; import { EnvDto } from 'src/dtos/env.dto';
import { ImmichEnvironment, ImmichHeader, ImmichTelemetry, ImmichWorker } from 'src/enum'; import { ImmichEnvironment, ImmichHeader, ImmichTelemetry, ImmichWorker } from 'src/enum';
@ -228,6 +228,8 @@ let cached: EnvData | undefined;
@Injectable() @Injectable()
@Telemetry({ enabled: false }) @Telemetry({ enabled: false })
export class ConfigRepository implements IConfigRepository { export class ConfigRepository implements IConfigRepository {
constructor(@Inject(IWorker) @Optional() private worker?: ImmichWorker) {}
getEnv(): EnvData { getEnv(): EnvData {
if (!cached) { if (!cached) {
cached = getEnv(); cached = getEnv();
@ -235,6 +237,10 @@ export class ConfigRepository implements IConfigRepository {
return cached; return cached;
} }
getWorker() {
return this.worker;
}
} }
export const clearEnvCache = () => (cached = undefined); export const clearEnvCache = () => (cached = undefined);

View file

@ -1,10 +1,9 @@
import { BadRequestException, Inject, Optional } from '@nestjs/common'; import { BadRequestException, Inject } from '@nestjs/common';
import sanitize from 'sanitize-filename'; import sanitize from 'sanitize-filename';
import { SystemConfig } from 'src/config'; 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 { StorageCore } from 'src/cores/storage.core';
import { UserEntity } from 'src/entities/user.entity'; import { UserEntity } from 'src/entities/user.entity';
import { ImmichWorker } from 'src/enum';
import { IAccessRepository } from 'src/interfaces/access.interface'; import { IAccessRepository } from 'src/interfaces/access.interface';
import { IActivityRepository } from 'src/interfaces/activity.interface'; import { IActivityRepository } from 'src/interfaces/activity.interface';
import { IAlbumUserRepository } from 'src/interfaces/album-user.interface'; import { IAlbumUserRepository } from 'src/interfaces/album-user.interface';
@ -50,7 +49,6 @@ export class BaseService {
protected storageCore: StorageCore; protected storageCore: StorageCore;
constructor( constructor(
@Inject(IWorker) @Optional() protected worker: ImmichWorker | undefined,
@Inject(ILoggerRepository) protected logger: ILoggerRepository, @Inject(ILoggerRepository) protected logger: ILoggerRepository,
@Inject(IAccessRepository) protected accessRepository: IAccessRepository, @Inject(IAccessRepository) protected accessRepository: IAccessRepository,
@Inject(IActivityRepository) protected activityRepository: IActivityRepository, @Inject(IActivityRepository) protected activityRepository: IActivityRepository,
@ -103,6 +101,10 @@ export class BaseService {
); );
} }
get worker() {
return this.configRepository.getWorker();
}
private get configRepos() { private get configRepos() {
return { return {
configRepo: this.configRepository, configRepo: this.configRepository,

View file

@ -2,6 +2,7 @@ import { BadRequestException } from '@nestjs/common';
import { defaults } from 'src/config'; import { defaults } from 'src/config';
import { ImmichWorker } from 'src/enum'; import { ImmichWorker } from 'src/enum';
import { IAssetRepository } from 'src/interfaces/asset.interface'; 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 { IJobRepository, JobCommand, JobItem, JobName, JobStatus, QueueName } from 'src/interfaces/job.interface';
import { ILoggerRepository } from 'src/interfaces/logger.interface'; import { ILoggerRepository } from 'src/interfaces/logger.interface';
import { ITelemetryRepository } from 'src/interfaces/telemetry.interface'; import { ITelemetryRepository } from 'src/interfaces/telemetry.interface';
@ -13,14 +14,15 @@ import { Mocked } from 'vitest';
describe(JobService.name, () => { describe(JobService.name, () => {
let sut: JobService; let sut: JobService;
let assetMock: Mocked<IAssetRepository>; let assetMock: Mocked<IAssetRepository>;
let configMock: Mocked<IConfigRepository>;
let jobMock: Mocked<IJobRepository>; let jobMock: Mocked<IJobRepository>;
let loggerMock: Mocked<ILoggerRepository>; let loggerMock: Mocked<ILoggerRepository>;
let telemetryMock: Mocked<ITelemetryRepository>; let telemetryMock: Mocked<ITelemetryRepository>;
beforeEach(() => { beforeEach(() => {
({ sut, assetMock, jobMock, loggerMock, telemetryMock } = newTestService(JobService, { ({ sut, assetMock, configMock, jobMock, loggerMock, telemetryMock } = newTestService(JobService, {}));
worker: ImmichWorker.MICROSERVICES,
})); configMock.getWorker.mockReturnValue(ImmichWorker.MICROSERVICES);
}); });
it('should work', () => { it('should work', () => {

View file

@ -96,5 +96,6 @@ export const mockEnvData = (config: Partial<EnvData>) => ({ ...envData, ...confi
export const newConfigRepositoryMock = (): Mocked<IConfigRepository> => { export const newConfigRepositoryMock = (): Mocked<IConfigRepository> => {
return { return {
getEnv: vitest.fn().mockReturnValue(mockEnvData({})), getEnv: vitest.fn().mockReturnValue(mockEnvData({})),
getWorker: vitest.fn().mockReturnValue(ImmichWorker.API),
}; };
}; };

View file

@ -58,9 +58,7 @@ export const newTestService = <T extends BaseService>(
Service: Constructor<T, BaseServiceArgs>, Service: Constructor<T, BaseServiceArgs>,
overrides?: Overrides, overrides?: Overrides,
) => { ) => {
const { metadataRepository, worker: workerOverride } = overrides || {}; const { metadataRepository } = overrides || {};
const worker = workerOverride || ImmichWorker.API;
const accessMock = newAccessRepositoryMock(); const accessMock = newAccessRepositoryMock();
const loggerMock = newLoggerRepositoryMock(); const loggerMock = newLoggerRepositoryMock();
@ -102,7 +100,6 @@ export const newTestService = <T extends BaseService>(
const viewMock = newViewRepositoryMock(); const viewMock = newViewRepositoryMock();
const sut = new Service( const sut = new Service(
worker,
loggerMock, loggerMock,
accessMock, accessMock,
activityMock, activityMock,