Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions apps/bot/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,24 @@ if (!GITHUB_TOKEN) {
process.exit(1)
}

const WORKER_URL = process.env.WORKER_URL || 'https://wright-worker.fly.dev'

/**
* Wake the worker by hitting its health endpoint.
* This triggers Fly.io auto-start if the machine is stopped.
* Errors are silently ignored — the request just needs to reach Fly's proxy.
*/
async function wakeWorker(): Promise<void> {
try {
await fetch(`${WORKER_URL}/health`, { signal: AbortSignal.timeout(5000) })
console.log('[wake] Worker pinged successfully')
} catch {
// Ignore — the request just needs to trigger Fly.io auto-start.
// The machine may take a few seconds to boot, so a timeout is expected.
console.log('[wake] Worker ping sent (may be booting)')
}
}

// ---------------------------------------------------------------------------
// Bot setup
// ---------------------------------------------------------------------------
Expand Down Expand Up @@ -349,6 +367,9 @@ bot.command('revise', async (ctx: Context) => {
parentJobId: originalJob.id,
})

// Wake the worker so it picks up the revision job
wakeWorker()

await ctx.reply(
[
'\u{1F504} <b>Revision queued!</b>',
Expand Down Expand Up @@ -441,6 +462,9 @@ bot.command('task', async (ctx: Context) => {
parentJobId,
})

// Wake the worker so it picks up the PR revision job
wakeWorker()

await ctx.reply(
[
'\u{1F504} <b>PR revision queued!</b>',
Expand Down Expand Up @@ -495,6 +519,9 @@ bot.command('task', async (ctx: Context) => {
githubToken: GITHUB_TOKEN,
})

// Wake the worker so it picks up the new job
wakeWorker()

await ctx.reply(
[
'\u{2705} <b>Job queued!</b>',
Expand Down Expand Up @@ -666,6 +693,9 @@ bot.on('message:text', async (ctx, next) => {
parentJobId: originalJob.id,
})

// Wake the worker so it picks up the revision job
wakeWorker()

await ctx.reply(
[
'\u{1F504} <b>Revision queued from reply!</b>',
Expand Down
4 changes: 2 additions & 2 deletions apps/worker/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ const FLY_APP_NAME = process.env.FLY_APP_NAME || 'wright-worker'
// Track active jobs with AbortControllers for cancellation
const runningJobs = new Map<string, AbortController>()
let activeJobs = 0
const IDLE_SHUTDOWN_MS = 5 * 60 * 1000 // 5 min idle → exit (scale-to-zero)
const IDLE_SHUTDOWN_MS = 15 * 60 * 1000 // 15 min idle → exit (scale-to-zero)
let idleTimer: ReturnType<typeof setTimeout> | null = null
let keepAliveInterval: ReturnType<typeof setInterval> | null = null

function resetIdleTimer() {
if (idleTimer) clearTimeout(idleTimer)
if (activeJobs === 0) {
idleTimer = setTimeout(() => {
console.log('No active jobs for 5 minutes, shutting down')
console.log('No active jobs for 15 minutes, shutting down')
process.exit(0)
}, IDLE_SHUTDOWN_MS)
}
Expand Down
Loading