Commit
Refactor: Improve startup, shutdown, and cleanup logic
Commit details
Commit notes
This commit introduces several improvements:
- **Startup Verification:** Added a `verifyDependencies` function to ensure critical services like the database and Redis are available before the server starts. This prevents the server from running in a degraded state. - **Graceful Shutdown:** Implemented timeouts for both startup verification and graceful shutdown to prevent the server from hanging indefinitely. - **Scheduled Cleanup:** Introduced a new module `scheduled-cleanup.ts` for managing periodic cleanup tasks. - `cleanupExpiredOAuthStates`: Removes expired OAuth state entries from the database. - `cleanupExpiredRefreshTokens`: Removes expired refresh tokens. - `runAllCleanupTasks`: A utility to run all cleanup tasks. - **OAuth State Cleanup:** The server now schedules `cleanupExpiredOAuthStates` to run every 5 minutes. - **Refresh Token Limit:** Enforced a `MAX_REFRESH_TOKENS_PER_USER` limit (set to 10) during user login. If a user exceeds this limit, the oldest tokens are automatically revoked. - **File Upload Limit:** Added a `MAX_CHUNK_SIZE` check for multipart/form-data requests to prevent excessively large file uploads. - **Configuration Checks:** Added checks for necessary Google OAuth configuration variables before initiating the OAuth callback. - **Code Organization:** Refactored the main `index.ts` file to better organize startup, shutdown, and dependency verification logic. - **Exported Constant:** Exported `MAX_REFRESH_TOKENS_PER_USER` from `refresh-token.ts` to be used in `auth.ts`.
Co-authored-by: armin.naimi <[email redacted]>
- Files changed
- 4
- Lines added
- +255
- Lines removed
- −24