Skip to content

Commit

Permalink
Implemented cronjob for message deletion (#207)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>

* packages!

* Cronjob works now

* Made requested changes

* packages!

* Commented out deleter by default for testing purposes

* Removed interface imports

---------

Co-authored-by: Alexander Wang <[email protected]>
Co-authored-by: AlexanderWangY <[email protected]>
  • Loading branch information
3 people authored Apr 12, 2024
1 parent 949e33a commit dcc7f5f
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 0 deletions.
3 changes: 3 additions & 0 deletions server/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -138,3 +138,6 @@ build
# Other
.env
build/

# Firebase Secrets
.firebase-secrets.json
20 changes: 20 additions & 0 deletions server/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
29 changes: 29 additions & 0 deletions server/src/actions/deleter.ts
Original file line number Diff line number Diff line change
@@ -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)
})
})
})
})
}


6 changes: 6 additions & 0 deletions server/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit dcc7f5f

Please sign in to comment.