From dcc7f5fdb04f57a498e9d0f5a1de8e0698549590 Mon Sep 17 00:00:00 2001 From: Andres De la Flor <84483578+fmcubium@users.noreply.github.com> Date: Thu, 11 Apr 2024 22:10:35 -0400 Subject: [PATCH] Implemented cronjob for message deletion (#207) * Cronjob works now * Made requested changes * Switched to publish subscribe model for messages (#208) * added pubsub for messages * test * finished pub sub * testing empty secret * Delete server/firebase-secrets.json * added pubsub for messages * test * rebased --------- Co-authored-by: AlexanderWangY * packages! * Cronjob works now * Made requested changes * packages! * Commented out deleter by default for testing purposes * Removed interface imports --------- Co-authored-by: Alexander Wang <98280966+AlexanderWangY@users.noreply.github.com> Co-authored-by: AlexanderWangY --- server/.gitignore | 3 +++ server/package-lock.json | 20 ++++++++++++++++++++ server/package.json | 1 + server/src/actions/deleter.ts | 29 +++++++++++++++++++++++++++++ server/src/index.ts | 6 ++++++ 5 files changed, 59 insertions(+) create mode 100644 server/src/actions/deleter.ts diff --git a/server/.gitignore b/server/.gitignore index 1ed29ce47..34bf73406 100644 --- a/server/.gitignore +++ b/server/.gitignore @@ -138,3 +138,6 @@ build # Other .env build/ + +# Firebase Secrets +.firebase-secrets.json \ No newline at end of file diff --git a/server/package-lock.json b/server/package-lock.json index a085f31ee..81ce346a5 100644 --- a/server/package-lock.json +++ b/server/package-lock.json @@ -16,6 +16,7 @@ "firebase-admin": "^12.0.0", "geofire-common": "^6.0.0", "mailgun.js": "^3.7.2", + "node-cron": "^3.0.3", "socket.io": "^4.7.4", "uuid": "^9.0.1" }, @@ -7504,6 +7505,25 @@ "node": ">= 0.6" } }, + "node_modules/node-cron": { + "version": "3.0.3", + "resolved": "https://registry.npmjs.org/node-cron/-/node-cron-3.0.3.tgz", + "integrity": "sha512-dOal67//nohNgYWb+nWmg5dkFdIwDm8EpeGYMekPMrngV3637lqnX0lbUcCtgibHTz6SEz7DAIjKvKDFYCnO1A==", + "dependencies": { + "uuid": "8.3.2" + }, + "engines": { + "node": ">=6.0.0" + } + }, + "node_modules/node-cron/node_modules/uuid": { + "version": "8.3.2", + "resolved": "https://registry.npmjs.org/uuid/-/uuid-8.3.2.tgz", + "integrity": "sha512-+NYs2QeMWy+GWFOEm9xnn6HCDp0l7QBD7ml8zLUmJ+93Q5NF0NocErnwkTkXVFNiX3/fpC6afS8Dhb/gz7R7eg==", + "bin": { + "uuid": "dist/bin/uuid" + } + }, "node_modules/node-fetch": { "version": "2.7.0", "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.7.0.tgz", diff --git a/server/package.json b/server/package.json index 093c72eb6..463b48ee5 100644 --- a/server/package.json +++ b/server/package.json @@ -31,6 +31,7 @@ "firebase-admin": "^12.0.0", "geofire-common": "^6.0.0", "mailgun.js": "^3.7.2", + "node-cron": "^3.0.3", "socket.io": "^4.7.4", "uuid": "^9.0.1" }, diff --git a/server/src/actions/deleter.ts b/server/src/actions/deleter.ts new file mode 100644 index 000000000..dc495e758 --- /dev/null +++ b/server/src/actions/deleter.ts @@ -0,0 +1,29 @@ +import { messagesCollection } from '../utilities/firebaseInit' +const cron = require('node-cron') + +//Schedule tasks to be run on the server +export const scheduleCron = () => { + cron.schedule('*/30 * * * * *', function() { + console.log('Deleting old messages every 30s.') + + //Deleter action, takes in a unix timestamp and deletes + //everything older than that + const expiryTime = Number(process.env.message_duration) //Set to 1 minute for testing purposes + + const q = messagesCollection.orderByChild('timeSent').endAt(Date.now() - expiryTime) + + q.on('value', (querySnapshot)=> { + querySnapshot.forEach(async (doc) => { + //Delete the doc here + console.log(doc.uid) + await doc.ref.delete().then(() => { + console.log("Document successfully deleted!") + }).catch((error) => { + console.error("Error removing document:", error) + }) + }) + }) + }) +} + + diff --git a/server/src/index.ts b/server/src/index.ts index 63d3b360b..d52c35ff3 100644 --- a/server/src/index.ts +++ b/server/src/index.ts @@ -13,6 +13,7 @@ import { getAuth } from 'firebase-admin/auth'; import Mailgun from "mailgun.js"; import { messagesCollection } from './utilities/firebaseInit'; import { calculateDistanceInMeters } from './actions/calculateDistance'; +import { scheduleCron } from './actions/deleter'; const { createServer } = require("http"); const { Server } = require("socket.io"); @@ -333,6 +334,11 @@ app.listen(express_port, () => { ); }); + +//Remove the comments if you want to use the deleter !!!!!! +//scheduleCron(); // Begin searching and collecting Garbage (old messages) + + // Some old API routes are commented out for now due to breaking type changes. // REST functions