refactor: server emit events (#11780)

This commit is contained in:
Jason Rasmussen 2024-08-15 16:12:41 -04:00 committed by GitHub
parent 32c05ea950
commit 433c7ab01d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
27 changed files with 222 additions and 182 deletions

View file

@ -6,6 +6,7 @@ import path, { basename, parse } from 'node:path';
import picomatch from 'picomatch';
import { StorageCore } from 'src/cores/storage.core';
import { SystemConfigCore } from 'src/cores/system-config.core';
import { OnEmit } from 'src/decorators';
import {
CreateLibraryDto,
LibraryResponseDto,
@ -22,7 +23,7 @@ import { AssetType } from 'src/enum';
import { IAssetRepository, WithProperty } from 'src/interfaces/asset.interface';
import { ICryptoRepository } from 'src/interfaces/crypto.interface';
import { DatabaseLock, IDatabaseRepository } from 'src/interfaces/database.interface';
import { OnEvents, SystemConfigUpdateEvent } from 'src/interfaces/event.interface';
import { ArgOf } from 'src/interfaces/event.interface';
import {
IBaseJob,
IEntityJob,
@ -45,7 +46,7 @@ import { validateCronExpression } from 'src/validation';
const LIBRARY_SCAN_BATCH_SIZE = 5000;
@Injectable()
export class LibraryService implements OnEvents {
export class LibraryService {
private configCore: SystemConfigCore;
private watchLibraries = false;
private watchLock = false;
@ -65,7 +66,8 @@ export class LibraryService implements OnEvents {
this.configCore = SystemConfigCore.create(systemMetadataRepository, this.logger);
}
async onBootstrapEvent() {
@OnEmit({ event: 'onBootstrap' })
async onBootstrap() {
const config = await this.configCore.getConfig({ withCache: false });
const { watch, scan } = config.library;
@ -102,7 +104,7 @@ export class LibraryService implements OnEvents {
});
}
onConfigValidateEvent({ newConfig }: SystemConfigUpdateEvent) {
onConfigValidate({ newConfig }: ArgOf<'onConfigValidate'>) {
const { scan } = newConfig.library;
if (!validateCronExpression(scan.cronExpression)) {
throw new Error(`Invalid cron expression ${scan.cronExpression}`);
@ -187,7 +189,8 @@ export class LibraryService implements OnEvents {
}
}
async onShutdownEvent() {
@OnEmit({ event: 'onShutdown' })
async onShutdown() {
await this.unwatchAll();
}