Fix jwt-token

This commit is contained in:
2025-09-23 15:03:31 +02:00
parent ea5583d56a
commit e3816b056e
4 changed files with 333 additions and 1 deletions

View File

@@ -5,6 +5,8 @@
const cron = require('node-cron');
const { Op } = require('sequelize');
const http = require('http');
const url = require('url');
// Initialize database connection
const { initializeDatabase, getModels } = require('./database');
@@ -46,6 +48,9 @@ class DataRetentionService {
console.log('⏰ Scheduled cleanup: Daily at 2:00 AM UTC');
// Start metrics HTTP server
this.startMetricsServer();
// Run immediate cleanup in development or if IMMEDIATE_CLEANUP is set
if (process.env.NODE_ENV === 'development' || process.env.IMMEDIATE_CLEANUP === 'true') {
console.log('🧹 Running immediate cleanup...');
@@ -233,6 +238,125 @@ class DataRetentionService {
};
}
/**
* Get detailed metrics for dashboard
*/
getMetrics() {
const uptime = Math.floor(process.uptime());
const memoryUsage = process.memoryUsage();
return {
service: {
name: 'data-retention-service',
version: '1.0.0',
status: 'running',
uptime: uptime,
uptimeFormatted: this.formatUptime(uptime)
},
performance: {
memoryUsage: {
heapUsed: Math.round(memoryUsage.heapUsed / 1024 / 1024),
heapTotal: Math.round(memoryUsage.heapTotal / 1024 / 1024),
external: Math.round(memoryUsage.external / 1024 / 1024),
rss: Math.round(memoryUsage.rss / 1024 / 1024)
},
cpuUsage: process.cpuUsage()
},
cleanup: {
lastRun: this.lastCleanup,
lastRunFormatted: this.lastCleanup ? new Date(this.lastCleanup).toLocaleString() : null,
isCurrentlyRunning: this.isRunning,
nextScheduledRun: '2:00 AM UTC daily',
stats: this.cleanupStats
},
schedule: {
cronExpression: '0 2 * * *',
timezone: 'UTC',
description: 'Daily cleanup at 2:00 AM UTC'
}
};
}
/**
* Format uptime in human readable format
*/
formatUptime(seconds) {
const days = Math.floor(seconds / 86400);
const hours = Math.floor((seconds % 86400) / 3600);
const minutes = Math.floor((seconds % 3600) / 60);
const secs = seconds % 60;
if (days > 0) {
return `${days}d ${hours}h ${minutes}m ${secs}s`;
} else if (hours > 0) {
return `${hours}h ${minutes}m ${secs}s`;
} else if (minutes > 0) {
return `${minutes}m ${secs}s`;
} else {
return `${secs}s`;
}
}
/**
* Start HTTP server for metrics endpoint
*/
startMetricsServer() {
const port = process.env.METRICS_PORT || 3001;
const server = http.createServer((req, res) => {
const parsedUrl = url.parse(req.url, true);
// Set CORS headers
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Methods', 'GET, OPTIONS');
res.setHeader('Access-Control-Allow-Headers', 'Content-Type');
res.setHeader('Content-Type', 'application/json');
if (req.method === 'OPTIONS') {
res.writeHead(200);
res.end();
return;
}
if (req.method === 'GET') {
if (parsedUrl.pathname === '/metrics') {
// Detailed metrics for dashboard
res.writeHead(200);
res.end(JSON.stringify(this.getMetrics(), null, 2));
} else if (parsedUrl.pathname === '/health') {
// Simple health check
res.writeHead(200);
res.end(JSON.stringify({
status: 'healthy',
uptime: Math.floor(process.uptime()),
lastCleanup: this.lastCleanup,
isRunning: this.isRunning
}, null, 2));
} else if (parsedUrl.pathname === '/stats') {
// Basic stats
res.writeHead(200);
res.end(JSON.stringify(this.getStats(), null, 2));
} else {
res.writeHead(404);
res.end(JSON.stringify({ error: 'Not found' }));
}
} else {
res.writeHead(405);
res.end(JSON.stringify({ error: 'Method not allowed' }));
}
});
server.listen(port, () => {
console.log(`📊 Metrics server listening on port ${port}`);
console.log(`📊 Endpoints: /health, /metrics, /stats`);
});
return server;
}
/**
* Graceful shutdown
*/