forked from koshikraj/ottopus
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
78 lines (69 loc) · 2.69 KB
/
Copy pathindex.ts
File metadata and controls
78 lines (69 loc) · 2.69 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
66
67
68
69
70
71
72
73
74
75
76
77
78
import { serve } from '@hono/node-server'
import { config } from './config.js'
import { handler } from './app.js'
import { getDb } from './db/client.js'
import { purgeExpired } from './oauth/index.js'
const log = (msg: string, extra: Record<string, unknown> = {}) => {
// stdout only — every platform captures it, and file logging works nowhere.
console.log(JSON.stringify({ level: 'info', msg, ...extra }))
}
const server = serve({ fetch: handler, port: config.port, hostname: config.host }, (info) => {
// The advertised identity is in the first line of the deploy log on purpose.
// config.ts refuses to boot on a loopback mcpUrl in production, but that
// guard needs NODE_ENV to be set, and a deploy that sets nothing at all would
// slip past it — so the value it would have caught is printed where anyone
// watching a deploy can see it. Every OAuth endpoint is built from mcpUrl.
log('listening', {
host: config.host,
port: info.port,
env: config.nodeEnv,
mcpUrl: config.mcpUrl,
webUrl: config.webUrl,
})
})
/**
* Authorization codes live a minute and parked consent requests ten, so both
* tables accumulate rows that can never be used again. Swept hourly rather than
* on a request, because nobody's tool call should pay for housekeeping.
*
* Unref'd, so a sweep pending at shutdown does not hold the process open. The
* next boot picks up whatever this one missed — nothing here is urgent, and
* everything it deletes was already refused by the queries above.
*/
const PURGE_EVERY_MS = 60 * 60 * 1000
if (config.databaseUrl) {
const db = getDb(config.databaseUrl)
const sweep = () =>
void purgeExpired(db).catch((err: unknown) =>
console.error(JSON.stringify({ level: 'error', msg: 'purge failed', err: String(err) })),
)
sweep()
setInterval(sweep, PURGE_EVERY_MS).unref()
}
/**
* Every platform sends SIGTERM before replacing a container. Without this the
* process dies mid-request — and this service holds long-lived MCP streams, so
* that is a dropped agent session rather than a retryable GET.
*/
let shuttingDown = false
function shutdown(signal: NodeJS.Signals): void {
if (shuttingDown) return
shuttingDown = true
log('shutting down', { signal })
const forced = setTimeout(() => {
console.error(JSON.stringify({ level: 'error', msg: 'forced exit, connections still open' }))
process.exit(1)
}, 10_000)
forced.unref()
server.close((err) => {
clearTimeout(forced)
if (err) {
console.error(JSON.stringify({ level: 'error', msg: 'shutdown failed', err: String(err) }))
process.exit(1)
}
log('shutdown complete')
process.exit(0)
})
}
process.on('SIGTERM', shutdown)
process.on('SIGINT', shutdown)