-
Notifications
You must be signed in to change notification settings - Fork 101
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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 <[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
1 parent
949e33a
commit dcc7f5f
Showing
5 changed files
with
59 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -138,3 +138,6 @@ build | |
# Other | ||
.env | ||
build/ | ||
|
||
# Firebase Secrets | ||
.firebase-secrets.json |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
}) | ||
}) | ||
}) | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters