feat: integrate monitoring collector into scheduler

This commit is contained in:
2026-02-23 16:12:47 +00:00
parent 990f3532b4
commit 0588ee3735

View File

@@ -17,6 +17,7 @@ import wpMonitorRouter from './routes/wp-monitor.js'
import serverMetricsRouter from './routes/server-metrics.js' import serverMetricsRouter from './routes/server-metrics.js'
import financialRouter from './routes/financial.js' import financialRouter from './routes/financial.js'
import { collectAllServerMetrics } from './services/server-metrics.js' import { collectAllServerMetrics } from './services/server-metrics.js'
import { collectMonitoringData } from './services/monitoring-collector.js'
const __filename = fileURLToPath(import.meta.url) const __filename = fileURLToPath(import.meta.url)
const __dirname = path.dirname(__filename) const __dirname = path.dirname(__filename)
@@ -42,6 +43,7 @@ app.use('/api', limiter)
// SECURITY: CORS Restrito (Vulnerabilidade 2.2) // SECURITY: CORS Restrito (Vulnerabilidade 2.2)
// ============================================================================ // ============================================================================
const allowedOrigins = [ const allowedOrigins = [
'https://dash.descomplicar.pt',
'https://dashboard.descomplicar.pt', 'https://dashboard.descomplicar.pt',
'https://desk.descomplicar.pt' 'https://desk.descomplicar.pt'
] ]
@@ -53,7 +55,7 @@ if (!isProduction) {
allowedOrigins.push(process.env.FRONTEND_URL || 'http://localhost:5173') allowedOrigins.push(process.env.FRONTEND_URL || 'http://localhost:5173')
} }
app.use(cors({ const corsMiddleware = cors({
origin: (origin, callback) => { origin: (origin, callback) => {
// Permitir requests sem origin (curl, Postman, etc) em dev // Permitir requests sem origin (curl, Postman, etc) em dev
if (!origin && !isProduction) { if (!origin && !isProduction) {
@@ -68,7 +70,10 @@ app.use(cors({
} }
}, },
credentials: true credentials: true
})) })
// CORS apenas nas rotas API (nao bloquear assets estaticos)
app.use('/api', corsMiddleware)
app.use(express.json()) app.use(express.json())
@@ -170,22 +175,28 @@ app.listen(PORT, () => {
console.log('='.repeat(50)) console.log('='.repeat(50))
} }
// Auto-collect server metrics every 5 minutes // Auto-collect metrics every 5 minutes
if (isProduction) { if (isProduction) {
const INTERVAL = 5 * 60 * 1000 const INTERVAL = 5 * 60 * 1000
console.log('[SCHEDULER] Server metrics collection every 5min') console.log('[SCHEDULER] Server metrics + monitoring collection every 5min')
// Initial collection after 30s (let server stabilize) // Initial collection after 30s (let server stabilize)
setTimeout(() => { setTimeout(() => {
collectAllServerMetrics().catch(err => collectAllServerMetrics().catch(err =>
console.error('[SCHEDULER] Initial collection failed:', err.message) console.error('[SCHEDULER] Initial server metrics failed:', err.message)
)
collectMonitoringData().catch(err =>
console.error('[SCHEDULER] Initial monitoring collection failed:', err.message)
) )
}, 30000) }, 30000)
// Recurring collection // Recurring collection
setInterval(() => { setInterval(() => {
collectAllServerMetrics().catch(err => collectAllServerMetrics().catch(err =>
console.error('[SCHEDULER] Collection failed:', err.message) console.error('[SCHEDULER] Server metrics failed:', err.message)
)
collectMonitoringData().catch(err =>
console.error('[SCHEDULER] Monitoring collection failed:', err.message)
) )
}, INTERVAL) }, INTERVAL)
} }