add crone to delete old pagers
This commit is contained in:
@@ -0,0 +1,46 @@
|
|||||||
|
import { Injectable, Logger } from '@nestjs/common';
|
||||||
|
import { Cron } from '@nestjs/schedule';
|
||||||
|
import { EntityManager } from '@mikro-orm/postgresql';
|
||||||
|
import { Pager } from '../entities/pager.entity';
|
||||||
|
|
||||||
|
@Injectable()
|
||||||
|
export class PagerCrone {
|
||||||
|
private readonly logger = new Logger(PagerCrone.name);
|
||||||
|
|
||||||
|
constructor(private readonly em: EntityManager) {}
|
||||||
|
|
||||||
|
// run every day at 03:00 (3:00 AM)
|
||||||
|
@Cron('0 3 * * *', {
|
||||||
|
name: 'deleteOldPagers',
|
||||||
|
timeZone: 'UTC',
|
||||||
|
})
|
||||||
|
async handleCron() {
|
||||||
|
try {
|
||||||
|
this.logger.debug('Starting daily pager cleanup (removing pagers older than 24 hours)');
|
||||||
|
|
||||||
|
// Calculate the date 24 hours ago
|
||||||
|
const twentyFourHoursAgo = new Date();
|
||||||
|
twentyFourHoursAgo.setHours(twentyFourHoursAgo.getHours() - 24);
|
||||||
|
|
||||||
|
// Find all notifications created more than 24 hours ago
|
||||||
|
const oldPagers = await this.em.find(Pager, {
|
||||||
|
createdAt: { $lt: twentyFourHoursAgo },
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!oldPagers || oldPagers.length === 0) {
|
||||||
|
this.logger.debug('No old pagers found to delete');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
this.logger.log(`Found ${oldPagers.length} pagers older than 24 hours to delete`);
|
||||||
|
|
||||||
|
// Delete the old notifications
|
||||||
|
await this.em.removeAndFlush(oldPagers);
|
||||||
|
|
||||||
|
this.logger.log(`Successfully deleted ${oldPagers.length} old pagers`);
|
||||||
|
} catch (err) {
|
||||||
|
this.logger.error(`pagers failed: ${err?.message}`, err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@@ -8,10 +8,11 @@ import { RolesModule } from '../roles/roles.module';
|
|||||||
import { PagerListeners } from './listeners/notification.listeners';
|
import { PagerListeners } from './listeners/notification.listeners';
|
||||||
import { AdminModule } from '../admin/admin.module';
|
import { AdminModule } from '../admin/admin.module';
|
||||||
import { NotificationsModule } from '../notifications/notifications.module';
|
import { NotificationsModule } from '../notifications/notifications.module';
|
||||||
|
import { PagerCrone } from './crone/pager.crone';
|
||||||
|
|
||||||
@Module({
|
@Module({
|
||||||
imports: [MikroOrmModule.forFeature([Pager]), JwtModule, RolesModule, AdminModule, NotificationsModule],
|
imports: [MikroOrmModule.forFeature([Pager]), JwtModule, RolesModule, AdminModule, NotificationsModule],
|
||||||
controllers: [PagerController],
|
controllers: [PagerController],
|
||||||
providers: [PagerService, PagerListeners],
|
providers: [PagerService, PagerListeners,PagerCrone],
|
||||||
})
|
})
|
||||||
export class PagerModule {}
|
export class PagerModule {}
|
||||||
|
|||||||
Reference in New Issue
Block a user