-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
65 lines (56 loc) · 1.8 KB
/
Copy pathserver.js
File metadata and controls
65 lines (56 loc) · 1.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import http from 'node:http';
import app from './src/app.js';
import { registerCronJobs } from './src/config/cron.js';
import { validateEnv } from './src/config/env.validator.js';
import { logger } from './src/config/logger.js';
import models from './src/models/index.js';
// Valider les variables d'environnement AVANT tout le reste
await validateEnv();
const PORT = process.env.PORT || 3022;
const server = http.createServer(app);
server.listen(PORT, () => {
logger.info({ port: PORT }, `Puna started on port ${PORT}`);
registerCronJobs();
});
/**
* Fermeture gracieuse du serveur
* - Ferme les connexions entrantes
* - Ferme la base de données
* - Force kill après timeout de sécurité
*/
function shutdownAndExit(error) {
logger.error(error, 'Critical error - initiating graceful shutdown');
// Fermer les nouvelles connexions
server.close(() => {
logger.info('HTTP server closed');
// Fermer la connexion Sequelize
models.sequelize
.close()
.then(() => {
logger.info('Database connection closed');
process.exit(1);
})
.catch((err) => {
logger.error(err, 'Error closing database connection');
process.exit(1);
});
});
// Sécurité : kill forcé après timeout si fermeture trop lente
setTimeout(() => {
logger.error('Shutdown timeout - forcing exit');
process.exit(1);
}, 5000);
}
/**
* Gestion des exceptions non capturées
*/
process.on('uncaughtException', (err) => {
shutdownAndExit(err);
});
/**
* Gestion des promesses rejetées non capturées
*/
process.on('unhandledRejection', (reason) => {
const error = reason instanceof Error ? reason : new Error(String(reason));
shutdownAndExit(error);
});