Skip to content

Commit c6b2e65

Browse files
abrichrclaude
andauthored
fix: bot wakes worker after job insert, increase idle timeout to 15min (#22)
The Telegram bot now pings the worker's /health endpoint after every insertJob call, triggering Fly.io auto-start if the machine is stopped. Also increases worker idle shutdown from 5 to 15 minutes. Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent ff313a8 commit c6b2e65

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

apps/bot/src/index.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,24 @@ if (!GITHUB_TOKEN) {
3535
process.exit(1)
3636
}
3737

38+
const WORKER_URL = process.env.WORKER_URL || 'https://wright-worker.fly.dev'
39+
40+
/**
41+
* Wake the worker by hitting its health endpoint.
42+
* This triggers Fly.io auto-start if the machine is stopped.
43+
* Errors are silently ignored — the request just needs to reach Fly's proxy.
44+
*/
45+
async function wakeWorker(): Promise<void> {
46+
try {
47+
await fetch(`${WORKER_URL}/health`, { signal: AbortSignal.timeout(5000) })
48+
console.log('[wake] Worker pinged successfully')
49+
} catch {
50+
// Ignore — the request just needs to trigger Fly.io auto-start.
51+
// The machine may take a few seconds to boot, so a timeout is expected.
52+
console.log('[wake] Worker ping sent (may be booting)')
53+
}
54+
}
55+
3856
// ---------------------------------------------------------------------------
3957
// Bot setup
4058
// ---------------------------------------------------------------------------
@@ -349,6 +367,9 @@ bot.command('revise', async (ctx: Context) => {
349367
parentJobId: originalJob.id,
350368
})
351369

370+
// Wake the worker so it picks up the revision job
371+
wakeWorker()
372+
352373
await ctx.reply(
353374
[
354375
'\u{1F504} <b>Revision queued!</b>',
@@ -441,6 +462,9 @@ bot.command('task', async (ctx: Context) => {
441462
parentJobId,
442463
})
443464

465+
// Wake the worker so it picks up the PR revision job
466+
wakeWorker()
467+
444468
await ctx.reply(
445469
[
446470
'\u{1F504} <b>PR revision queued!</b>',
@@ -495,6 +519,9 @@ bot.command('task', async (ctx: Context) => {
495519
githubToken: GITHUB_TOKEN,
496520
})
497521

522+
// Wake the worker so it picks up the new job
523+
wakeWorker()
524+
498525
await ctx.reply(
499526
[
500527
'\u{2705} <b>Job queued!</b>',
@@ -666,6 +693,9 @@ bot.on('message:text', async (ctx, next) => {
666693
parentJobId: originalJob.id,
667694
})
668695

696+
// Wake the worker so it picks up the revision job
697+
wakeWorker()
698+
669699
await ctx.reply(
670700
[
671701
'\u{1F504} <b>Revision queued from reply!</b>',

apps/worker/src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,15 @@ const FLY_APP_NAME = process.env.FLY_APP_NAME || 'wright-worker'
1919
// Track active jobs with AbortControllers for cancellation
2020
const runningJobs = new Map<string, AbortController>()
2121
let activeJobs = 0
22-
const IDLE_SHUTDOWN_MS = 5 * 60 * 1000 // 5 min idle → exit (scale-to-zero)
22+
const IDLE_SHUTDOWN_MS = 15 * 60 * 1000 // 15 min idle → exit (scale-to-zero)
2323
let idleTimer: ReturnType<typeof setTimeout> | null = null
2424
let keepAliveInterval: ReturnType<typeof setInterval> | null = null
2525

2626
function resetIdleTimer() {
2727
if (idleTimer) clearTimeout(idleTimer)
2828
if (activeJobs === 0) {
2929
idleTimer = setTimeout(() => {
30-
console.log('No active jobs for 5 minutes, shutting down')
30+
console.log('No active jobs for 15 minutes, shutting down')
3131
process.exit(0)
3232
}, IDLE_SHUTDOWN_MS)
3333
}

0 commit comments

Comments
 (0)