add notification cleanup script
This commit is contained in:
@@ -1,5 +1,4 @@
|
|||||||
import mongoose from "mongoose";
|
import mongoose from "mongoose";
|
||||||
import { setTimeout } from "timers/promises";
|
|
||||||
|
|
||||||
const permitsModel = mongoose.model(
|
const permitsModel = mongoose.model(
|
||||||
"permit",
|
"permit",
|
||||||
@@ -181,7 +180,4 @@ const processedModel = mongoose.model(
|
|||||||
console.log(`${count} permits archived`);
|
console.log(`${count} permits archived`);
|
||||||
|
|
||||||
await mongoose.connection.close();
|
await mongoose.connection.close();
|
||||||
|
|
||||||
console.log("Archiving complete. Going to sleep...");
|
|
||||||
await setTimeout(3600 * 25 * 1000);
|
|
||||||
})().catch((err) => console.log(err));
|
})().catch((err) => console.log(err));
|
||||||
|
|||||||
26
cron/notificationCleanup.js
Normal file
26
cron/notificationCleanup.js
Normal file
@@ -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));
|
||||||
Reference in New Issue
Block a user