1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
//! The async API's handles of the Jobs engine: the queues, schedules,
//! workers and runs of [`tinystore::jobs`](crate::jobs), each call that
//! waits awaited.
//!
//! ```no_run
//! # use std::time::Duration;
//! # #[derive(serde::Serialize, serde::Deserialize)]
//! # struct Email { to: String }
//! # async fn render(_: &Email) -> tinystore::Result<String> { Ok(String::new()) }
//! # async fn deliver(_: &Email, _: &str) -> tinystore::Result<()> { Ok(()) }
//! # async fn example() -> tinystore::Result<()> {
//! use tinystore::aio::Store;
//! use tinystore::aio::jobs::Run;
//!
//! async fn send(email: Email, run: Run) -> tinystore::Result<()> {
//! let body: String = run.step("render", || render(&email)).await?; // kept across attempts
//! deliver(&email, &body).await
//! }
//!
//! let store = Store::open("data", Default::default())?;
//! let emails = store.queue::<Email>("emails").open()?; // opening stays a plain call
//! emails.delay(Duration::from_secs(5)).add(&Email { to: "ada@example.com".into() }).await?;
//!
//! let (stop, stopped) = tokio::sync::oneshot::channel::<()>();
//! let worker = tokio::spawn(emails.work(send).until(stopped)); // a future the program spawns
//! let _ = stop.send(()); // no job more: the handlers under way finish, and their jobs settle
//! worker.await.expect("the worker's task")?;
//! store.close().await?;
//! # Ok(())
//! # }
//! ```
//!
//! What the calls answer is the blocking module's data: a
//! [`Job`](crate::jobs::Job), a [`Page`](crate::jobs::Page), and the
//! [`Filter`](crate::jobs::Filter) and [`Outcome`](crate::jobs::Outcome)
//! they take stay in [`tinystore::jobs`](crate::jobs).
//!
//! # Where the work runs
//!
//! | A call | Runs | Its future dropped once polled |
//! |--------------------------|----------------------------------------------------|---------------------------------|
//! | `add`, `set`, `update` | queued at jobs.db's writer; commit on the pool | commits; its answer is let go |
//! | `cancel`, `settle` | queued at jobs.db's writer; commit on the pool | commits; its answer is let go |
//! | `claim` | its lease a write, a spilled value a point read | commits; its lease runs out |
//! | `get`, `list` | on the store's pool | stops; its reader goes back |
//! | `all`, `watch`: streams | each read on the store's pool | stops; a read under way stops |
//! | `work`, `run_due` | handlers in the future; claims on a store thread | [below](#a-worker-is-a-future) |
//! | `run.step`'s kept answer | in the first poll when a reader is idle, else pool | answered in that poll, or stops |
//! | `run.step`'s keep | queued at jobs.db's writer; commit on the pool | commits |
//!
//! None of them commits, syncs the disk or waits on the thread that polls its
//! future. A claim dropped once its lease was written gives its job back when
//! the lease, the queue's timeout, runs out, the attempt counted, as a
//! blocking claim never settled does. The other calls, opening a queue or a
//! schedule among them, do no I/O or are made once at start, since a first
//! open may migrate jobs.db. Inside a transaction's closure, `tx.with(&queue)`
//! gives the blocking [`TxQueue`](crate::jobs::TxQueue)'s calls.
//!
//! # A worker is a future
//!
//! [`Queue::work`] returns a [`Worker`] at once and starts nothing: the
//! program spawns it, `tokio::spawn(queue.work(handler))`. Its first poll
//! starts the claim loop on a thread of the store's, which leases due jobs
//! and settles finished ones in one grouped write, as a blocking worker's
//! does. The handlers run inside the worker's future, on the program's
//! executor, as many at once as the worker's concurrency.
//!
//! - [`Worker::until`] stops it gracefully: once its signal is ready the
//! worker takes no job more, waits for the handlers under way, each within
//! the queue's timeout and a grace, and returns once their answers are
//! written. Jobs it claimed and had not started go back uncounted.
//! - Dropping it is the abort: the jobs claimed and not started go back
//! uncounted, and those whose handler's future it dropped run again after
//! their backoff, the attempt counted. The drop waits for nothing.
//! - The store's close stops it as `until` does, and it returns `Ok(())`.
//! - A cancel of a running job turns `run.stopped()` true, and what its
//! handler returns settles nothing, as in the blocking API: the worker
//! does not drop the handler's future.
//!
//! # Closing
//!
//! A store's close waits for the handlers its workers run, which only the
//! program's executor polls. `store.close().await` closes on a thread of its
//! own while the executor goes on polling them. A blocking close, or the drop
//! of the store's last handle, on the executor's only thread waits instead
//! for the queue's timeout and ten seconds more, and the jobs under way run
//! again once their leases end.
use Arc;
use Serialize;
use DeserializeOwned;
pub use ;
pub use Run;
pub use ;
pub use Watch;
pub use ;
use cratePool;
/// The pool of the store a blocking queue was opened from, or one stopped
/// already when that store is gone, so that every call answers `Closed`.