Jobs / Watching a job
Watching a job
A program can ask where a job is, how many jobs are ahead of it, and how far it got, and follow it as it changes: "3 ahead", then "thinking", then done. There is no status table to keep in step with the queue: the queue knows.
const job = await videos.get(id) // { id, value, state, at, attempt, ahead, progress, error, … } | undefined
for await (const job of videos.watch(id)) {
send({ state: job.state, ahead: job.ahead, progress: job.progress }) // waiting, 3 ahead … running, 0.4 … done
}
videos.work(async (video, run) => {
await transcode(video, done => run.setProgress(done)) // shown to get and watch
})job = await videos.get(id) # Job(id, value, state, at, attempt, ahead, progress, …) | None
async for job in videos.watch(id):
send(job.state, job.ahead, job.progress)
async def convert(video: Video, run: tinystore.Run) -> None:
await transcode(video, run.set_progress)
videos.work(convert)job, found, err := videos.Get(ctx, id)
for job, err := range videos.Watch(ctx, id) {
if err != nil {
return err
}
send(job.State, job.Ahead, job.Progress)
}
err = videos.Work(ctx, func(ctx context.Context, v Video, run *jobs.Run) error {
return transcode(ctx, v, func(done float64) { run.SetProgress(done) })
})let job = videos.get(&id)?;
for job in videos.watch(&id) {
let job = job?;
send(job.state, job.ahead, job.progress);
}What a job says
| Field | Means |
|---|---|
state | scheduled, waiting, running, failed or done |
at | when it runs next |
ahead | the jobs that run before a waiting one, exact up to 10,000 |
aheadAtLeast | true when the count stopped at 10,000 and more are ahead |
attempt | this run's number, the first being 1 |
progress | what the handler set with setProgress, any JSON up to 4 KiB |
error | why its last run failed |
lastRun | when the last finished run started and ended |
get returns nothing for an id with no job. A done job without dedupe is
gone, so get returns nothing for it too.
The store stops counting at 10,000. Past that, ahead is 10,000 and
aheadAtLeast is true, so show "10,000+", not "10,000". It's never an
estimate (contract JOB-25). Python and Rust spell it ahead_at_least, Go
AheadAtLeast.
After it ran
A page that shows "sent" once a reminder ran needs the done job to stay. Open
the queue with dedupe: a done job is kept that long, with its value and its
last run, and get returns it as done.
const reminders = store.queue<Reminder>('reminders', { dedupe: '7d' })
const job = await reminders.get(id) // { state: 'done', lastRun: { startedAt, endedAt }, … } for a week
const shown = job?.state ?? 'gone' // no job: never added, cancelled, or done more than a week agoreminders = store.queue("reminders", Reminder, dedupe="7d")
job = await reminders.get(id) # Job(state="done", last_run=…) for a week
shown = job.state if job else "gone"A job keeps its last run, not each attempt: error is why the last run
failed. A history of every delivery, its attempt and its answer, is the
application's own: an event the handler records, or a row it writes.
How a watch behaves
watchyields the job as it is, then again each time it changes, and ends with it:done,failed, orcancelled, a state only a watcher sees.- A watch whose connection drops watches again on the next one; a job that ended meanwhile ends the watch without its last state.
- Progress lives in memory: a run that a restart ends starts its progress again from nothing. A worker in Bun, Node, Python or Go sends the latest progress ten times a second at most, and a Rust worker writes it at once, so a handler can set it for every chunk it reads.
Stream the states to a browser over server-sent events, and a user sees the queue move.
See also
- Jobs: the engine's overview.
- Show the reply's progress in the Handbook.