mirror of
https://github.com/immich-app/immich.git
synced 2024-12-01 09:51:16 +02:00
refactor(server): cron jobs (#2067)
This commit is contained in:
parent
1efc74dabc
commit
e36b620020
@ -3,11 +3,11 @@ import { Injectable } from '@nestjs/common';
|
|||||||
import { Cron, CronExpression } from '@nestjs/schedule';
|
import { Cron, CronExpression } from '@nestjs/schedule';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class ScheduleTasksService {
|
export class AppCronJobs {
|
||||||
constructor(private userService: UserService) {}
|
constructor(private userService: UserService) {}
|
||||||
|
|
||||||
@Cron(CronExpression.EVERY_DAY_AT_11PM)
|
@Cron(CronExpression.EVERY_DAY_AT_11PM)
|
||||||
async onUserDeleteCheck() {
|
async onQueueUserDeleteCheck() {
|
||||||
await this.userService.handleUserDeleteCheck();
|
await this.userService.handleQueueUserDelete();
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -5,7 +5,6 @@ import { ConfigModule } from '@nestjs/config';
|
|||||||
import { AlbumModule } from './api-v1/album/album.module';
|
import { AlbumModule } from './api-v1/album/album.module';
|
||||||
import { AppController } from './app.controller';
|
import { AppController } from './app.controller';
|
||||||
import { ScheduleModule } from '@nestjs/schedule';
|
import { ScheduleModule } from '@nestjs/schedule';
|
||||||
import { ScheduleTasksModule } from './modules/schedule-tasks/schedule-tasks.module';
|
|
||||||
import { TagModule } from './api-v1/tag/tag.module';
|
import { TagModule } from './api-v1/tag/tag.module';
|
||||||
import { DomainModule, SearchService } from '@app/domain';
|
import { DomainModule, SearchService } from '@app/domain';
|
||||||
import { InfraModule } from '@app/infra';
|
import { InfraModule } from '@app/infra';
|
||||||
@ -23,23 +22,15 @@ import {
|
|||||||
} from './controllers';
|
} from './controllers';
|
||||||
import { APP_GUARD } from '@nestjs/core';
|
import { APP_GUARD } from '@nestjs/core';
|
||||||
import { AuthGuard } from './middlewares/auth.guard';
|
import { AuthGuard } from './middlewares/auth.guard';
|
||||||
|
import { AppCronJobs } from './app.cron-jobs';
|
||||||
|
|
||||||
@Module({
|
@Module({
|
||||||
imports: [
|
imports: [
|
||||||
ConfigModule.forRoot(immichAppConfig),
|
ConfigModule.forRoot(immichAppConfig),
|
||||||
|
DomainModule.register({ imports: [InfraModule] }),
|
||||||
DomainModule.register({
|
|
||||||
imports: [InfraModule],
|
|
||||||
}),
|
|
||||||
|
|
||||||
AssetModule,
|
AssetModule,
|
||||||
|
|
||||||
AlbumModule,
|
AlbumModule,
|
||||||
|
|
||||||
ScheduleModule.forRoot(),
|
ScheduleModule.forRoot(),
|
||||||
|
|
||||||
ScheduleTasksModule,
|
|
||||||
|
|
||||||
TagModule,
|
TagModule,
|
||||||
],
|
],
|
||||||
controllers: [
|
controllers: [
|
||||||
@ -55,7 +46,12 @@ import { AuthGuard } from './middlewares/auth.guard';
|
|||||||
SystemConfigController,
|
SystemConfigController,
|
||||||
UserController,
|
UserController,
|
||||||
],
|
],
|
||||||
providers: [{ provide: APP_GUARD, useExisting: AuthGuard }, AuthGuard],
|
providers: [
|
||||||
|
//
|
||||||
|
{ provide: APP_GUARD, useExisting: AuthGuard },
|
||||||
|
AuthGuard,
|
||||||
|
AppCronJobs,
|
||||||
|
],
|
||||||
})
|
})
|
||||||
export class AppModule implements OnModuleInit {
|
export class AppModule implements OnModuleInit {
|
||||||
constructor(private searchService: SearchService) {}
|
constructor(private searchService: SearchService) {}
|
||||||
|
@ -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 {}
|
|
@ -426,7 +426,7 @@ describe(UserService.name, () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('handleUserDeleteCheck', () => {
|
describe('handleQueueUserDelete', () => {
|
||||||
it('should skip users not ready for deletion', async () => {
|
it('should skip users not ready for deletion', async () => {
|
||||||
userRepositoryMock.getDeletedUsers.mockResolvedValue([
|
userRepositoryMock.getDeletedUsers.mockResolvedValue([
|
||||||
{},
|
{},
|
||||||
@ -435,7 +435,7 @@ describe(UserService.name, () => {
|
|||||||
{ deletedAt: makeDeletedAt(5) },
|
{ deletedAt: makeDeletedAt(5) },
|
||||||
] as UserEntity[]);
|
] as UserEntity[]);
|
||||||
|
|
||||||
await sut.handleUserDeleteCheck();
|
await sut.handleQueueUserDelete();
|
||||||
|
|
||||||
expect(userRepositoryMock.getDeletedUsers).toHaveBeenCalled();
|
expect(userRepositoryMock.getDeletedUsers).toHaveBeenCalled();
|
||||||
expect(jobMock.queue).not.toHaveBeenCalled();
|
expect(jobMock.queue).not.toHaveBeenCalled();
|
||||||
@ -445,7 +445,7 @@ describe(UserService.name, () => {
|
|||||||
const user = { deletedAt: makeDeletedAt(10) };
|
const user = { deletedAt: makeDeletedAt(10) };
|
||||||
userRepositoryMock.getDeletedUsers.mockResolvedValue([user] as UserEntity[]);
|
userRepositoryMock.getDeletedUsers.mockResolvedValue([user] as UserEntity[]);
|
||||||
|
|
||||||
await sut.handleUserDeleteCheck();
|
await sut.handleQueueUserDelete();
|
||||||
|
|
||||||
expect(userRepositoryMock.getDeletedUsers).toHaveBeenCalled();
|
expect(userRepositoryMock.getDeletedUsers).toHaveBeenCalled();
|
||||||
expect(jobMock.queue).toHaveBeenCalledWith({ name: JobName.USER_DELETION, data: { user } });
|
expect(jobMock.queue).toHaveBeenCalledWith({ name: JobName.USER_DELETION, data: { user } });
|
||||||
|
@ -141,7 +141,7 @@ export class UserService {
|
|||||||
return { admin, password, provided: !!providedPassword };
|
return { admin, password, provided: !!providedPassword };
|
||||||
}
|
}
|
||||||
|
|
||||||
async handleUserDeleteCheck() {
|
async handleQueueUserDelete() {
|
||||||
const users = await this.userRepository.getDeletedUsers();
|
const users = await this.userRepository.getDeletedUsers();
|
||||||
for (const user of users) {
|
for (const user of users) {
|
||||||
if (this.isReadyForDeletion(user)) {
|
if (this.isReadyForDeletion(user)) {
|
||||||
|
Loading…
Reference in New Issue
Block a user