hippofish/packages/backend/src/queue/processors/CleanProcessorService.ts

75 lines
2.2 KiB
TypeScript
Raw Normal View History

/*
* SPDX-FileCopyrightText: syuilo and misskey-project
* SPDX-License-Identifier: AGPL-3.0-only
*/
2022-09-17 20:27:08 +02:00
import { Inject, Injectable } from '@nestjs/common';
import { In, LessThan } from 'typeorm';
2022-09-17 20:27:08 +02:00
import { DI } from '@/di-symbols.js';
import type { AntennasRepository, RoleAssignmentsRepository, UserIpsRepository } from '@/models/_.js';
2022-09-17 20:27:08 +02:00
import type Logger from '@/logger.js';
import { bindThis } from '@/decorators.js';
import { IdService } from '@/core/IdService.js';
import type { Config } from '@/config.js';
import { ReversiService } from '@/core/ReversiService.js';
2022-09-17 20:27:08 +02:00
import { QueueLoggerService } from '../QueueLoggerService.js';
import type * as Bull from 'bullmq';
2022-09-17 20:27:08 +02:00
@Injectable()
export class CleanProcessorService {
2022-09-18 20:11:50 +02:00
private logger: Logger;
2022-09-17 20:27:08 +02:00
constructor(
@Inject(DI.config)
private config: Config,
2022-09-17 20:27:08 +02:00
@Inject(DI.userIpsRepository)
private userIpsRepository: UserIpsRepository,
@Inject(DI.antennasRepository)
private antennasRepository: AntennasRepository,
@Inject(DI.roleAssignmentsRepository)
private roleAssignmentsRepository: RoleAssignmentsRepository,
2022-09-17 20:27:08 +02:00
private queueLoggerService: QueueLoggerService,
private reversiService: ReversiService,
private idService: IdService,
2022-09-17 20:27:08 +02:00
) {
2022-09-18 20:11:50 +02:00
this.logger = this.queueLoggerService.logger.createSubLogger('clean');
2022-09-17 20:27:08 +02:00
}
@bindThis
public async process(): Promise<void> {
2022-09-18 20:11:50 +02:00
this.logger.info('Cleaning...');
2022-09-17 20:27:08 +02:00
this.userIpsRepository.delete({
createdAt: LessThan(new Date(Date.now() - (1000 * 60 * 60 * 24 * 90))),
});
// 使われてないアンテナを停止
if (this.config.deactivateAntennaThreshold > 0) {
this.antennasRepository.update({
lastUsedAt: LessThan(new Date(Date.now() - this.config.deactivateAntennaThreshold)),
}, {
isActive: false,
});
}
const expiredRoleAssignments = await this.roleAssignmentsRepository.createQueryBuilder('assign')
.where('assign.expiresAt IS NOT NULL')
.andWhere('assign.expiresAt < :now', { now: new Date() })
.getMany();
if (expiredRoleAssignments.length > 0) {
await this.roleAssignmentsRepository.delete({
id: In(expiredRoleAssignments.map(x => x.id)),
});
}
this.reversiService.cleanOutdatedGames();
2022-09-18 20:11:50 +02:00
this.logger.succ('Cleaned.');
2022-09-17 20:27:08 +02:00
}
}