src/cron/cron.service.ts
Properties |
|
Methods |
|
constructor(userService: UsersService, utilService: UtilsService, codesService: CodesService)
|
||||||||||||
|
Defined in src/cron/cron.service.ts:9
|
||||||||||||
|
Parameters :
|
| Public Async checkBlocks |
checkBlocks()
|
Decorators :
@Cron(CronExpression.EVERY_30_SECONDS)
|
|
Defined in src/cron/cron.service.ts:16
|
|
Returns :
any
|
| Public Async removeExpiredAndUsedCodes |
removeExpiredAndUsedCodes()
|
Decorators :
@Cron(CronExpression.EVERY_2_HOURS)
|
|
Defined in src/cron/cron.service.ts:25
|
|
Returns :
any
|
| Private Readonly logger |
Default value : new Logger(CronService.name)
|
|
Defined in src/cron/cron.service.ts:9
|
import { Injectable, Logger } from '@nestjs/common';
import { UsersService } from '../users/users.service';
import { Cron, CronExpression } from '@nestjs/schedule';
import { UtilsService } from '../utils/utils.service';
import { CodesService } from '../codes/codes.service';
@Injectable()
export class CronService {
private readonly logger = new Logger(CronService.name);
constructor(
private readonly userService: UsersService,
private readonly utilService: UtilsService,
private readonly codesService: CodesService,
) {}
@Cron(CronExpression.EVERY_30_SECONDS)
public async checkBlocks() {
const blocks = await this.userService.getActiveBlocks();
for (const block of blocks) {
if (this.utilService.didTimeStampExpire(parseInt(block.blocked_until))) {
await this.userService.unblockUser(block.snowflake);
}
}
}
@Cron(CronExpression.EVERY_2_HOURS)
public async removeExpiredAndUsedCodes() {
const codes = await this.codesService.getUsedCodes();
const pool = await this.codesService.getCodePool();
const pArray: Promise<void>[] = [];
codes.forEach((code) => {
if (this.utilService.didTimeStampExpire(parseInt(code.expiration))) {
pArray.push(this.codesService.delete(code.code));
}
});
await Promise.all(pArray);
const poolAfterClean = await this.codesService.getCodePool();
this.logger.log(`Code pool changed ${pool} => ${poolAfterClean}`);
}
}