43 lines
1.2 KiB
TypeScript
43 lines
1.2 KiB
TypeScript
import { db } from './db'
|
|
import { redis } from './redis'
|
|
import { runMigrations } from './migrate'
|
|
import { seedIfEmpty } from './seed'
|
|
import { buildApp } from './app'
|
|
import { config } from './config'
|
|
|
|
async function waitForDb(retries = 10, delayMs = 3000) {
|
|
for (let i = 1; i <= retries; i++) {
|
|
try {
|
|
await db.query('SELECT 1')
|
|
console.log('[db] Connected')
|
|
return
|
|
} catch {
|
|
console.log(`[db] Not ready, retry ${i}/${retries}...`)
|
|
await new Promise(r => setTimeout(r, delayMs))
|
|
}
|
|
}
|
|
throw new Error('Database not available after retries')
|
|
}
|
|
|
|
async function main() {
|
|
await waitForDb()
|
|
await runMigrations()
|
|
await seedIfEmpty()
|
|
|
|
// При рестарте сервера все агенты считаются офлайн — они переподключатся сами
|
|
await db.query('UPDATE workstations SET is_online = false').catch(() => {})
|
|
console.log('[server] Reset all workstations to offline')
|
|
|
|
await redis.connect()
|
|
console.log('[redis] Connected')
|
|
|
|
const app = await buildApp()
|
|
await app.listen({ port: config.port, host: '0.0.0.0' })
|
|
console.log(`[server] Listening on :${config.port}`)
|
|
}
|
|
|
|
main().catch(err => {
|
|
console.error('[server] Fatal error:', err)
|
|
process.exit(1)
|
|
})
|