Skip to content
This repository has been archived by the owner on Apr 19, 2023. It is now read-only.

Commit

Permalink
✨ Update metrics, verify domains, delete audit logs in tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
AnandChowdhary committed Jan 9, 2021
1 parent 66e63e5 commit 533f690
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 18 deletions.
12 changes: 11 additions & 1 deletion src/providers/tasks/tasks.module.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
import { Module } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config';
import { DomainsModule } from '../../modules/domains/domains.module';
import { ElasticSearchModule } from '../elasticsearch/elasticsearch.module';
import { PrismaModule } from '../prisma/prisma.module';
import { TasksService } from './tasks.service';
import { UsersModule } from '../../modules/users/users.module';
import { MetricsModule } from '../../modules/metrics/metrics.module';

@Module({
imports: [ConfigModule, PrismaModule, ElasticSearchModule],
imports: [
ConfigModule,
PrismaModule,
ElasticSearchModule,
DomainsModule,
UsersModule,
MetricsModule,
],
providers: [TasksService],
exports: [TasksService],
})
Expand Down
70 changes: 53 additions & 17 deletions src/providers/tasks/tasks.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,36 @@ import { Injectable, Logger } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { Cron, CronExpression } from '@nestjs/schedule';
import { Configuration } from '../../config/configuration.interface';
import { DomainsService } from '../../modules/domains/domains.service';
import { MetricsService } from '../../modules/metrics/metrics.service';
import { UsersService } from '../../modules/users/users.service';
import { ElasticSearchService } from '../elasticsearch/elasticsearch.service';
import { PrismaService } from '../prisma/prisma.service';

@Injectable()
export class TasksService {
private trackingConfig = this.configService.get<Configuration['tracking']>(
'tracking',
);
private securityConfig = this.configService.get<Configuration['security']>(
'security',
);

constructor(
private prisma: PrismaService,
private configService: ConfigService,
private elasticSearchService: ElasticSearchService,
private domainsService: DomainsService,
private usersService: UsersService,
private metricsService: MetricsService,
) {}
private readonly logger = new Logger(TasksService.name);

@Cron(CronExpression.EVERY_MINUTE)
async updateMetrics() {
await this.metricsService.updateProcessMetrics();
}

@Cron(CronExpression.EVERY_DAY_AT_1PM)
async deleteOldSessions() {
const now = new Date();
now.setDate(
now.getDate() - this.securityConfig.unusedRefreshTokenExpiryDays,
);
const unusedRefreshTokenExpiryDays =
this.configService.get<number>('security.unusedRefreshTokenExpiryDays') ??
30;
now.setDate(now.getDate() - unusedRefreshTokenExpiryDays);
const deleted = await this.prisma.session.deleteMany({
where: { updatedAt: { lte: now } },
});
Expand All @@ -37,23 +42,54 @@ export class TasksService {
@Cron(CronExpression.EVERY_DAY_AT_2PM)
async deleteInactiveUsers() {
const now = new Date();
now.setDate(now.getDate() - this.securityConfig.inactiveUserDeleteDays);
const deleted = await this.prisma.user.deleteMany({
const inactiveUserDeleteDays =
this.configService.get<number>('security.inactiveUserDeleteDays') ?? 30;
now.setDate(now.getDate() - inactiveUserDeleteDays);
const deleted = await this.prisma.user.findMany({
select: { id: true },
where: {
active: false,
sessions: { every: { updatedAt: { lte: now } } },
},
});
if (deleted.count)
this.logger.debug(`Deleted ${deleted.count} inactive users`);
if (deleted.length) {
for await (const user of deleted)
await this.usersService.deleteUser(user.id);
this.logger.debug(`Deleted ${deleted.length} inactive users`);
}
}

@Cron(CronExpression.EVERY_DAY_AT_3PM)
async deleteOldLogs() {
if (this.trackingConfig.deleteOldLogs)
const tracking = this.configService.get<Configuration['tracking']>(
'tracking',
);
if (tracking.deleteOldLogs)
return this.elasticSearchService.deleteOldRecords(
this.trackingConfig.index,
this.trackingConfig.deleteOldLogsDays,
tracking.index,
tracking.deleteOldLogsDays,
);
}

@Cron(CronExpression.EVERY_DAY_AT_5PM)
async verifyDomains() {
const domains = await this.prisma.domain.findMany({
where: { isVerified: false },
});
for await (const domain of domains) {
try {
await this.domainsService.verifyDomain(domain.groupId, domain.id);
} catch (error) {}
}
}

@Cron(CronExpression.EVERY_DAY_AT_6PM)
async deleteOldAuditLogs() {
const now = new Date();
now.setDate(now.getDate() - 90);
const deleted = await this.prisma.auditLog.deleteMany({
where: { createdAt: { lte: now } },
});
if (deleted.count) this.logger.debug(`Deleted ${deleted.count} audit logs`);
}
}

0 comments on commit 533f690

Please sign in to comment.