refactor(server): simplify config init process (#5702)

This commit is contained in:
Jason Rasmussen 2024-01-01 13:16:44 -05:00 committed by GitHub
parent 5f6d09d3da
commit 03eb5ffc5c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 35 additions and 35 deletions

View File

@ -19,7 +19,7 @@ import { SharedLinkService } from './shared-link';
import { SmartInfoService } from './smart-info';
import { StorageService } from './storage';
import { StorageTemplateService } from './storage-template';
import { INITIAL_SYSTEM_CONFIG, SystemConfigService } from './system-config';
import { SystemConfigService } from './system-config';
import { TagService } from './tag';
import { UserService } from './user';
@ -47,14 +47,6 @@ const providers: Provider[] = [
TagService,
UserService,
ImmichLogger,
{
provide: INITIAL_SYSTEM_CONFIG,
inject: [SystemConfigService, DatabaseService],
useFactory: async (configService: SystemConfigService, databaseService: DatabaseService) => {
await databaseService.init();
return configService.getConfig();
},
},
];
@Global()

View File

@ -207,15 +207,15 @@ describe(JobService.name, () => {
});
});
describe('registerHandlers', () => {
describe('init', () => {
it('should register a handler for each queue', async () => {
await sut.registerHandlers(makeMockHandlers(true));
await sut.init(makeMockHandlers(true));
expect(configMock.load).toHaveBeenCalled();
expect(jobMock.addHandler).toHaveBeenCalledTimes(Object.keys(QueueName).length);
});
it('should subscribe to config changes', async () => {
await sut.registerHandlers(makeMockHandlers(false));
await sut.init(makeMockHandlers(false));
SystemConfigCore.create(newSystemConfigRepositoryMock(false)).config$.next({
job: {
@ -323,7 +323,7 @@ describe(JobService.name, () => {
assetMock.getByIds.mockResolvedValue([]);
}
await sut.registerHandlers(makeMockHandlers(true));
await sut.init(makeMockHandlers(true));
await jobMock.addHandler.mock.calls[0][2](item);
await asyncTick(3);
@ -334,7 +334,7 @@ describe(JobService.name, () => {
});
it(`should not queue any jobs when ${item.name} finishes with 'false'`, async () => {
await sut.registerHandlers(makeMockHandlers(false));
await sut.init(makeMockHandlers(false));
await jobMock.addHandler.mock.calls[0][2](item);
await asyncTick(3);

View File

@ -120,7 +120,7 @@ export class JobService {
}
}
async registerHandlers(jobHandlers: Record<JobName, JobHandler>) {
async init(jobHandlers: Record<JobName, JobHandler>) {
const config = await this.configCore.getConfig();
for (const queueName of Object.values(QueueName)) {
let concurrency = 1;

View File

@ -27,6 +27,7 @@ import {
} from '@test';
import { when } from 'jest-when';
import { Stats } from 'node:fs';
import { SystemConfigCore } from '../system-config';
describe(StorageTemplateService.name, () => {
let sut: StorageTemplateService;
@ -38,7 +39,7 @@ describe(StorageTemplateService.name, () => {
let storageMock: jest.Mocked<IStorageRepository>;
let userMock: jest.Mocked<IUserRepository>;
let cryptoMock: jest.Mocked<ICryptoRepository>;
let databaseRepository: jest.Mocked<IDatabaseRepository>;
let databaseMock: jest.Mocked<IDatabaseRepository>;
it('should work', () => {
expect(sut).toBeDefined();
@ -53,22 +54,23 @@ describe(StorageTemplateService.name, () => {
storageMock = newStorageRepositoryMock();
userMock = newUserRepositoryMock();
cryptoMock = newCryptoRepositoryMock();
databaseRepository = newDatabaseRepositoryMock();
databaseMock = newDatabaseRepositoryMock();
configMock.load.mockResolvedValue([{ key: SystemConfigKey.STORAGE_TEMPLATE_ENABLED, value: true }]);
sut = new StorageTemplateService(
albumMock,
assetMock,
configMock,
defaults,
moveMock,
personMock,
storageMock,
userMock,
cryptoMock,
databaseRepository,
databaseMock,
);
configMock.load.mockResolvedValue([{ key: SystemConfigKey.STORAGE_TEMPLATE_ENABLED, value: true }]);
SystemConfigCore.create(configMock).config$.next(defaults);
});
describe('handleMigrationSingle', () => {

View File

@ -21,7 +21,6 @@ import {
} from '../repositories';
import { StorageCore, StorageFolder } from '../storage';
import {
INITIAL_SYSTEM_CONFIG,
supportedDayTokens,
supportedHourTokens,
supportedMinuteTokens,
@ -49,32 +48,33 @@ export class StorageTemplateService {
private logger = new ImmichLogger(StorageTemplateService.name);
private configCore: SystemConfigCore;
private storageCore: StorageCore;
private template: {
private _template: {
compiled: HandlebarsTemplateDelegate<any>;
raw: string;
needsAlbum: boolean;
};
} | null = null;
private get template() {
if (!this._template) {
throw new Error('Template not initialized');
}
return this._template;
}
constructor(
@Inject(IAlbumRepository) private albumRepository: IAlbumRepository,
@Inject(IAssetRepository) private assetRepository: IAssetRepository,
@Inject(ISystemConfigRepository) configRepository: ISystemConfigRepository,
@Inject(INITIAL_SYSTEM_CONFIG) config: SystemConfig,
@Inject(IMoveRepository) moveRepository: IMoveRepository,
@Inject(IPersonRepository) personRepository: IPersonRepository,
@Inject(IStorageRepository) private storageRepository: IStorageRepository,
@Inject(IUserRepository) private userRepository: IUserRepository,
@Inject(ICryptoRepository) private cryptoRepository: ICryptoRepository,
@Inject(ICryptoRepository) cryptoRepository: ICryptoRepository,
@Inject(IDatabaseRepository) private databaseRepository: IDatabaseRepository,
) {
this.template = this.compile(config.storageTemplate.template);
this.configCore = SystemConfigCore.create(configRepository);
this.configCore.addValidator((config) => this.validate(config));
this.configCore.config$.subscribe((config) => {
const template = config.storageTemplate.template;
this.logger.debug(`Received config, compiling storage template: ${template}`);
this.template = this.compile(template);
});
this.configCore.config$.subscribe((config) => this.onConfig(config));
this.storageCore = StorageCore.create(
assetRepository,
moveRepository,
@ -270,6 +270,14 @@ export class StorageTemplateService {
}
}
private onConfig(config: SystemConfig) {
const template = config.storageTemplate.template;
if (!this._template || template !== this.template.raw) {
this.logger.debug(`Compiling new storage template: ${template}`);
this._template = this.compile(template);
}
}
private compile(template: string) {
return {
raw: template,

View File

@ -25,5 +25,3 @@ export const supportedPresetTokens = [
'{{y}}/{{y}}-{{WW}}/{{assetId}}',
'{{album}}/{{filename}}',
];
export const INITIAL_SYSTEM_CONFIG = 'INITIAL_SYSTEM_CONFIG';

View File

@ -42,7 +42,7 @@ export class SystemConfigService {
async init() {
const config = await this.core.getConfig();
await this.setLogLevel(config);
this.config$.next(config);
}
get config$() {

View File

@ -38,7 +38,7 @@ export class AppService {
async init() {
await this.databaseService.init();
await this.configService.init();
await this.jobService.registerHandlers({
await this.jobService.init({
[JobName.ASSET_DELETION]: (data) => this.assetService.handleAssetDeletion(data),
[JobName.ASSET_DELETION_CHECK]: () => this.assetService.handleAssetDeletionCheck(),
[JobName.DELETE_FILES]: (data: IDeleteFilesJob) => this.storageService.handleDeleteFiles(data),