Reading — step 1 of 5
Read
~1 min readJob Queue & Execution
The Execution Loop
Your scheduler's main loop:
while running:
next_run, job = heap.peek() # earliest-firing job
delta = next_run - now()
if delta > 0:
sleep(min(delta, 60)) # cap at 60s for responsiveness
continue
heap.pop()
spawn(run_job, job)
job.last_run = now()
job.next_run = next_run(job.cron, job.last_run)
heap.push((job.next_run, job.id))
The 60-second cap matters: if a job is added with a sooner firing time while we're sleeping for an hour, we'd miss it. Some schedulers wake on signal (condition variable) instead.
spawn(run_job, job) should NOT block the loop. Use threads, asyncio, or
spawn a subprocess and wait() separately.
Discussion
Ask a question, share an insight, or help someone who’s stuck.
Sign in to post a comment or reply.
Loading…