refactor(server): event names (#12084)

This commit is contained in:
Jason Rasmussen 2024-08-27 18:06:50 -04:00 committed by GitHub
parent aac6a4b052
commit 0be3c4472f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
16 changed files with 55 additions and 54 deletions

View File

@ -62,7 +62,7 @@ export class ApiModule implements OnModuleInit, OnModuleDestroy {
async onModuleInit() {
const items = setupEventHandlers(this.moduleRef);
await this.eventRepository.emit('onBootstrap', 'api');
await this.eventRepository.emit('app.bootstrap', 'api');
this.logger.setContext('EventLoader');
const eventMap = _.groupBy(items, 'event');
@ -74,7 +74,7 @@ export class ApiModule implements OnModuleInit, OnModuleDestroy {
}
async onModuleDestroy() {
await this.eventRepository.emit('onShutdown');
await this.eventRepository.emit('app.shutdown');
}
}
@ -90,11 +90,11 @@ export class MicroservicesModule implements OnModuleInit, OnModuleDestroy {
async onModuleInit() {
setupEventHandlers(this.moduleRef);
await this.eventRepository.emit('onBootstrap', 'microservices');
await this.eventRepository.emit('app.bootstrap', 'microservices');
}
async onModuleDestroy() {
await this.eventRepository.emit('onShutdown');
await this.eventRepository.emit('app.shutdown');
}
}

View File

@ -6,19 +6,19 @@ export const IEventRepository = 'IEventRepository';
type EmitEventMap = {
// app events
onBootstrap: ['api' | 'microservices'];
onShutdown: [];
'app.bootstrap': ['api' | 'microservices'];
'app.shutdown': [];
// config events
onConfigUpdate: [{ newConfig: SystemConfig; oldConfig: SystemConfig }];
onConfigValidate: [{ newConfig: SystemConfig; oldConfig: SystemConfig }];
'config.update': [{ newConfig: SystemConfig; oldConfig: SystemConfig }];
'config.validate': [{ newConfig: SystemConfig; oldConfig: SystemConfig }];
// album events
onAlbumUpdate: [{ id: string; updatedBy: string }];
onAlbumInvite: [{ id: string; userId: string }];
'album.update': [{ id: string; updatedBy: string }];
'album.invite': [{ id: string; userId: string }];
// user events
onUserSignup: [{ notify: boolean; id: string; tempPassword?: string }];
'user.signup': [{ notify: boolean; id: string; tempPassword?: string }];
};
export type EmitEvent = keyof EmitEventMap;

View File

@ -205,7 +205,7 @@ describe(AlbumService.name, () => {
expect(userMock.get).toHaveBeenCalledWith('user-id', {});
expect(accessMock.asset.checkOwnerAccess).toHaveBeenCalledWith(authStub.admin.user.id, new Set(['123']));
expect(eventMock.emit).toHaveBeenCalledWith('onAlbumInvite', {
expect(eventMock.emit).toHaveBeenCalledWith('album.invite', {
id: albumStub.empty.id,
userId: 'user-id',
});
@ -384,7 +384,7 @@ describe(AlbumService.name, () => {
userId: authStub.user2.user.id,
albumId: albumStub.sharedWithAdmin.id,
});
expect(eventMock.emit).toHaveBeenCalledWith('onAlbumInvite', {
expect(eventMock.emit).toHaveBeenCalledWith('album.invite', {
id: albumStub.sharedWithAdmin.id,
userId: userStub.user2.id,
});
@ -572,7 +572,7 @@ describe(AlbumService.name, () => {
albumThumbnailAssetId: 'asset-1',
});
expect(albumMock.addAssetIds).toHaveBeenCalledWith('album-123', ['asset-1', 'asset-2', 'asset-3']);
expect(eventMock.emit).toHaveBeenCalledWith('onAlbumUpdate', {
expect(eventMock.emit).toHaveBeenCalledWith('album.update', {
id: 'album-123',
updatedBy: authStub.admin.user.id,
});
@ -616,7 +616,7 @@ describe(AlbumService.name, () => {
albumThumbnailAssetId: 'asset-1',
});
expect(albumMock.addAssetIds).toHaveBeenCalledWith('album-123', ['asset-1', 'asset-2', 'asset-3']);
expect(eventMock.emit).toHaveBeenCalledWith('onAlbumUpdate', {
expect(eventMock.emit).toHaveBeenCalledWith('album.update', {
id: 'album-123',
updatedBy: authStub.user1.user.id,
});

View File

@ -140,7 +140,7 @@ export class AlbumService {
});
for (const { userId } of albumUsers) {
await this.eventRepository.emit('onAlbumInvite', { id: album.id, userId });
await this.eventRepository.emit('album.invite', { id: album.id, userId });
}
return mapAlbumWithAssets(album);
@ -192,7 +192,7 @@ export class AlbumService {
albumThumbnailAssetId: album.albumThumbnailAssetId ?? firstNewAssetId,
});
await this.eventRepository.emit('onAlbumUpdate', { id, updatedBy: auth.user.id });
await this.eventRepository.emit('album.update', { id, updatedBy: auth.user.id });
}
return results;
@ -240,7 +240,7 @@ export class AlbumService {
}
await this.albumUserRepository.create({ userId: userId, albumId: id, role });
await this.eventRepository.emit('onAlbumInvite', { id, userId });
await this.eventRepository.emit('album.invite', { id, userId });
}
return this.findOrFail(id, { withAssets: true }).then(mapAlbumWithoutAssets);

View File

@ -68,7 +68,7 @@ export class DatabaseService {
this.logger.setContext(DatabaseService.name);
}
@OnEmit({ event: 'onBootstrap', priority: -200 })
@OnEmit({ event: 'app.bootstrap', priority: -200 })
async onBootstrap() {
const version = await this.databaseRepository.getPostgresVersion();
const current = semver.coerce(version);

View File

@ -66,7 +66,7 @@ export class LibraryService {
this.configCore = SystemConfigCore.create(systemMetadataRepository, this.logger);
}
@OnEmit({ event: 'onBootstrap' })
@OnEmit({ event: 'app.bootstrap' })
async onBootstrap() {
const config = await this.configCore.getConfig({ withCache: false });
@ -104,7 +104,8 @@ export class LibraryService {
});
}
onConfigValidate({ newConfig }: ArgOf<'onConfigValidate'>) {
@OnEmit({ event: 'config.validate' })
onConfigValidate({ newConfig }: ArgOf<'config.validate'>) {
const { scan } = newConfig.library;
if (!validateCronExpression(scan.cronExpression)) {
throw new Error(`Invalid cron expression ${scan.cronExpression}`);
@ -189,7 +190,7 @@ export class LibraryService {
}
}
@OnEmit({ event: 'onShutdown' })
@OnEmit({ event: 'app.shutdown' })
async onShutdown() {
await this.unwatchAll();
}

View File

@ -121,8 +121,8 @@ export class MetadataService {
);
}
@OnEmit({ event: 'onBootstrap' })
async onBootstrap(app: ArgOf<'onBootstrap'>) {
@OnEmit({ event: 'app.bootstrap' })
async onBootstrap(app: ArgOf<'app.bootstrap'>) {
if (app !== 'microservices') {
return;
}
@ -130,8 +130,8 @@ export class MetadataService {
await this.init(config);
}
@OnEmit({ event: 'onConfigUpdate' })
async onConfigUpdate({ newConfig }: ArgOf<'onConfigUpdate'>) {
@OnEmit({ event: 'config.update' })
async onConfigUpdate({ newConfig }: ArgOf<'config.update'>) {
await this.init(newConfig);
}
@ -153,7 +153,7 @@ export class MetadataService {
}
}
@OnEmit({ event: 'onShutdown' })
@OnEmit({ event: 'app.shutdown' })
async onShutdown() {
await this.repository.teardown();
}

View File

@ -39,8 +39,8 @@ export class MicroservicesService {
private versionService: VersionService,
) {}
@OnEmit({ event: 'onBootstrap' })
async onBootstrap(app: ArgOf<'onBootstrap'>) {
@OnEmit({ event: 'app.bootstrap' })
async onBootstrap(app: ArgOf<'app.bootstrap'>) {
if (app !== 'microservices') {
return;
}

View File

@ -42,8 +42,8 @@ export class NotificationService {
this.configCore = SystemConfigCore.create(systemMetadataRepository, logger);
}
@OnEmit({ event: 'onConfigValidate', priority: -100 })
async onConfigValidate({ oldConfig, newConfig }: ArgOf<'onConfigValidate'>) {
@OnEmit({ event: 'config.validate', priority: -100 })
async onConfigValidate({ oldConfig, newConfig }: ArgOf<'config.validate'>) {
try {
if (
newConfig.notifications.smtp.enabled &&
@ -57,20 +57,20 @@ export class NotificationService {
}
}
@OnEmit({ event: 'onUserSignup' })
async onUserSignup({ notify, id, tempPassword }: ArgOf<'onUserSignup'>) {
@OnEmit({ event: 'user.signup' })
async onUserSignup({ notify, id, tempPassword }: ArgOf<'user.signup'>) {
if (notify) {
await this.jobRepository.queue({ name: JobName.NOTIFY_SIGNUP, data: { id, tempPassword } });
}
}
@OnEmit({ event: 'onAlbumUpdate' })
async onAlbumUpdate({ id, updatedBy }: ArgOf<'onAlbumUpdate'>) {
@OnEmit({ event: 'album.update' })
async onAlbumUpdate({ id, updatedBy }: ArgOf<'album.update'>) {
await this.jobRepository.queue({ name: JobName.NOTIFY_ALBUM_UPDATE, data: { id, senderId: updatedBy } });
}
@OnEmit({ event: 'onAlbumInvite' })
async onAlbumInvite({ id, userId }: ArgOf<'onAlbumInvite'>) {
@OnEmit({ event: 'album.invite' })
async onAlbumInvite({ id, userId }: ArgOf<'album.invite'>) {
await this.jobRepository.queue({ name: JobName.NOTIFY_ALBUM_INVITE, data: { id, recipientId: userId } });
}

View File

@ -42,7 +42,7 @@ export class ServerService {
this.configCore = SystemConfigCore.create(systemMetadataRepository, this.logger);
}
@OnEmit({ event: 'onBootstrap' })
@OnEmit({ event: 'app.bootstrap' })
async onBootstrap(): Promise<void> {
const featureFlags = await this.getFeatures();
if (featureFlags.configFile) {

View File

@ -39,8 +39,8 @@ export class SmartInfoService {
this.configCore = SystemConfigCore.create(systemMetadataRepository, this.logger);
}
@OnEmit({ event: 'onBootstrap' })
async onBootstrap(app: ArgOf<'onBootstrap'>) {
@OnEmit({ event: 'app.bootstrap' })
async onBootstrap(app: ArgOf<'app.bootstrap'>) {
if (app !== 'microservices') {
return;
}
@ -49,8 +49,8 @@ export class SmartInfoService {
await this.init(config);
}
@OnEmit({ event: 'onConfigValidate' })
onConfigValidate({ newConfig }: ArgOf<'onConfigValidate'>) {
@OnEmit({ event: 'config.validate' })
onConfigValidate({ newConfig }: ArgOf<'config.validate'>) {
try {
getCLIPModelInfo(newConfig.machineLearning.clip.modelName);
} catch {
@ -60,8 +60,8 @@ export class SmartInfoService {
}
}
@OnEmit({ event: 'onConfigUpdate' })
async onConfigUpdate({ oldConfig, newConfig }: ArgOf<'onConfigUpdate'>) {
@OnEmit({ event: 'config.update' })
async onConfigUpdate({ oldConfig, newConfig }: ArgOf<'config.update'>) {
await this.init(newConfig, oldConfig);
}

View File

@ -89,8 +89,8 @@ export class StorageTemplateService {
);
}
@OnEmit({ event: 'onConfigValidate' })
onConfigValidate({ newConfig }: ArgOf<'onConfigValidate'>) {
@OnEmit({ event: 'config.validate' })
onConfigValidate({ newConfig }: ArgOf<'config.validate'>) {
try {
const { compiled } = this.compile(newConfig.storageTemplate.template);
this.render(compiled, {

View File

@ -14,7 +14,7 @@ export class StorageService {
this.logger.setContext(StorageService.name);
}
@OnEmit({ event: 'onBootstrap' })
@OnEmit({ event: 'app.bootstrap' })
onBootstrap() {
const libraryBase = StorageCore.getBaseFolder(StorageFolder.LIBRARY);
this.storageRepository.mkdirSync(libraryBase);

View File

@ -33,7 +33,7 @@ export class SystemConfigService {
this.core.config$.subscribe((config) => this.setLogLevel(config));
}
@OnEmit({ event: 'onBootstrap', priority: -100 })
@OnEmit({ event: 'app.bootstrap', priority: -100 })
async onBootstrap() {
const config = await this.core.getConfig({ withCache: false });
this.core.config$.next(config);
@ -48,8 +48,8 @@ export class SystemConfigService {
return mapConfig(defaults);
}
@OnEmit({ event: 'onConfigValidate' })
onConfigValidate({ newConfig, oldConfig }: ArgOf<'onConfigValidate'>) {
@OnEmit({ event: 'config.validate' })
onConfigValidate({ newConfig, oldConfig }: ArgOf<'config.validate'>) {
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.');
}
@ -63,7 +63,7 @@ export class SystemConfigService {
const oldConfig = await this.core.getConfig({ withCache: false });
try {
await this.eventRepository.emit('onConfigValidate', { newConfig: dto, oldConfig });
await this.eventRepository.emit('config.validate', { 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);
@ -74,7 +74,7 @@ export class SystemConfigService {
// 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('onConfigUpdate', { newConfig, oldConfig });
await this.eventRepository.emit('config.update', { newConfig, oldConfig });
return mapConfig(newConfig);
}

View File

@ -45,7 +45,7 @@ export class UserAdminService {
const { notify, ...rest } = dto;
const user = await this.userCore.createUser(rest);
await this.eventRepository.emit('onUserSignup', {
await this.eventRepository.emit('user.signup', {
notify: !!notify,
id: user.id,
tempPassword: user.shouldChangePassword ? rest.password : undefined,

View File

@ -37,7 +37,7 @@ export class VersionService {
this.configCore = SystemConfigCore.create(systemMetadataRepository, this.logger);
}
@OnEmit({ event: 'onBootstrap' })
@OnEmit({ event: 'app.bootstrap' })
async onBootstrap(): Promise<void> {
await this.handleVersionCheck();
}