Skip to content

Commit

Permalink
refactor(server): get worker (#13927)
Browse files Browse the repository at this point in the history
  • Loading branch information
jrasm91 authored Nov 5, 2024
1 parent 1d55b5b commit 5edbb93
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 13 deletions.
1 change: 1 addition & 0 deletions server/src/interfaces/config.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,5 @@ export interface EnvData {

export interface IConfigRepository {
getEnv(): EnvData;
getWorker(): ImmichWorker | undefined;
}
10 changes: 8 additions & 2 deletions server/src/repositories/config.repository.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -228,13 +228,19 @@ 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();
}

return cached;
}

getWorker() {
return this.worker;
}
}

export const clearEnvCache = () => (cached = undefined);
10 changes: 6 additions & 4 deletions server/src/services/base.service.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -103,6 +101,10 @@ export class BaseService {
);
}

get worker() {
return this.configRepository.getWorker();
}

private get configRepos() {
return {
configRepo: this.configRepository,
Expand Down
8 changes: 5 additions & 3 deletions server/src/services/job.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -13,14 +14,15 @@ import { Mocked } from 'vitest';
describe(JobService.name, () => {
let sut: JobService;
let assetMock: Mocked<IAssetRepository>;
let configMock: Mocked<IConfigRepository>;
let jobMock: Mocked<IJobRepository>;
let loggerMock: Mocked<ILoggerRepository>;
let telemetryMock: Mocked<ITelemetryRepository>;

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', () => {
Expand Down
1 change: 1 addition & 0 deletions server/test/repositories/config.repository.mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,5 +96,6 @@ export const mockEnvData = (config: Partial<EnvData>) => ({ ...envData, ...confi
export const newConfigRepositoryMock = (): Mocked<IConfigRepository> => {
return {
getEnv: vitest.fn().mockReturnValue(mockEnvData({})),
getWorker: vitest.fn().mockReturnValue(ImmichWorker.API),
};
};
5 changes: 1 addition & 4 deletions server/test/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,7 @@ export const newTestService = <T extends BaseService>(
Service: Constructor<T, BaseServiceArgs>,
overrides?: Overrides,
) => {
const { metadataRepository, worker: workerOverride } = overrides || {};

const worker = workerOverride || ImmichWorker.API;
const { metadataRepository } = overrides || {};

const accessMock = newAccessRepositoryMock();
const loggerMock = newLoggerRepositoryMock();
Expand Down Expand Up @@ -102,7 +100,6 @@ export const newTestService = <T extends BaseService>(
const viewMock = newViewRepositoryMock();

const sut = new Service(
worker,
loggerMock,
accessMock,
activityMock,
Expand Down

0 comments on commit 5edbb93

Please sign in to comment.