watch, dont poll

This commit is contained in:
Chris Wanstrath 2026-02-02 11:50:29 -08:00
parent 054c73b926
commit 8347177c77

View File

@ -9,7 +9,7 @@ import { SCHEDULES, type CronJob } from './lib/schedules'
import type { Child } from 'hono/jsx'
import { join } from 'path'
import { mkdir, writeFile } from 'fs/promises'
import { existsSync } from 'fs'
import { existsSync, watch } from 'fs'
const APPS_DIR = process.env.APPS_DIR!
@ -372,8 +372,10 @@ async function init() {
}
}
// Re-discover every 60s
setInterval(async () => {
// Watch for cron file changes
let debounceTimer: Timer | null = null
async function rediscover() {
const jobs = await discoverCronJobs()
const existing = getAllJobs()
@ -403,7 +405,15 @@ setInterval(async () => {
}
setJobs(jobs)
}, 60000)
}
watch(APPS_DIR, { recursive: true }, (_event, filename) => {
if (!filename?.includes('/cron/') && !filename?.includes('\\cron\\')) return
if (!filename.endsWith('.ts')) return
if (debounceTimer) clearTimeout(debounceTimer)
debounceTimer = setTimeout(rediscover, 100)
})
init()