File tree Expand file tree Collapse file tree 4 files changed +54
-0
lines changed
Expand file tree Collapse file tree 4 files changed +54
-0
lines changed Original file line number Diff line number Diff line change @@ -7,5 +7,7 @@ const sessionSchema = new mongoose.Schema({
77 createdAt : { type : Date , default : Date . now } ,
88} ) ;
99
10+ sessionSchema . index ( { expiresAt : 1 } ) ;
11+ sessionSchema . index ( { token : 1 } ) ;
1012
1113export const Session = mongoose . model ( "Session" , sessionSchema ) ;
Original file line number Diff line number Diff line change @@ -10,6 +10,7 @@ import { ChatMessage } from "./models/chatMessageModel.js";
1010import Room from "./models/roomModel.js" ; // ✅ Import for room events
1111import app from "./app.js" ;
1212import logger from "./utils/logger.js" ;
13+ import { initializeScheduler } from "./utils/scheduler.js" ;
1314
1415dotenv . config ( ) ;
1516
@@ -166,6 +167,7 @@ mongoose
166167 . connect ( MONGO_URI )
167168 . then ( ( ) => {
168169 logger . info ( "🗄️ MongoDB connected successfully!" ) ;
170+ initializeScheduler ( ) ;
169171 httpServer . listen ( PORT , ( ) => {
170172 logger . info ( `🚀 Server running on port ${ PORT } ` ) ;
171173 logger . info ( `📡 Socket.io real-time chat ready` ) ;
Original file line number Diff line number Diff line change 1+ import cron from "node-cron" ;
2+ import { cleanupExpiredSessions } from "./sessionCleanup.js" ;
3+ import logger from "./logger.js" ;
4+
5+ export const initializeScheduler = ( ) => {
6+ const sessionCleanupCron = process . env . SESSION_CLEANUP_CRON || "0 * * * *" ;
7+ const sessionCleanupJob = cron . schedule ( sessionCleanupCron , async ( ) => {
8+ try {
9+ logger . info ( "🔄 Running scheduled session cleanup..." ) ;
10+ const result = await cleanupExpiredSessions ( ) ;
11+ logger . info ( `✅ Session cleanup completed: ${ result . deletedCount } expired session(s) removed` ) ;
12+ } catch ( error : any ) {
13+ logger . error ( "Scheduled session cleanup failed:" , error ) ;
14+ }
15+ } , {
16+ timezone : "UTC" ,
17+ } ) ;
18+ cleanupExpiredSessions ( )
19+ . then ( ( result ) => {
20+ logger . info ( `✅ Initial session cleanup completed on startup: ${ result . deletedCount } expired session(s) removed` ) ;
21+ } )
22+ . catch ( ( error ) => {
23+ logger . error ( " Initial session cleanup failed:" , error ) ;
24+ } ) ;
25+
26+ logger . info ( "📅 Scheduled tasks initialized:" ) ;
27+ logger . info ( ` - Session cleanup: Cron schedule "${ sessionCleanupCron } " (every hour by default)` ) ;
28+
29+ return {
30+ sessionCleanupJob,
31+ } ;
32+ } ;
33+
Original file line number Diff line number Diff line change 1+ import { Session } from "../models/sessionModel.js" ;
2+ import logger from "./logger.js" ;
3+ export const cleanupExpiredSessions = async ( ) : Promise < { deletedCount : number } > => {
4+ try {
5+ const now = new Date ( ) ;
6+ const result = await Session . deleteMany ( {
7+ expiresAt : { $lt : now } ,
8+ } ) ;
9+ if ( result . deletedCount > 0 ) {
10+ logger . info ( `🧹 Cleaned up ${ result . deletedCount } expired session(s)` ) ;
11+ }
12+ return { deletedCount : result . deletedCount || 0 } ;
13+ } catch ( error : any ) {
14+ logger . error ( " Error cleaning up expired sessions:" , error ) ;
15+ throw error ;
16+ }
17+ } ;
You can’t perform that action at this time.
0 commit comments