feat: integrate monitoring collector into scheduler
This commit is contained in:
@@ -17,6 +17,7 @@ import wpMonitorRouter from './routes/wp-monitor.js'
|
||||
import serverMetricsRouter from './routes/server-metrics.js'
|
||||
import financialRouter from './routes/financial.js'
|
||||
import { collectAllServerMetrics } from './services/server-metrics.js'
|
||||
import { collectMonitoringData } from './services/monitoring-collector.js'
|
||||
|
||||
const __filename = fileURLToPath(import.meta.url)
|
||||
const __dirname = path.dirname(__filename)
|
||||
@@ -42,6 +43,7 @@ app.use('/api', limiter)
|
||||
// SECURITY: CORS Restrito (Vulnerabilidade 2.2)
|
||||
// ============================================================================
|
||||
const allowedOrigins = [
|
||||
'https://dash.descomplicar.pt',
|
||||
'https://dashboard.descomplicar.pt',
|
||||
'https://desk.descomplicar.pt'
|
||||
]
|
||||
@@ -53,7 +55,7 @@ if (!isProduction) {
|
||||
allowedOrigins.push(process.env.FRONTEND_URL || 'http://localhost:5173')
|
||||
}
|
||||
|
||||
app.use(cors({
|
||||
const corsMiddleware = cors({
|
||||
origin: (origin, callback) => {
|
||||
// Permitir requests sem origin (curl, Postman, etc) em dev
|
||||
if (!origin && !isProduction) {
|
||||
@@ -68,7 +70,10 @@ app.use(cors({
|
||||
}
|
||||
},
|
||||
credentials: true
|
||||
}))
|
||||
})
|
||||
|
||||
// CORS apenas nas rotas API (nao bloquear assets estaticos)
|
||||
app.use('/api', corsMiddleware)
|
||||
|
||||
app.use(express.json())
|
||||
|
||||
@@ -170,22 +175,28 @@ app.listen(PORT, () => {
|
||||
console.log('='.repeat(50))
|
||||
}
|
||||
|
||||
// Auto-collect server metrics every 5 minutes
|
||||
// Auto-collect metrics every 5 minutes
|
||||
if (isProduction) {
|
||||
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)
|
||||
setTimeout(() => {
|
||||
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)
|
||||
|
||||
// Recurring collection
|
||||
setInterval(() => {
|
||||
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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user