Reference / Jobs API for Rust
Jobs API for Rust
Every public type, trait and function of the Jobs engine in tinystore::jobs and tinystore::aio::jobs, generated from its source. The Jobs guide explains how to use them, and the Bun and Node, Python and Go pages list the same API.
Store
impl StoreStore.queue
pub fn queue<V>(&self, name: &str) -> QueueBuilder<V>
where
V: Serialize + DeserializeOwned + Send + 'static,A queue of jobs whose values are V, as JSON, in the store's jobs.db;
the engine opens with the first queue.
Store.schedule
pub fn schedule(&self, name: &str) -> ScheduleBuilderA schedule: one repeating job under name, the code's own. Each time
the program opens it, the code's repeat replaces the one kept.
aio::Store
impl aio::Storeaio::Store.queue
pub fn queue<V>(&self, name: &str) -> QueueBuilder<V>
where
V: Serialize + DeserializeOwned + Send + 'static,A queue of jobs whose values are V, as JSON, in the store's jobs.db,
as the blocking Store::queue opens one: the
same builder, its calls awaited.
aio::Store.schedule
pub fn schedule(&self, name: &str) -> ScheduleBuilderA schedule: one repeating job under name, the code's own, as
the blocking Store::schedule opens one.
JobCall
pub struct JobCall<'a, V> { /* private fields */ }A call on one job with what it asks beside its value, run by its last
step: later.id(&key).at(nine_am).add(&draft).
JobCall.adding
pub fn adding(self, value: &V) -> WriteThe job's add for a batch of the database the queue was opened from,
with what the call asks beside its value; see
Queue::adding.
JobCall.id
pub fn id(self, id: impl AsRef<str>) -> SelfThe job's id, 1 to 1024 bytes of text: what finds it again, and what adds it once however many times its caller asks.
JobCall.at
pub fn at(self, time: SystemTime) -> SelfRuns the job at time; a time in the past runs it now.
JobCall.delay
pub fn delay(self, span: Duration) -> SelfRuns the job span from now.
JobCall.group
pub fn group(self, group: impl AsRef<str>) -> SelfThe group whose running jobs the queue's concurrency bounds.
JobCall.every
pub fn every(self, span: Duration) -> SelfRepeats the job every span, each id at a phase of its own within it,
the same across restarts; its first run within the next interval unless
at or delay says. A repeat needs an id, which alone stops it.
JobCall.cron
pub fn cron(self, expr: &str, zone: &str) -> SelfRepeats the job by a five-field cron expression on the wall clock of
zone, an IANA name, "UTC" among them.
JobCall.dedupe
pub fn dedupe(self, dedupe: impl Into<Dedupe>) -> SelfHow long this job holds its id once done, in the place of its queue's:
a span from when it was done, or Dedupe::calendar(Calendar::Day, "Europe/Moscow"), until the local day it was done in ends.
JobCall.keep
pub fn keep(self, span: Duration) -> SelfHow long this job is kept once it failed for good, in the place of its
queue's; Duration::ZERO keeps it not at all, its watchers still told
it failed and why.
JobCall.ttl
pub fn ttl(self, span: Duration) -> SelfThe job leaves its queue span after its last write unless written
again: an upsert, a set or an update that names it renews it,
as a key's ttl does. A job does not start at or after its term; a
run under way finishes, and a repeat ends there.
JobCall.quiet
pub fn quiet(self, quiet: Quiet) -> SelfThe hours in which this job does not run, in the place of its queue's,
for its first run and every later one; Quiet::none() holds it by
none. A time it is given inside them, at among them, becomes their
end: the hours are a promise to the person, not a hint.
JobCall.expires_at
pub fn expires_at(self, time: SystemTime) -> SelfThe job leaves its queue at time, as ttl says; one already past
is gone at once.
JobCall.add
pub fn add(self, value: &V) -> Result<bool>Adds the job unless its id is taken: by a job scheduled, waiting or running, or done while the queue's dedupe keeps it. A job failed for good holds no id, and the new job takes its place. Says whether it added one; a job without an id is always added.
JobCall.set
pub fn set(self, value: &V) -> Result<()>Makes the id's job this value at this time, whatever it was: a job waiting is made new, a failed one starts again, and a running one runs once more after this run, with this value.
JobCall.upsert
pub fn upsert(self, value: &V) -> Result<bool>Adds the job when its id is free, as add does, or brings the one
there to this call: its value, group, repeat and options of its own
where they differ, its time, attempts and steps as they were. A
changed value drops the steps kept for the old one; a changed repeat
moves a job not yet due to the repeat's next time. Says whether it
added or changed anything: a job that already was so is not written,
so it is cheap to call on every request. A done job its dedupe holds
is left alone, false, as add leaves it.
JobCall.gather
pub fn gather(self, items: &V, gather: Gather) -> Result<Gathered>Gathers items, a JSON list, into the id's job: a job scheduled or
waiting takes them after its own, in the order they came, and is due
settle after this gather, never later than max after the gather
that started it. With no job there, it adds one of these items; a
running one runs on with what it read, and the items gathered
meanwhile are its next run's alone once it ends well, at their settle
time, follow its own in its retry, and are a job of their own under
the id once it fails for good. count is what the next run takes. A done job
its dedupe holds keeps the id, and the gather adds nothing, count 0,
as add would; a failed one gives the id up, as to add. A job that
repeats, or a value that is not a list there or here, is invalid; a
list past a job's bound a limit. A gather needs an id, and its time
is its settle's: at, delay and a repeat are refused beside it.
JobCall.update
pub fn update(self, value: &V) -> Result<bool>Changes the id's job when it is not running: its value, the steps kept
for the old one dropped with it, and its time, group or repeat when the
call names them. Says whether it did: a job that runs, is done or failed,
or never was answers false.
Dedupe
pub struct Dedupe(_)How long a done job keeps its id taken, so that an add of it adds
nothing: a Duration from when it was done, or
Dedupe::calendar(Calendar::Day, "Europe/Moscow"), until the local day it
was done in ends. A day is 23 or 25 hours on the days the clock moves.
Dedupe.calendar
pub fn calendar(unit: Calendar, zone: &str) -> DedupeUntil the end of the local unit the job was done in, in zone, an
IANA name, "UTC" among them.
Gather
pub struct Gather { /* private fields */ }How a gather waits: settle after the last item, and never past max
after the first; Gather::settle(5 * MINUTE).max(HOUR).
Gather.settle
pub fn settle(span: Duration) -> GatherThe job runs span after the last gather under its id.
Gather.max
pub fn max(self, span: Duration) -> GatherThe job runs at the latest span after the gather that started it,
however many follow.
Gathered
pub struct Gathered {
pub added: bool,
pub count: u64,
pub at: SystemTime,
}What a gather did: whether it started the job, how many items the job holds now, and when it is due.
Maintenance
pub struct Maintenance {
pub failed: usize,
pub done: usize,
pub ids: usize,
pub expired: usize,
}What one maintenance pass removed.
maintain
pub fn maintain(store: &Store) -> Result<Maintenance>Removes what the queues this process opened keep no longer, 10,000 rows a transaction. The store runs it every minute; a store opened without background work leaves it to the caller.
Concurrency
pub struct Concurrency { /* private fields */ }How many jobs run at once: in all, across every worker of the store, and
in each group. Concurrency::total(8).group(2) bounds both;
Concurrency::default().group(2) each group alone.
Concurrency.total
pub fn total(jobs: u32) -> ConcurrencyConcurrency.group
pub fn group(self, jobs: u32) -> ConcurrencyAt most jobs of each group run at once, a group being what an add
named; one customer's backlog cannot hold back the others.
All
pub struct All<V> { /* private fields */ }Every job a filter names, a page at a time; a job whose value no longer reads is its error, and the walk goes on past it.
Claimed
pub struct Claimed<V> {
pub value: V,
}A job a program claimed for a loop of its own: its value, its run, and the lease it settles.
Claimed.run
pub fn run(&self) -> &RunClaimed.settle
pub fn settle(self, outcome: impl Into<Outcome>) -> Result<()>Settles the job by outcome, as a handler's answer does: () for done,
or one of the run's answers. A job whose lease ended and that another
claim, a set or an update took since, or that a cancel took, is a
conflict, and the settlement changes nothing.
Queue
pub struct Queue<V> { /* private fields */ }Jobs of one type in the order of their time. A handle is cheap to clone and to keep.
Queue.adding
pub fn adding(&self, value: &V) -> WriteA job's add for a batch of the database the queue was opened from:
in Database::batch it commits with the batch's rows or not at
all, and shares the commit with every other write of the file, where
Database::tx holds the writer and pays a sync of its own. It adds
as add does, unless its id is taken; a
queue kept in another file fails the batch, Invalid.
let db = store.database("app").open()?;
let indexing = db.queue::<Index>("indexing").open()?;
db.batch([
tinystore::sql!("update notes set version = version + 1 where id = ?", note).into(),
indexing.adding(&Index { note }),
])?;Queue.add
pub fn add(&self, value: &V) -> Result<bool>Adds a job that runs as soon as a worker is free, and returns once it is on disk. Adds from many callers share a commit.
Queue.set
pub fn set(&self, id: impl AsRef<str>, value: &V) -> Result<()>Makes the job under id this value, due now, whatever it was; see
JobCall::set.
Queue.update
pub fn update(&self, id: impl AsRef<str>, value: &V) -> Result<bool>Gives the job under id this value when it is not running, the steps
kept for the old one dropped with it, and says whether it did; see
JobCall::update.
Queue.upsert
pub fn upsert(&self, id: impl AsRef<str>, value: &V) -> Result<bool>Adds the job under id with this value, or brings the one there to
it, its time, attempts and steps as they were, and says whether it
added or changed anything; see JobCall::upsert.
Queue.gather
pub fn gather(&self, id: impl AsRef<str>, items: &V, gather: Gather) -> Result<Gathered>Gathers items, a list, into the job under id, which runs settle
after the last gather and never later than max after the first; see
JobCall::gather.
Queue.cancel
pub fn cancel(&self, job: impl JobRef) -> Result<bool>Takes a job, whatever its state, and says whether it was there: the
job under an id, "welcome-42", or a job a page gave, &job, by its
id or, for one without, by the row it was listed from. A job retried,
run again for its repeat or cancelled since it was listed is in no
row, and a cancel of it takes nothing, never another job.
A running one's handler is told to stop: run.stopped() turns true,
and what it returns settles nothing. A repeating job stops.
Queue.counts
pub fn counts(&self) -> Result<Counts>How many jobs the queue holds in each state, from one snapshot; it reads no value.
Queue.get
pub fn get(&self, id: impl AsRef<str>) -> Result<Option<Job<V>>>Where the job under id is: scheduled, waiting and how many jobs are
ahead of it, running and its progress, failed and why, or done while the
queue's dedupe keeps its id; none for an id with no job.
Queue.list
pub fn list(&self, filter: &Filter) -> Result<Page<V>>A page of the queue's jobs; see Filter.
Queue.all
pub fn all(&self, filter: Filter) -> All<V>Every job filter names, a page at a time, holding nothing between
pages; a job written during the walk may or may not be met.
Queue.id
pub fn id(&self, id: impl AsRef<str>) -> JobCall<'_, V>A call on the job with this id; see JobCall.
Queue.at
pub fn at(&self, time: SystemTime) -> JobCall<'_, V>Queue.delay
pub fn delay(&self, span: Duration) -> JobCall<'_, V>Queue.group
pub fn group(&self, group: impl AsRef<str>) -> JobCall<'_, V>Queue.keep
pub fn keep(&self, span: Duration) -> JobCall<'_, V>Queue.work
pub fn work<F, R, E>(&self, handler: F) -> Result<Worker>
where
F: Fn(V, &Run) -> Result<R, E> + Send + Sync + 'static,
R: Into<Outcome>,
E: Display,Runs handler on each job as it falls due, on threads the store starts
for it, as many at once as the queue's concurrency lets, one when it
sets none. It returns at once; see Worker.
The handler gets the job's value and its run. Ok(()) is done; an
answer of the run's says otherwise; an error retries the job after its
backoff, its text kept as the job's error, and so does a panic.
Queue.run_due
pub fn run_due<F, R, E>(&self, handler: F) -> Result<usize>
where
F: Fn(V, &Run) -> Result<R, E> + Sync,
R: Into<Outcome>,
E: Display,Runs what is due when it starts and what falls due meanwhile, then returns how many jobs ran; it never waits for a later job. Tests, scripts and commands use it.
Queue.concurrency
pub fn concurrency(&self, handlers: u32) -> Workers<'_, V>A worker that runs at most handlers at once, under the queue's
concurrency.
Queue.claim
pub fn claim(&self) -> Result<Option<Claimed<V>>>Takes the next due job for a loop of the program's own, which settles
it with Claimed::settle; none when nothing is due. A job not settled
within the queue's timeout goes back to the queue.
Queue.watch
pub fn watch(&self, id: impl AsRef<str>) -> Watch<V>The job under id as it is, then again each time its state, place,
attempt, time, progress or error changes; it ends with the job: done,
failed or cancelled, the last it yields. An id with no job yields
nothing.
QueueBuilder
pub struct QueueBuilder<V> { /* private fields */ }A queue being opened: its name, its type, and how it treats its jobs.
QueueBuilder.attempts
pub fn attempts(self, attempts: u32) -> SelfRuns a job gets before it fails for good, the first counted: 10.
QueueBuilder.backoff
pub fn backoff(self, initial: Duration, max: Duration) -> SelfThe wait before a retry: initial, doubling each time up to max, a
tenth longer or shorter by the job: 1 s to 1 h.
QueueBuilder.timeout
pub fn timeout(self, timeout: Duration) -> SelfHow long one run may take before it is told to stop: a minute.
QueueBuilder.concurrency
pub fn concurrency(self, concurrency: impl Into<Concurrency>) -> SelfJobs running at once across every worker of the store, 8, or in all
and in each group, Concurrency::total(8).group(2). Without it each
worker runs one at a time.
QueueBuilder.rate
pub fn rate(self, count: u32, per: Duration) -> SelfJobs started in any span of per: 30 a second, as an API's limit asks.
It counts starts, retries among them, and lives in memory.
QueueBuilder.group_rate
pub fn group_rate(self, count: u32, per: Duration) -> SelfJobs of each group started in any span of per, beside the queue's
rate: each account's own limit at a provider, 2 a second a bot. A
job of a group past it waits for its group's turn, and the other
groups' jobs start meanwhile. It lives in memory, as rate does.
QueueBuilder.dedupe
pub fn dedupe(self, dedupe: impl Into<Dedupe>) -> SelfHow long a done job keeps its id taken, so that adding it again adds
nothing: a span from when it was done, or Dedupe::calendar(Calendar::Day, "Europe/Moscow"), until the local day it was done in ends. A job's own
wins over it.
QueueBuilder.keep
pub fn keep(self, span: Duration) -> SelfHow long a failed job stays, with its error, to be found and started
again: a week; Duration::ZERO keeps it not at all. A job's own wins
over it.
QueueBuilder.quiet
pub fn quiet(self, quiet: Quiet) -> SelfThe hours in which the queue's jobs do not run: a time the store gives a job inside them, its first or a retry's, a repeat's or a gather's, becomes their end. A job's own, given to its call, wins over it.
QueueBuilder.max_waiting
pub fn max_waiting(self, jobs: u64) -> SelfJobs that may wait; an add past it is a limit: ten million.
QueueBuilder.open
pub fn open(self) -> Result<Queue<V>>Workers
pub struct Workers<'a, V> { /* private fields */ }A worker being started: how many handlers it runs at once.
Workers.work
pub fn work<F, R, E>(self, handler: F) -> Result<Worker>
where
F: Fn(V, &Run) -> Result<R, E> + Send + Sync + 'static,
R: Into<Outcome>,
E: Display,Starts the worker; see Queue::work.
Workers.run_due
pub fn run_due<F, R, E>(self, handler: F) -> Result<usize>
where
F: Fn(V, &Run) -> Result<R, E> + Sync,
R: Into<Outcome>,
E: Display,Runs what is due; see Queue::run_due.
Quiet
pub struct Quiet { /* private fields */ }Hours in which a queue's jobs, or one job, do not run: Quiet::hours("23:00", "07:00", "Europe/Moscow"), from 23:00 to 07:00 on Moscow's clock; a from
after to wraps midnight. Quiet::none() on a call holds its job by no
quiet hours, its queue's neither. The times and the zone are checked where
the queue opens or the call is made: a time not HH:MM, the two the same,
or a zone no one has is invalid.
Quiet.hours
pub fn hours(from: &str, to: &str, zone: &str) -> QuietQuiet.none
pub fn none() -> QuietCounts
pub struct Counts {
pub scheduled: u64,
pub waiting: u64,
pub running: u64,
pub failed: u64,
}How many jobs a queue holds in each state, from one snapshot. A job done is not counted: it left the queue.
Filter
pub struct Filter { /* private fields */ }Which jobs a page lists. With a prefix, those whose ids start with it, in
the byte order of their ids; a prefix is text: chat:4 meets chat 42 too,
so end it with a separator. With none, every job the queue holds: those
with ids in that order, then those without in the order they run, then
those without that failed, the last failed first. With no prefix and
Failed, the failed jobs alone, the last failed first.
Filter.prefix
pub fn prefix(prefix: impl Into<String>) -> FilterFilter.state
pub fn state(self, state: State) -> FilterThe jobs in state alone: scheduled, waiting, running or failed.
Filter.limit
pub fn limit(self, jobs: usize) -> FilterJobs a page holds at most: 100 unless said, 1000 at most.
Filter.after
pub fn after(self, next: impl Into<String>) -> FilterWhere the page before ended: its next.
Job
pub struct Job<V> {
pub id: Option<String>,
pub value: V,
pub state: State,
pub at: SystemTime,
pub attempt: u32,
pub ahead: u32,
pub ahead_at_least: bool,
pub progress: Option<Value>,
pub error: Option<String>,
pub group: Option<String>,
pub repeat: Option<String>,
pub last_run: Option<LastRun>,
pub expires_at: Option<SystemTime>,
}A job as its queue holds it.
JobRef
pub trait JobRefThe job a cancel takes: its id, "welcome-42", or a job a page gave,
&job, taken by its id, or by the row it was listed from when it has none.
LastRun
pub struct LastRun {
pub started_at: SystemTime,
pub ended_at: SystemTime,
}When the last run a handler finished began and ended.
PAGE_JOBS
pub const PAGE_JOBS: usizeThe most jobs a page holds.
Page
pub struct Page<V> {
pub jobs: Vec<Job<V>>,
pub next: Option<String>,
}One page of jobs, from one snapshot. A page of one state may read several, one for each window of rows it reads past the other states'.
State
pub enum State {
Scheduled,
Waiting,
Running,
Done,
Failed,
Cancelled,
}Where a job is in its life.
Outcome
pub struct Outcome(_)What a handler answers about its job. Ok(()) is done; the run's retry,
snooze and fail make the others.
Run
pub struct Run { /* private fields */ }This call on a job: when it was due, which attempt it is, whether it was told to stop, and the answers a handler returns to say what happens next.
Run.id
pub fn id(&self) -> Option<&str>The id the job was added under; none for a job added without one.
Run.at
pub fn at(&self) -> SystemTimeWhen the run was due, which is earlier than now when it started late: a handler that reads "the last day" counts it from here.
Run.attempt
pub fn attempt(&self) -> u32This run's number among the job's attempts, the first being 1.
Run.group
pub fn group(&self) -> Option<&str>Run.stopped
pub fn stopped(&self) -> boolWhether the run was told to stop: a cancel took its job, or it ran past its queue's timeout. A handler that checks it between pieces of work stops in time; what it did before stays done.
Run.set_progress
pub fn set_progress(&self, progress: &impl Serialize) -> Result<()>Shows how far the run got to get and watch: any JSON up to 4 KiB, a
fraction or a count as the application likes. It lives in memory, and a
run a restart ends starts again from nothing.
Run.step
pub fn step<T, E>(&self, name: &str, work: impl FnOnce() -> Result<T, E>) -> Result<T, E>
where
T: Serialize + DeserializeOwned,
E: From<Error>,Runs work once for the job under name and keeps what it answered:
the attempt after a retry, a lost worker or a restart gets the kept
answer back without running work again. A step whose attempt ends
before its answer is kept runs again, so what work does outside the
store should bear doing twice.
Run.done
pub fn done(&self) -> OutcomeThe answer that says the job is done, for a handler whose other
branches answer retry or fail; a handler that answers nothing else
returns Ok(()) instead.
Run.retry
pub fn retry(&self, when: impl Into<When>) -> OutcomeRuns the job again at when, the run counted as an attempt; past the
queue's attempts it fails for good.
Run.snooze
pub fn snooze(&self, when: impl Into<When>) -> OutcomeRuns the job again at when, the run not counted: a provider that asks
for a minute, a row that says later.
Run.fail
pub fn fail(&self, reason: impl Display) -> OutcomeFails the job for good now, reason kept as its error.
When
pub enum When {
After(Duration),
At(SystemTime),
}When a retry or a snooze runs the job again: a span from now, or a time.
Schedule
pub struct Schedule { /* private fields */ }A repeat the code owns, and the worker running it when it was opened with one.
Schedule.get
pub fn get(&self) -> Result<Option<Job<()>>>When it runs next and how its last run went: what a page of cron jobs shows.
Schedule.run_due
pub fn run_due<F, R, E>(&self, handler: F) -> Result<usize>
where
F: Fn(&Run) -> Result<R, E> + Sync,
R: Into<Outcome>,
E: Display,Runs what is due, as Queue::run_due does.
Schedule.stop
pub fn stop(&self)Stops the schedule's worker, as Worker::stop does.
Schedule.cancel
pub fn cancel(&self) -> Result<bool>Removes the schedule's job, until the program opens it again.
Schedule.run_now
pub fn run_now(&self) -> Result<()>Runs the schedule once as soon as a worker is free, under its own
rules, and leaves its later times as they were: run at 03:29, a
schedule of 03:30 runs again at 03:30. A run under way is followed by
one more right after it, never beside it; a run waiting for a retry
runs now, its attempts going on. A schedule whose job a cancel took is
not_found until the program opens it again.
ScheduleBuilder
pub struct ScheduleBuilder { /* private fields */ }A schedule being opened: its name and its repeat.
ScheduleBuilder.every
pub fn every(self, span: Duration) -> SelfRuns every span, at the schedule's own phase within it.
ScheduleBuilder.cron
pub fn cron(self, expr: &str, zone: &str) -> SelfRuns by a five-field cron expression on the wall clock of zone, an
IANA name, "UTC" among them.
ScheduleBuilder.attempts
pub fn attempts(self, attempts: u32) -> SelfRuns a run gets before it fails, the first counted: 10. A failed run retries, never past the next time, and the schedule goes on.
ScheduleBuilder.backoff
pub fn backoff(self, initial: Duration, max: Duration) -> SelfScheduleBuilder.timeout
pub fn timeout(self, timeout: Duration) -> SelfScheduleBuilder.work
pub fn work<F, R, E>(self, handler: F) -> Result<Schedule>
where
F: Fn(&Run) -> Result<R, E> + Send + Sync + 'static,
R: Into<Outcome>,
E: Display,Opens the schedule and runs handler at each time on a worker of the
store's, as Queue::work does; the handler gets the run alone.
ScheduleBuilder.open
pub fn open(self) -> Result<Schedule>Opens the schedule without running it: for a program that reads where
it is, or a test that runs it with run_due.
TxQueue
pub struct TxQueue<'a, V> { /* private fields */ }A queue's calls inside a transaction, tx.with(&queue): its writes and
its reads of one job. A worker, a watch and a page read outside it.
TxQueue.add
pub fn add(&self, value: &V) -> Result<bool>Adds a job, as Queue::add does, once the transaction commits.
TxQueue.set
pub fn set(&self, id: impl AsRef<str>, value: &V) -> Result<()>TxQueue.update
pub fn update(&self, id: impl AsRef<str>, value: &V) -> Result<bool>TxQueue.upsert
pub fn upsert(&self, id: impl AsRef<str>, value: &V) -> Result<bool>TxQueue.gather
pub fn gather(&self, id: impl AsRef<str>, items: &V, gather: Gather) -> Result<Gathered>Gathers items into the job under id, as Queue::gather does, once
the transaction commits.
TxQueue.cancel
pub fn cancel(&self, job: impl JobRef) -> Result<bool>Takes the job under an id, or a job a page gave, as Queue::cancel
does; a running job's handler is told to stop once the transaction
commits.
TxQueue.get
pub fn get(&self, id: impl AsRef<str>) -> Result<Option<Job<V>>>The job under id as the transaction sees it, its own writes among it.
TxQueue.id
pub fn id(&self, id: impl AsRef<str>) -> JobCall<'a, V>TxQueue.at
pub fn at(&self, time: SystemTime) -> JobCall<'a, V>TxQueue.delay
pub fn delay(&self, span: Duration) -> JobCall<'a, V>TxQueue.group
pub fn group(&self, group: impl AsRef<str>) -> JobCall<'a, V>TxQueue.keep
pub fn keep(&self, span: Duration) -> JobCall<'a, V>Watch
pub struct Watch<V> { /* private fields */ }A job watched from Rust: the job as it is, then again each time it
changes, until it ends; each next waits on the calling thread. Dropped,
it stops watching.
Worker
pub struct Worker { /* private fields */ }A worker running a queue's handlers on threads the store started for it.
Dropped, it keeps running until the store closes; stop stops it.
Worker.stop
pub fn stop(&self)Takes no job more and waits for the handlers under way, each bounded by its queue's timeout; the store's close stops every worker so. Called from one of its own handlers, it returns at once, and the worker ends once its handlers have returned and their answers are written.
aio::jobs::All
pub struct All<V> { /* private fields */ }Every job a filter names, a page at a time: a Stream whose next page
is read on the store's pool once the last one is used up. Dropped, it
stops, and so does a page under way. A job whose value no longer reads is
its error, and the walk goes on past it.
aio::jobs::Claimed
pub struct Claimed<V> {
pub value: V,
}A job a program claimed for a loop of its own: its value, its run, and the lease it settles.
aio::jobs::Claimed.run
pub fn run(&self) -> &Runaio::jobs::Claimed.settle
pub fn settle(
self,
outcome: impl Into<Outcome>,
) -> impl Future<Output = Result<()>> + Send + 'staticSettles the job by outcome, as a handler's answer does: () for done,
or one of the run's answers. Its write is queued at jobs.db's writer at
the first poll, and the job is let go as it commits, whether or not the
future is still awaited. A job whose lease ended and that another
claim, a change or a cancel took since is a conflict.
aio::jobs::JobCall
pub struct JobCall<'a, V> { /* private fields */ }A call on one job with what it asks beside its value, run by its last
step, as jobs::JobCall: later.id(&key).at(nine_am).add(&draft).await.
aio::jobs::JobCall.id
pub fn id(self, id: impl AsRef<str>) -> SelfThe job's id, 1 to 1024 bytes of text: what finds it again, and what adds it once however many times its caller asks.
aio::jobs::JobCall.at
pub fn at(self, time: SystemTime) -> SelfRuns the job at time; a time in the past runs it now.
aio::jobs::JobCall.delay
pub fn delay(self, span: Duration) -> SelfRuns the job span from now.
aio::jobs::JobCall.group
pub fn group(self, group: impl AsRef<str>) -> SelfThe group whose running jobs the queue's concurrency bounds.
aio::jobs::JobCall.dedupe
pub fn dedupe(self, dedupe: impl Into<Dedupe>) -> SelfHow long this job holds its id once done; see jobs::JobCall::dedupe.
aio::jobs::JobCall.keep
pub fn keep(self, span: Duration) -> SelfHow long this job is kept once it failed for good; see jobs::JobCall::keep.
aio::jobs::JobCall.ttl
pub fn ttl(self, span: Duration) -> SelfThe job leaves its queue span after its last write unless written
again; see jobs::JobCall::ttl.
aio::jobs::JobCall.quiet
pub fn quiet(self, quiet: Quiet) -> SelfThe hours in which this job does not run, in the place of its queue's;
see jobs::JobCall::quiet.
aio::jobs::JobCall.expires_at
pub fn expires_at(self, time: SystemTime) -> SelfThe job leaves its queue at time; see jobs::JobCall::expires_at.
aio::jobs::JobCall.every
pub fn every(self, span: Duration) -> SelfRepeats the job every span; see jobs::JobCall::every.
aio::jobs::JobCall.cron
pub fn cron(self, expr: &str, zone: &str) -> SelfRepeats the job by a five-field cron expression on the wall clock of
zone, an IANA name, "UTC" among them.
aio::jobs::JobCall.add
pub fn add(self, value: &V) -> impl Future<Output = Result<bool>> + Send + 'staticAdds the job unless its id is taken, and says whether it added one;
see jobs::JobCall::add. The call is checked and its value encoded
now, and its write queued at the first poll: polled once, it commits
whether or not it is still awaited.
aio::jobs::JobCall.set
pub fn set(self, value: &V) -> impl Future<Output = Result<()>> + Send + 'staticMakes the id's job this value at this time, whatever it was; see
jobs::JobCall::set.
aio::jobs::JobCall.update
pub fn update(self, value: &V) -> impl Future<Output = Result<bool>> + Send + 'staticChanges the id's job when it is not running, the steps kept for the old
value dropped with it, and says whether it did; see
jobs::JobCall::update.
aio::jobs::JobCall.upsert
pub fn upsert(self, value: &V) -> impl Future<Output = Result<bool>> + Send + 'staticAdds the job when its id is free, or brings the one there to this
call, and says whether it added or changed anything; see
jobs::JobCall::upsert.
aio::jobs::JobCall.gather
pub fn gather(
self,
items: &V,
gather: Gather,
) -> impl Future<Output = Result<Gathered>> + Send + 'staticGathers items into the id's job, and says what it did; see
jobs::JobCall::gather. Checked now, queued at the first poll.
aio::jobs::JobCall.adding
pub fn adding(self, value: &V) -> WriteThe job's add for a batch of the database the queue was opened from,
with what the call asks beside its value; see
Queue::adding.
aio::jobs::Queue
pub struct Queue<V> { /* private fields */ }Jobs of one type in the order of their time, as jobs::Queue, its
calls that wait awaited. A handle is cheap to clone and to keep.
aio::jobs::Queue.blocking
pub fn blocking(&self) -> &Queue<V>The same queue, for its blocking calls.
aio::jobs::Queue.add
pub fn add(&self, value: &V) -> impl Future<Output = Result<bool>> + Send + 'staticAdds a job that runs as soon as a worker is free, and answers once it is on disk; adds from many callers share a commit. Its write is queued at jobs.db's writer at the first poll, so a future polled once commits whether or not it is still awaited.
aio::jobs::Queue.set
pub fn set(
&self,
id: impl AsRef<str>,
value: &V,
) -> impl Future<Output = Result<()>> + Send + 'staticMakes the job under id this value, due now, whatever it was; see
JobCall::set.
aio::jobs::Queue.update
pub fn update(
&self,
id: impl AsRef<str>,
value: &V,
) -> impl Future<Output = Result<bool>> + Send + 'staticGives the job under id this value when it is not running, the steps
kept for the old one dropped with it, and says whether it did; see
JobCall::update.
aio::jobs::Queue.upsert
pub fn upsert(
&self,
id: impl AsRef<str>,
value: &V,
) -> impl Future<Output = Result<bool>> + Send + 'staticAdds the job under id, or brings the one there to this value, its
time, attempts and steps as they were; see JobCall::upsert.
aio::jobs::Queue.gather
pub fn gather(
&self,
id: impl AsRef<str>,
items: &V,
gather: Gather,
) -> impl Future<Output = Result<Gathered>> + Send + 'staticGathers items, a list, into the job under id, which runs settle
after the last gather and never later than max after the first; see
jobs::JobCall::gather.
aio::jobs::Queue.cancel
pub fn cancel(&self, job: impl JobRef) -> impl Future<Output = Result<bool>> + Send + 'staticTakes a job, whatever its state, and says whether it was there: the
job under an id, or a job a page gave, as jobs::Queue::cancel
does. A running one's handler is told to stop: run.stopped() turns
true, and what it returns settles nothing. A repeating job stops.
aio::jobs::Queue.counts
pub fn counts(&self) -> impl Future<Output = Result<Counts>> + Send + 'staticHow many jobs the queue holds in each state, from one snapshot, read on
the store's pool; see jobs::Queue::counts.
aio::jobs::Queue.get
pub fn get(
&self,
id: impl AsRef<str>,
) -> impl Future<Output = Result<Option<Job<V>>>> + Send + 'staticWhere the job under id is, as jobs::Queue::get says; none for an
id with no job. It reads on the store's pool, since it counts up to
10,000 jobs ahead of a waiting one.
aio::jobs::Queue.list
pub fn list(&self, filter: &Filter) -> impl Future<Output = Result<Page<V>>> + Send + 'staticA page of the queue's jobs, read on the store's pool; see Filter.
aio::jobs::Queue.all
pub fn all(&self, filter: Filter) -> All<V>Every job filter names, a page at a time, each page read on the
store's pool once the one before is used up and nothing held between
pages; a job written during the walk may or may not be met.
aio::jobs::Queue.id
pub fn id(&self, id: impl AsRef<str>) -> JobCall<'_, V>A call on the job with this id; see JobCall.
aio::jobs::Queue.at
pub fn at(&self, time: SystemTime) -> JobCall<'_, V>aio::jobs::Queue.delay
pub fn delay(&self, span: Duration) -> JobCall<'_, V>aio::jobs::Queue.group
pub fn group(&self, group: impl AsRef<str>) -> JobCall<'_, V>aio::jobs::Queue.keep
pub fn keep(&self, span: Duration) -> JobCall<'_, V>aio::jobs::Queue.claim
pub fn claim(&self) -> impl Future<Output = Result<Option<Claimed<V>>>> + Send + 'staticTakes the next due job for a loop of the program's own, which settles
it with Claimed::settle; none when nothing is due, never waiting
for a job. Its lease is a write at jobs.db's writer, and a value kept
in a row of its own is read after the commit, in the poll when a
reader is idle. Dropped once its write was queued and before it
answered, it gives the job back as a stopping worker does, due at once
and its attempt not counted.
aio::jobs::Queue.watch
pub fn watch(&self, id: impl AsRef<str>) -> Watch<V>The job under id as it is, then again each time its state, place,
attempt, time, progress or error changes, as a Stream; it ends
with the job: done, failed or cancelled, the last it yields. An id
with no job yields nothing.
Each commit of the queue wakes the watch, which reads its job again on the store's pool, one read at a time, and once more after a read when a change came meanwhile. Unlike the blocking watch it does not look again every minute: a job whose time comes is seen when a worker takes it. Dropped, it stops watching, and a read under way stops.
aio::jobs::Queue.work
pub fn work<F, W, R, E>(&self, handler: F) -> Worker
where
F: Fn(V, Run) -> W + Send + Sync + 'static,
W: Future<Output = Result<R, E>> + Send + 'static,
R: Into<Outcome> + 'static,
E: Display + 'static,A worker that runs handler on each job as it falls due, as many at
once as the queue's concurrency lets, one when it sets none: a future
the program spawns, which starts nothing before its first poll and
runs until Worker::until's signal, its drop or the store's close;
the page of tinystore::aio::jobs says what each does.
The handler gets the job's value and its run by value. Ok(()) is
done; an answer of the run's says otherwise; an error retries the job
after its backoff, its text kept as the job's error, and so does a
panic.
aio::jobs::Queue.run_due
pub fn run_due<F, W, R, E>(
&self,
handler: F,
) -> impl Future<Output = Result<usize>> + Send + 'static
where
F: Fn(V, Run) -> W + Send + Sync + 'static,
W: Future<Output = Result<R, E>> + Send + 'static,
R: Into<Outcome> + 'static,
E: Display + 'static,Runs what is due when its future is first polled and what falls due meanwhile, then answers how many jobs ran; it never waits for a later job. Tests, scripts and commands use it. Its future is a worker's, and dropped it stops as a worker's drop does.
aio::jobs::Queue.concurrency
pub fn concurrency(&self, handlers: u32) -> Workers<'_, V>A worker that runs at most handlers at once, under the queue's
concurrency.
aio::jobs::Queue.adding
pub fn adding(&self, value: &V) -> WriteA job's add for a batch of the database the queue was opened from, as
Queue::adding makes one: it checks the
call and encodes its value, with no I/O, and the batch is awaited.
aio::jobs::QueueBuilder
pub struct QueueBuilder<V> { /* private fields */ }A queue being opened, as jobs::QueueBuilder: its name, its type, and
how it treats its jobs.
aio::jobs::QueueBuilder.attempts
pub fn attempts(self, attempts: u32) -> SelfRuns a job gets before it fails for good, the first counted: 10.
aio::jobs::QueueBuilder.backoff
pub fn backoff(self, initial: Duration, max: Duration) -> SelfThe wait before a retry: initial, doubling each time up to max, a
tenth longer or shorter by the job: 1 s to 1 h.
aio::jobs::QueueBuilder.timeout
pub fn timeout(self, timeout: Duration) -> SelfHow long one run may take before it is told to stop: a minute.
aio::jobs::QueueBuilder.concurrency
pub fn concurrency(self, concurrency: impl Into<Concurrency>) -> SelfJobs running at once across every worker of the store, 8, or in all
and in each group, Concurrency::total(8).group(2). Without it each
worker runs one at a time.
aio::jobs::QueueBuilder.rate
pub fn rate(self, count: u32, per: Duration) -> SelfJobs started in any span of per: 30 a second, as an API's limit asks.
It counts starts, retries among them, and lives in memory.
aio::jobs::QueueBuilder.dedupe
pub fn dedupe(self, dedupe: impl Into<Dedupe>) -> SelfHow long a done job keeps its id taken, so that adding it again adds
nothing: a span, or a local calendar's day, week or month; see
jobs::QueueBuilder::dedupe.
aio::jobs::QueueBuilder.keep
pub fn keep(self, span: Duration) -> SelfHow long a failed job stays, with its error, to be found and started
again: a week; Duration::ZERO keeps it not at all.
aio::jobs::QueueBuilder.quiet
pub fn quiet(self, quiet: Quiet) -> SelfThe hours in which the queue's jobs do not run; see jobs::QueueBuilder::quiet.
aio::jobs::QueueBuilder.max_waiting
pub fn max_waiting(self, jobs: u64) -> SelfJobs that may wait; an add past it is a limit: ten million.
aio::jobs::QueueBuilder.open
pub fn open(self) -> Result<Queue<V>>Opens the queue, a plain call made once at start: the first open of the engine migrates jobs.db and gives back what a dead process held.
aio::jobs::Run
pub struct Run { /* private fields */ }This call on a job, as jobs::Run: when it was due, which attempt it
is, whether it was told to stop, and the answers a handler returns. A
handler takes it by value, so that its future may move to another thread.
aio::jobs::Run.id
pub fn id(&self) -> Option<&str>The id the job was added under; none for a job added without one.
aio::jobs::Run.at
pub fn at(&self) -> SystemTimeWhen the run was due, which is earlier than now when it started late.
aio::jobs::Run.attempt
pub fn attempt(&self) -> u32This run's number among the job's attempts, the first being 1.
aio::jobs::Run.group
pub fn group(&self) -> Option<&str>aio::jobs::Run.stopped
pub fn stopped(&self) -> boolWhether the run was told to stop: a cancel took its job, or it ran past its queue's timeout. A handler that checks it between pieces of work stops in time; what it did before stays done.
aio::jobs::Run.set_progress
pub fn set_progress(&self, progress: &impl Serialize) -> Result<()>Shows how far the run got to get and watch: any JSON up to 4 KiB. It
lives in memory, so it is a plain call.
aio::jobs::Run.step
pub async fn step<T, E, F, W>(&self, name: &str, work: F) -> Result<T, E>
where
T: Serialize + DeserializeOwned,
E: From<Error>,
F: FnOnce() -> W,
W: Future<Output = Result<T, E>>,Runs work once for the job under name and keeps what it answered,
as jobs::Run::step does: the attempt after a retry, a lost worker
or a restart gets the kept answer back without running work again.
The kept answer is read in the first poll when one of jobs.db's
readers is idle, else on the store's pool, and work's future runs
inside this one. Dropped before work answers, nothing is kept and the
step runs again in the next attempt; dropped once its answer's keep was
queued at the writer, the keep commits.
aio::jobs::Run.done
pub fn done(&self) -> OutcomeThe answer that says the job is done, for a handler whose other
branches answer retry or fail.
aio::jobs::Run.retry
pub fn retry(&self, when: impl Into<When>) -> OutcomeRuns the job again at when, the run counted as an attempt; past the
queue's attempts it fails for good.
aio::jobs::Run.snooze
pub fn snooze(&self, when: impl Into<When>) -> OutcomeRuns the job again at when, the run not counted.
aio::jobs::Run.fail
pub fn fail(&self, reason: impl Display) -> OutcomeFails the job for good now, reason kept as its error.
aio::jobs::Schedule
pub struct Schedule { /* private fields */ }A repeat the code owns, as jobs::Schedule: one repeating job under the
schedule's name. It holds no worker: the program spawns the one
Schedule::work gives, which stops by Worker::until or a drop.
aio::jobs::Schedule.blocking
pub fn blocking(&self) -> &ScheduleThe same schedule, for its blocking calls.
aio::jobs::Schedule.get
pub fn get(&self) -> impl Future<Output = Result<Option<Job<()>>>> + Send + 'staticWhen it runs next and how its last run went, read on the store's pool.
aio::jobs::Schedule.work
pub fn work<F, W, R, E>(&self, handler: F) -> Worker
where
F: Fn(Run) -> W + Send + Sync + 'static,
W: Future<Output = Result<R, E>> + Send + 'static,
R: Into<Outcome> + 'static,
E: Display + 'static,The worker that runs handler at each time, as a queue's worker does
and gets the run alone: it starts at its first poll. On a copy of
jobs.db it ends there with unavailable, since the home's workers run
the schedule.
aio::jobs::Schedule.run_due
pub fn run_due<F, W, R, E>(
&self,
handler: F,
) -> impl Future<Output = Result<usize>> + Send + 'static
where
F: Fn(Run) -> W + Send + Sync + 'static,
W: Future<Output = Result<R, E>> + Send + 'static,
R: Into<Outcome> + 'static,
E: Display + 'static,Runs what is due, as Queue::run_due does, and answers how many runs
ran.
aio::jobs::Schedule.cancel
pub fn cancel(&self) -> impl Future<Output = Result<bool>> + Send + 'staticRemoves the schedule's job, until the program opens it again; a write queued at jobs.db's writer at the first poll.
aio::jobs::Schedule.run_now
pub fn run_now(&self) -> impl Future<Output = Result<()>> + Send + 'staticRuns the schedule once as soon as a worker is free, its later times as
they were, as jobs::Schedule::run_now does; a write queued at
jobs.db's writer at the first poll.
aio::jobs::ScheduleBuilder
pub struct ScheduleBuilder { /* private fields */ }A schedule being opened, as jobs::ScheduleBuilder: its name and its
repeat.
aio::jobs::ScheduleBuilder.every
pub fn every(self, span: Duration) -> SelfRuns every span, at the schedule's own phase within it.
aio::jobs::ScheduleBuilder.cron
pub fn cron(self, expr: &str, zone: &str) -> SelfRuns by a five-field cron expression on the wall clock of zone, an
IANA name, "UTC" among them.
aio::jobs::ScheduleBuilder.attempts
pub fn attempts(self, attempts: u32) -> SelfRuns a run gets before it fails, the first counted: 10. A failed run retries, never past the next time, and the schedule goes on.
aio::jobs::ScheduleBuilder.backoff
pub fn backoff(self, initial: Duration, max: Duration) -> Selfaio::jobs::ScheduleBuilder.timeout
pub fn timeout(self, timeout: Duration) -> Selfaio::jobs::ScheduleBuilder.open
pub fn open(self) -> Result<Schedule>Opens the schedule without running it, a plain call made once at
start: it writes the code's repeat, as jobs::ScheduleBuilder::open.
aio::jobs::ScheduleBuilder.work
pub fn work<F, W, R, E>(self, handler: F) -> Result<Worker>
where
F: Fn(Run) -> W + Send + Sync + 'static,
W: Future<Output = Result<R, E>> + Send + 'static,
R: Into<Outcome> + 'static,
E: Display + 'static,Opens the schedule and gives the worker that runs handler at each
time, for a program that never reads the schedule: the program spawns
it, as Schedule::work's. A copy of jobs.db runs no schedule.
aio::jobs::Watch
pub struct Watch<V> { /* private fields */ }A job watched: a Stream of the job as it is, then again each time it
changes, until it ends.
aio::jobs::Worker
pub struct Worker { /* private fields */ }A queue's worker: a future that runs its handlers, Ok(()) once it
stopped. It starts at its first poll; spawn it on the program's runtime.
aio::jobs::Worker.until
pub fn until(self, signal: impl Future + Send + 'static) -> WorkerStops the worker gracefully once signal is ready, as
tokio::signal::ctrl_c() is on a ctrl-c: it takes no job more, gives
back uncounted the jobs it claimed and had not begun, waits for the
handlers under way, each within the queue's timeout and a grace, and
returns once their answers are written. Given several, the first that
is ready stops it.
aio::jobs::Workers
pub struct Workers<'a, V> { /* private fields */ }A worker being made: how many handlers it runs at once.
aio::jobs::Workers.work
pub fn work<F, W, R, E>(self, handler: F) -> Worker
where
F: Fn(V, Run) -> W + Send + Sync + 'static,
W: Future<Output = Result<R, E>> + Send + 'static,
R: Into<Outcome> + 'static,
E: Display + 'static,The worker; see Queue::work.
aio::jobs::Workers.run_due
pub fn run_due<F, W, R, E>(self, handler: F) -> impl Future<Output = Result<usize>> + Send + 'static
where
F: Fn(V, Run) -> W + Send + Sync + 'static,
W: Future<Output = Result<R, E>> + Send + 'static,
R: Into<Outcome> + 'static,
E: Display + 'static,Runs what is due; see Queue::run_due.