From 8456f04e15f27a9ec85fbc2aaf8daaf408de958b Mon Sep 17 00:00:00 2001 From: Akhil Meka Date: Wed, 12 Mar 2025 17:27:42 +0530 Subject: [PATCH] add notification cleanup script --- cron/archive.js | 4 ---- cron/notificationCleanup.js | 26 ++++++++++++++++++++++++++ 2 files changed, 26 insertions(+), 4 deletions(-) create mode 100644 cron/notificationCleanup.js diff --git a/cron/archive.js b/cron/archive.js index e44b0fa..40b8b03 100644 --- a/cron/archive.js +++ b/cron/archive.js @@ -1,5 +1,4 @@ import mongoose from "mongoose"; -import { setTimeout } from "timers/promises"; const permitsModel = mongoose.model( "permit", @@ -181,7 +180,4 @@ const processedModel = mongoose.model( console.log(`${count} permits archived`); await mongoose.connection.close(); - - console.log("Archiving complete. Going to sleep..."); - await setTimeout(3600 * 25 * 1000); })().catch((err) => console.log(err)); diff --git a/cron/notificationCleanup.js b/cron/notificationCleanup.js new file mode 100644 index 0000000..9c5a5b7 --- /dev/null +++ b/cron/notificationCleanup.js @@ -0,0 +1,26 @@ +import mongoose from "mongoose"; + +const notifModel = mongoose.model( + "notificaiton", + new mongoose.Schema( + { + createdAt: Date, + }, + { strict: false } + ) +); + +(async () => { + console.log( + new Date().toISOString() + " Started notification cleanup script..." + ); + + await mongoose.connect(process.env.DB_URI); + + const fifthDay = new Date(Date.now() - 3600 * 24 * 5 * 1000); + const result = await notifModel.deleteMany({ createdAt: { $lt: fifthDay } }); + + console.log(`Deleted ${result.deletedCount} notifications`); + + await mongoose.connection.close(); +})().catch((err) => console.log(err));