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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
//! Background jobs for id_effect services: a Postgres `SKIP LOCKED` queue on `id_effect_sql`,
//! workers, retries with backoff and cron schedules.
//!
//! - A **[`Job`]** is a `#[derive(Schema)]` struct with a stable [`Job::NAME`], a queue, a
//! priority, a maximum number of attempts, an optional [`UniqueKey`] and a retry
//! [`Schedule`](id_effect::Schedule). Its payload is stored as `jsonb` through its schema.
//! - The **[`JobQueue`]** capability enqueues ([`JobQueueExt::enqueue`], [`enqueue`]) and
//! administers jobs; [`JobStore`] adds the worker side (claim, fenced outcomes, heartbeat,
//! rescue). [`PgJobQueue`] stores them in the table `id_effect_jobs` ([`migrator`],
//! [`PgJobQueue::ensure_schema`]), [`MemoryJobQueue`] in memory for tests.
//! - **[`enqueue_in`]** inserts a job with the caller's
//! [`SqlTransaction`](id_effect_sql::SqlTransaction), so it commits or rolls back with the
//! caller's work.
//! - A **[`Worker`]** runs the handlers of a [`JobRegistry`] with a concurrency limit per queue,
//! a heartbeat and lease, rescues the jobs of crashed workers, and drains on shutdown.
//! - With the feature `cron`, **[`enqueue_on_tick`]** makes a core cron entry enqueue one job per
//! tick (deduplicated across schedulers by [`tick_key`]), and **[`CronLeader`]** runs the
//! schedule on the node holding a cluster singleton.
//!
//! Every time the queue reads (`run_at`, leases, `finished_at`) comes from a
//! [`WallClock`](id_effect::WallClock), so tests run on virtual time. Delivery is at least once:
//! a job whose worker died mid-run runs again, so handlers must be idempotent.
//!
//! [`JobState`] is the lifecycle every queued job moves through; [`JobState::as_str`] is the value
//! stored in the queue table's `state` column.
//!
//! ```
//! use id_effect_jobs::JobState;
//!
//! let state: JobState = "failed".parse().unwrap();
//! assert!(!state.is_terminal());
//! assert_eq!(state.as_str(), "failed");
//! ```
//!
//! A job, its handler, and a worker draining the queue on a test executor:
//!
//! ```
//! use std::sync::Arc;
//! use std::sync::atomic::{AtomicU32, Ordering};
//!
//! use id_effect::{Executor, Schema, TestExecutor, UtcDateTime, WallClock, hlist, sync};
//! use id_effect_jobs::{
//! Job, JobFailure, JobOptions, JobQueueExt, JobQueueService, JobRegistry, MemoryJobQueue, Worker,
//! };
//!
//! #[derive(Clone, Schema)]
//! struct Welcome {
//! email: String,
//! }
//!
//! impl Job for Welcome {
//! const NAME: &'static str = "app.welcome";
//! }
//!
//! let sent = Arc::new(AtomicU32::new(0));
//! let s = Arc::clone(&sent);
//! let registry = JobRegistry::new()
//! .register::<Welcome, JobFailure>(move |job, _ctx| {
//! let s = Arc::clone(&s);
//! sync(move || {
//! assert_eq!(job.email, "ada@example.com");
//! s.fetch_add(1, Ordering::SeqCst);
//! })
//! })
//! .unwrap();
//!
//! let exec = TestExecutor::new();
//! let clock = WallClock::anchored(exec.now(), UtcDateTime::unsafe_make(1_800_000_000_000));
//! let store = MemoryJobQueue::new(clock);
//! let env = hlist![JobQueueService::new(store.clone())];
//! let job = Welcome { email: "ada@example.com".into() };
//! let program = store
//! .enqueue(job, JobOptions::default())
//! .flat_map(move |_| Worker::new(registry).run_until_idle().provide_all(env));
//! let done = program.fork_root(Arc::new(exec.clone()));
//! exec.run_until_stalled();
//! assert!(done.is_done());
//! assert_eq!(sent.load(Ordering::SeqCst), 1);
//! ```
//!
//! Tier `platform` (ADR 0010 Amendment 6): the Ledger reference app under
//! `examples/reference/` depends on it.
use fmt;
use FromStr;
pub use ;
/// What [`CronLeader::new`] is built from: the singleton and its PostgreSQL lease
/// (`id_effect_node`), and the local process id (`id_effect_process`), so a service reaches them
/// through this crate.
pub use ;
pub use ;
pub use ;
pub use ;
pub use ;
pub use ;
pub use ;
/// Lifecycle of a queued job.
///
/// `scheduled → running → succeeded`; a failed attempt goes `running → failed → running …` until
/// it succeeds or its attempts are exhausted (`dead`). `scheduled` and `failed` jobs can be
/// `cancelled`; `dead` and `cancelled` jobs can be retried by hand, and a `failed` job's next
/// attempt brought forward ([`JobQueue::retry_now`]).
/// A `state` column value that names no [`JobState`].
;
/// Compiles and runs the README's examples as doctests.
;