worker: add serve.ts — health/status server + worker boot for container deploy
All checks were successful
deploy / deploy (push) Successful in 6s

Container entrypoint used by the 24/7 deploy (root/larp-quant-agent-bot): serves
a health/status endpoint on APP_PORT and boots the worker loop in one process.
Local dev keeps using `pnpm worker` (loop.ts) directly.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
zefiroff 2026-06-09 20:04:58 +04:00
parent 76ebbf3954
commit b31d378eea

42
worker/serve.ts Normal file
View file

@ -0,0 +1,42 @@
/**
* Container entrypoint for the always-on Telegram worker.
*
* The platform's docker deploy proxies the domain to APP_PORT, so we expose a
* tiny health / status endpoint and boot the worker loop in the same process.
* Secrets (TELEGRAM_BOT_TOKEN / TELEGRAM_CHAT_ID) arrive as real env vars via
* Forgejo Secrets + RUNTIME_KEYS config.ts falls back to process.env when
* there is no .env.local. State persists to QUANT_DATA_FILE (=/data on the
* persistent volume) so the paper track record survives redeploys.
*
* Local dev still uses `pnpm worker` (worker/loop.ts) directly this file is
* only the HTTP wrapper the platform needs.
*/
import { createServer } from 'node:http'
import { getState } from './store.ts'
import './loop.ts' // boots the worker: boot message → tick loop → command poll
const started = Date.now()
const PORT = Number(process.env.APP_PORT || process.env.PORT || 8080)
createServer((req, res) => {
if (req.url === '/health') {
res.writeHead(200, { 'content-type': 'text/plain' })
res.end('ok')
return
}
const body: Record<string, unknown> = {
ok: true,
service: 'quant-agent-bot',
uptimeSec: Math.round((Date.now() - started) / 1000),
}
try {
const st = getState()
body.equity = Math.round(st.equity)
body.open = st.positions.length
body.closed = st.closed.length
} catch {
/* state not loaded yet — health still ok */
}
res.writeHead(200, { 'content-type': 'application/json' })
res.end(JSON.stringify(body))
}).listen(PORT, () => console.log(`[serve] health on :${PORT}`))