Skip to content

Commit befa5ec

Browse files
author
Dmitriy Zhuk
committed
fix: graceful shutdown on SIGTERM/SIGINT — ensures final S3 sync before docker stop
When docker stop sends SIGTERM, runtime now calls stop() which: 1. Stops channel listeners 2. Stops cron + heartbeat 3. Pushes final S3 sync (preserving all .agent/ data) docker stop uses t=30s timeout (set in instalegram API) to give enough time.
1 parent 2dbaa61 commit befa5ec

1 file changed

Lines changed: 17 additions & 0 deletions

File tree

src/index.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ process.on("unhandledRejection", (reason) => {
1313
console.error("[unhandledRejection] caught:", reason)
1414
})
1515

16+
17+
1618
// Validate env vars
1719
const requiredEnv = ["TELEGRAM_BOT_TOKEN", "TELEGRAM_BOT_NAME", "TELEGRAM_BOT_ADMIN_IDS", "CLAUDE_CODE_OAUTH_TOKEN"]
1820
const optionalEnv = ["LLM_MODEL", "LLM_FALLBACK_MODEL", "SECRET_PROVIDER", "ELEVENLABS_API_KEY", "S3_BUCKET", "S3_PREFIX", "AWS_REGION", "AWS_ACCESS_KEY_ID", "AWS_SECRET_ACCESS_KEY", "AWS_SECRET_PREFIX", "SLACK_BOT_TOKEN", "SLACK_APP_TOKEN", "ANTHROPIC_API_KEY", "CLEANSLICE_AGENT_DIR", "PORT"]
@@ -52,6 +54,21 @@ const runtime = new AgentRuntime({
5254
await runtime.start()
5355
console.log("🤖 Agent runtime started")
5456

57+
// Graceful shutdown on SIGTERM (docker stop) and SIGINT (ctrl+c)
58+
// Gives runtime time to push final S3 sync before exit
59+
async function shutdown(signal: string) {
60+
console.log(`[shutdown] received ${signal}, stopping runtime...`)
61+
try {
62+
await runtime.stop()
63+
console.log("[shutdown] clean exit")
64+
} catch (err) {
65+
console.error("[shutdown] error during stop:", err)
66+
}
67+
process.exit(0)
68+
}
69+
process.on("SIGTERM", () => shutdown("SIGTERM"))
70+
process.on("SIGINT", () => shutdown("SIGINT"))
71+
5572
Bun.serve({
5673
port: Number(process.env.PORT ?? 3000),
5774
fetch(req) {

0 commit comments

Comments
 (0)