1
0
mirror of https://github.com/immich-app/immich.git synced 2024-11-28 09:33:27 +02:00

refactor(server): cron jobs (#2067)

This commit is contained in:
Jason Rasmussen 2023-03-24 08:19:48 -04:00 committed by GitHub
parent 1efc74dabc
commit e36b620020
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 15 additions and 29 deletions

View File

@ -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();
}
}

View File

@ -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) {}

View File

@ -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 {}

View File

@ -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 } });

View File

@ -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)) {