src/app.ts
Startup sequence
HTTP server
Socket.IO
See Socket server for event details.
Scheduled jobs
scheduleExchangeSessionJobs() registers all node-schedule cron jobs. See Scheduled jobs for the full timetable.
Market services initialization
initializeMarketServices() runs on startup:
- Ensures Zerodha market data streaming is active (
initializeMarketDataIfNeeded) - Starts recurring buy and trailing sell for each active index setting whose exchange is currently open
- Syncs the trailing-sell interval
Graceful shutdown
Triggered bySIGINT, SIGTERM, SIGBREAK, uncaughtException, or unhandledRejection:
- Cancel all
node-schedulejobs - Disconnect Zerodha WebSocket (
handleZerodhaDisconnect) - Stop all registered intervals (
stopAllIntervals) - Reset all recurring orders (
resetAll) - Close Socket.IO and HTTP server (10s force-exit timeout)
Error handling
Global Express error middleware:413for payload-too-large errors500with{ success: false, message: 'Internal server error' }for all other errors
Database connection
import './config/database/index' is loaded at the top of app.ts before route registration. MongoDB connection is established asynchronously.