Skip to content

Commit

Permalink
Cronjob works now
Browse files Browse the repository at this point in the history
  • Loading branch information
fmcubium committed Apr 1, 2024
1 parent 787e006 commit 674a0e2
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
1 change: 1 addition & 0 deletions server/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -138,3 +138,4 @@ build
# Other
.env
build/
.firebase-secrets.json
25 changes: 25 additions & 0 deletions server/src/actions/deleter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
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.id)
await doc.ref.delete()
})
})
})
}


4 changes: 4 additions & 0 deletions server/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {geohashForLocation} from 'geofire-common';
import { ConnectedUser } from './types/User';
import { getAuth } from 'firebase-admin/auth';
import Mailgun from "mailgun.js";
import { scheduleCron } from './actions/deleter';

const { createServer } = require("http");
const { Server } = require("socket.io");
Expand All @@ -31,6 +32,9 @@ const io = new Server(socketServer, {
},
});

// Begin searching and collecting Garbage (old messages)
scheduleCron();

// Firebase JWT Authorization Custom Middleware
io.use(async (socket, next) => {
const token = socket.handshake.auth.token;
Expand Down

0 comments on commit 674a0e2

Please sign in to comment.