mirror of
https://github.com/immich-app/immich
synced 2025-11-07 17:27:20 +00:00
refactor(server): event emits (#10648)
* refactor(server): event emits * refactor: change default priority to 0
This commit is contained in:
parent
7e99394c70
commit
72bf9439b0
25 changed files with 222 additions and 171 deletions
|
|
@ -13,35 +13,34 @@ import {
|
|||
supportedYearTokens,
|
||||
} from 'src/constants';
|
||||
import { SystemConfigCore } from 'src/cores/system-config.core';
|
||||
import { OnServerEvent } from 'src/decorators';
|
||||
import { EventHandlerOptions, OnServerEvent } from 'src/decorators';
|
||||
import { SystemConfigDto, SystemConfigTemplateStorageOptionDto, mapConfig } from 'src/dtos/system-config.dto';
|
||||
import {
|
||||
ClientEvent,
|
||||
IEventRepository,
|
||||
ServerAsyncEvent,
|
||||
ServerAsyncEventMap,
|
||||
OnEvents,
|
||||
ServerEvent,
|
||||
SystemConfigUpdate,
|
||||
} from 'src/interfaces/event.interface';
|
||||
import { ILoggerRepository } from 'src/interfaces/logger.interface';
|
||||
import { ISearchRepository } from 'src/interfaces/search.interface';
|
||||
import { ISystemMetadataRepository } from 'src/interfaces/system-metadata.interface';
|
||||
|
||||
@Injectable()
|
||||
export class SystemConfigService {
|
||||
export class SystemConfigService implements OnEvents {
|
||||
private core: SystemConfigCore;
|
||||
|
||||
constructor(
|
||||
@Inject(ISystemMetadataRepository) repository: ISystemMetadataRepository,
|
||||
@Inject(IEventRepository) private eventRepository: IEventRepository,
|
||||
@Inject(ILoggerRepository) private logger: ILoggerRepository,
|
||||
@Inject(ISearchRepository) private smartInfoRepository: ISearchRepository,
|
||||
) {
|
||||
this.logger.setContext(SystemConfigService.name);
|
||||
this.core = SystemConfigCore.create(repository, this.logger);
|
||||
this.core.config$.subscribe((config) => this.setLogLevel(config));
|
||||
}
|
||||
|
||||
async init() {
|
||||
@EventHandlerOptions({ priority: -100 })
|
||||
async onBootstrapEvent() {
|
||||
const config = await this.core.getConfig({ withCache: false });
|
||||
this.config$.next(config);
|
||||
}
|
||||
|
|
@ -59,8 +58,7 @@ export class SystemConfigService {
|
|||
return mapConfig(defaults);
|
||||
}
|
||||
|
||||
@OnServerEvent(ServerAsyncEvent.CONFIG_VALIDATE)
|
||||
onValidateConfig({ newConfig, oldConfig }: ServerAsyncEventMap[ServerAsyncEvent.CONFIG_VALIDATE]) {
|
||||
onConfigValidateEvent({ newConfig, oldConfig }: SystemConfigUpdate) {
|
||||
if (!_.isEqual(instanceToPlain(newConfig.logging), oldConfig.logging) && this.getEnvLogLevel()) {
|
||||
throw new Error('Logging cannot be changed while the environment variable IMMICH_LOG_LEVEL is set.');
|
||||
}
|
||||
|
|
@ -74,10 +72,7 @@ export class SystemConfigService {
|
|||
const oldConfig = await this.core.getConfig({ withCache: false });
|
||||
|
||||
try {
|
||||
await this.eventRepository.serverSendAsync(ServerAsyncEvent.CONFIG_VALIDATE, {
|
||||
newConfig: dto,
|
||||
oldConfig,
|
||||
});
|
||||
await this.eventRepository.emit('onConfigValidateEvent', { newConfig: dto, oldConfig });
|
||||
} catch (error) {
|
||||
this.logger.warn(`Unable to save system config due to a validation error: ${error}`);
|
||||
throw new BadRequestException(error instanceof Error ? error.message : error);
|
||||
|
|
@ -85,12 +80,11 @@ export class SystemConfigService {
|
|||
|
||||
const newConfig = await this.core.updateConfig(dto);
|
||||
|
||||
// TODO probably move web socket emits to a separate service
|
||||
this.eventRepository.clientBroadcast(ClientEvent.CONFIG_UPDATE, {});
|
||||
this.eventRepository.serverSend(ServerEvent.CONFIG_UPDATE, null);
|
||||
await this.eventRepository.emit('onConfigUpdateEvent', { newConfig, oldConfig });
|
||||
|
||||
if (oldConfig.machineLearning.clip.modelName !== newConfig.machineLearning.clip.modelName) {
|
||||
await this.smartInfoRepository.init(newConfig.machineLearning.clip.modelName);
|
||||
}
|
||||
return mapConfig(newConfig);
|
||||
}
|
||||
|
||||
|
|
@ -115,7 +109,7 @@ export class SystemConfigService {
|
|||
}
|
||||
|
||||
@OnServerEvent(ServerEvent.CONFIG_UPDATE)
|
||||
async onConfigUpdate() {
|
||||
async onConfigUpdateEvent() {
|
||||
await this.core.refreshConfig();
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue