From e36b620020095858fd265ee389962334e0b67405 Mon Sep 17 00:00:00 2001 From: Jason Rasmussen Date: Fri, 24 Mar 2023 08:19:48 -0400 Subject: [PATCH] refactor(server): cron jobs (#2067) --- ...dule-tasks.service.ts => app.cron-jobs.ts} | 6 +++--- server/apps/immich/src/app.module.ts | 20 ++++++++----------- .../schedule-tasks/schedule-tasks.module.ts | 10 ---------- .../libs/domain/src/user/user.service.spec.ts | 6 +++--- server/libs/domain/src/user/user.service.ts | 2 +- 5 files changed, 15 insertions(+), 29 deletions(-) rename server/apps/immich/src/{modules/schedule-tasks/schedule-tasks.service.ts => app.cron-jobs.ts} (68%) delete mode 100644 server/apps/immich/src/modules/schedule-tasks/schedule-tasks.module.ts diff --git a/server/apps/immich/src/modules/schedule-tasks/schedule-tasks.service.ts b/server/apps/immich/src/app.cron-jobs.ts similarity index 68% rename from server/apps/immich/src/modules/schedule-tasks/schedule-tasks.service.ts rename to server/apps/immich/src/app.cron-jobs.ts index bd8a7715e7..c05f3e908f 100644 --- a/server/apps/immich/src/modules/schedule-tasks/schedule-tasks.service.ts +++ b/server/apps/immich/src/app.cron-jobs.ts @@ -3,11 +3,11 @@ import { Injectable } from '@nestjs/common'; import { Cron, CronExpression } from '@nestjs/schedule'; @Injectable() -export class ScheduleTasksService { +export class AppCronJobs { constructor(private userService: UserService) {} @Cron(CronExpression.EVERY_DAY_AT_11PM) - async onUserDeleteCheck() { - await this.userService.handleUserDeleteCheck(); + async onQueueUserDeleteCheck() { + await this.userService.handleQueueUserDelete(); } } diff --git a/server/apps/immich/src/app.module.ts b/server/apps/immich/src/app.module.ts index 0334baa374..a59315b5b2 100644 --- a/server/apps/immich/src/app.module.ts +++ b/server/apps/immich/src/app.module.ts @@ -5,7 +5,6 @@ import { ConfigModule } from '@nestjs/config'; import { AlbumModule } from './api-v1/album/album.module'; import { AppController } from './app.controller'; import { ScheduleModule } from '@nestjs/schedule'; -import { ScheduleTasksModule } from './modules/schedule-tasks/schedule-tasks.module'; import { TagModule } from './api-v1/tag/tag.module'; import { DomainModule, SearchService } from '@app/domain'; import { InfraModule } from '@app/infra'; @@ -23,23 +22,15 @@ import { } from './controllers'; import { APP_GUARD } from '@nestjs/core'; import { AuthGuard } from './middlewares/auth.guard'; +import { AppCronJobs } from './app.cron-jobs'; @Module({ imports: [ ConfigModule.forRoot(immichAppConfig), - - DomainModule.register({ - imports: [InfraModule], - }), - + DomainModule.register({ imports: [InfraModule] }), AssetModule, - AlbumModule, - ScheduleModule.forRoot(), - - ScheduleTasksModule, - TagModule, ], controllers: [ @@ -55,7 +46,12 @@ import { AuthGuard } from './middlewares/auth.guard'; SystemConfigController, UserController, ], - providers: [{ provide: APP_GUARD, useExisting: AuthGuard }, AuthGuard], + providers: [ + // + { provide: APP_GUARD, useExisting: AuthGuard }, + AuthGuard, + AppCronJobs, + ], }) export class AppModule implements OnModuleInit { constructor(private searchService: SearchService) {} diff --git a/server/apps/immich/src/modules/schedule-tasks/schedule-tasks.module.ts b/server/apps/immich/src/modules/schedule-tasks/schedule-tasks.module.ts deleted file mode 100644 index 9d511c6192..0000000000 --- a/server/apps/immich/src/modules/schedule-tasks/schedule-tasks.module.ts +++ /dev/null @@ -1,10 +0,0 @@ -import { Module } from '@nestjs/common'; -import { TypeOrmModule } from '@nestjs/typeorm'; -import { AssetEntity, ExifEntity, UserEntity } from '@app/infra'; -import { ScheduleTasksService } from './schedule-tasks.service'; - -@Module({ - imports: [TypeOrmModule.forFeature([AssetEntity, ExifEntity, UserEntity])], - providers: [ScheduleTasksService], -}) -export class ScheduleTasksModule {} diff --git a/server/libs/domain/src/user/user.service.spec.ts b/server/libs/domain/src/user/user.service.spec.ts index e169d87eca..5973b4da07 100644 --- a/server/libs/domain/src/user/user.service.spec.ts +++ b/server/libs/domain/src/user/user.service.spec.ts @@ -426,7 +426,7 @@ describe(UserService.name, () => { }); }); - describe('handleUserDeleteCheck', () => { + describe('handleQueueUserDelete', () => { it('should skip users not ready for deletion', async () => { userRepositoryMock.getDeletedUsers.mockResolvedValue([ {}, @@ -435,7 +435,7 @@ describe(UserService.name, () => { { deletedAt: makeDeletedAt(5) }, ] as UserEntity[]); - await sut.handleUserDeleteCheck(); + await sut.handleQueueUserDelete(); expect(userRepositoryMock.getDeletedUsers).toHaveBeenCalled(); expect(jobMock.queue).not.toHaveBeenCalled(); @@ -445,7 +445,7 @@ describe(UserService.name, () => { const user = { deletedAt: makeDeletedAt(10) }; userRepositoryMock.getDeletedUsers.mockResolvedValue([user] as UserEntity[]); - await sut.handleUserDeleteCheck(); + await sut.handleQueueUserDelete(); expect(userRepositoryMock.getDeletedUsers).toHaveBeenCalled(); expect(jobMock.queue).toHaveBeenCalledWith({ name: JobName.USER_DELETION, data: { user } }); diff --git a/server/libs/domain/src/user/user.service.ts b/server/libs/domain/src/user/user.service.ts index 741741cae1..acb1c87f18 100644 --- a/server/libs/domain/src/user/user.service.ts +++ b/server/libs/domain/src/user/user.service.ts @@ -141,7 +141,7 @@ export class UserService { return { admin, password, provided: !!providedPassword }; } - async handleUserDeleteCheck() { + async handleQueueUserDelete() { const users = await this.userRepository.getDeletedUsers(); for (const user of users) { if (this.isReadyForDeletion(user)) {