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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
/*!
# pg_task
FSM-based Resumable Postgres tasks
- **FSM-based** - each task is a granular state machine
- **Resumable** - on error, after you fix the step logic or the external world,
the task is able to pick up where it stopped
- **Postgres** - a single table handles task scheduling, state
transitions, and error processing
## Table of Contents
- [Tutorial](#tutorial)
- [Defining Tasks](#defining-tasks)
- [Investigating Errors](#investigating-errors)
- [Listening for Task Errors](#listening-for-task-errors)
- [Fixing the World](#fixing-the-world)
- [Scheduling Tasks](#scheduling-tasks)
- [Running Workers](#running-workers)
- [Stopping Workers](#stopping-workers)
- [Delaying Steps](#delaying-steps)
- [Retrying Steps](#retrying-steps)
## Tutorial
_The full runnable code is in [examples/tutorial.rs][tutorial-example]._
### Defining Tasks
We create a greeter task consisting of two steps:
```rust
# use async_trait::async_trait;
# use pg_task::{NextStep, Step, StepResult};
# use serde::{Deserialize, Serialize};
# use sqlx::PgPool;
#[derive(Debug, Deserialize, Serialize)]
pub struct ReadName {
filename: String,
}
# #[derive(Debug, Deserialize, Serialize)]
# pub struct SayHello {
# name: String,
# }
# pg_task::task!(Greeter { ReadName, SayHello });
# #[async_trait]
# impl Step<Greeter> for SayHello {
# async fn step(self, _db: &PgPool) -> StepResult<Greeter> {
# NextStep::none()
# }
# }
#[async_trait]
impl Step<Greeter> for ReadName {
const RETRY_LIMIT: i32 = 5;
async fn step(self, _db: &PgPool) -> StepResult<Greeter> {
let name = std::fs::read_to_string(&self.filename)?;
NextStep::now(SayHello { name })
}
}
```
The first step tries to read a name from a file:
- `filename` - the only state we need in this step
- `impl Step<Greeter> for ReadName` - our step is a part of a `Greeter` task
- `RETRY_LIMIT` - the step is fallible, let's retry it a few times
- `NextStep::now(SayHello { name })` - move our task to the `SayHello` step
right now
```rust
# use async_trait::async_trait;
# use pg_task::{NextStep, Step, StepResult};
# use serde::{Deserialize, Serialize};
# use sqlx::PgPool;
# #[derive(Debug, Deserialize, Serialize)]
# pub struct ReadName {
# filename: String,
# }
#[derive(Debug, Deserialize, Serialize)]
pub struct SayHello {
name: String,
}
# pg_task::task!(Greeter { ReadName, SayHello });
# #[async_trait]
# impl Step<Greeter> for ReadName {
# async fn step(self, _db: &PgPool) -> StepResult<Greeter> {
# NextStep::none()
# }
# }
#[async_trait]
impl Step<Greeter> for SayHello {
async fn step(self, _db: &PgPool) -> StepResult<Greeter> {
println!("Hello, {}", self.name);
NextStep::none()
}
}
```
The second step prints the greeting and finishes the task returning
`NextStep::none()`.
The [full code][tutorial-example] includes the remaining setup. Run it with:
```bash
cargo run --example tutorial
```
### Investigating Errors
The example logs 6 attempts: the first try plus `RETRY_LIMIT` retries. Inspect
the row to see what happened:
```bash
~$ psql pg_task -c 'table pg_task'
-[ RECORD 1 ]------------------------------------------------
id | cddf7de1-1194-4bee-90c6-af73d9206ce2
step | {"Greeter":{"ReadName":{"filename":"name.txt"}}}
wakeup_at | 2024-06-30 09:32:27.703599+06
tried | 6
locked_by |
lock_expires_at |
error | No such file or directory (os error 2)
created_at | 2024-06-30 09:32:22.628563+06
updated_at | 2024-06-30 09:32:27.703599+06
```
- a non-null `error` field indicates that the task has errored and contains the
error message
- the `step` field provides you with the information about a particular step and
its state when the error occurred
### Listening for Task Errors
Use [`listen_for_task_errors`] to receive live task error events:
```rust,no_run
# async fn demo(pool: sqlx::PgPool) -> pg_task::Result<()> {
let mut errors = pg_task::listen_for_task_errors(&pool).await?;
loop {
let task = errors.recv().await?;
eprintln!("task {} failed: {}", task.id, task.error);
}
# }
```
### Fixing the World
The task failed because the file is missing. Create it:
```bash
echo 'Fixed World' >name.txt
```
Clear `error` to rerun the task:
```bash
psql pg_task -c 'update pg_task set error = null'
```
The worker reruns the task and prints the greeting from the final step.
### Scheduling Tasks
Scheduling a task means inserting a row into the `pg_task` table. You can do it
from `psql` or from code in any language.
The crate also provides helpers for first-step serialization and scheduling:
- [`enqueue`] - to run the task immediately
- [`delay`] - to run it with a delay
- [`schedule`] - to schedule it to a particular time
### Running Workers
After [defining](#defining-tasks) the steps of each task, we need to wrap them
into enums representing whole tasks via [`task!`]:
```rust
# use async_trait::async_trait;
# use pg_task::{NextStep, Step};
# use sqlx::PgPool;
# #[derive(Debug, serde::Deserialize, serde::Serialize)]
# struct StepA;
# #[derive(Debug, serde::Deserialize, serde::Serialize)]
# struct StepB;
# #[derive(Debug, serde::Deserialize, serde::Serialize)]
# struct StepC;
pg_task::task!(Task1 { StepA, StepB });
pg_task::task!(Task2 { StepC });
# #[async_trait]
# impl Step<Task1> for StepA {
# async fn step(self, _db: &PgPool) -> pg_task::StepResult<Task1> {
# NextStep::none()
# }
# }
# #[async_trait]
# impl Step<Task1> for StepB {
# async fn step(self, _db: &PgPool) -> pg_task::StepResult<Task1> {
# NextStep::none()
# }
# }
# #[async_trait]
# impl Step<Task2> for StepC {
# async fn step(self, _db: &PgPool) -> pg_task::StepResult<Task2> {
# NextStep::none()
# }
# }
```
One more enum is needed to combine all the possible tasks:
```rust
# use async_trait::async_trait;
# use pg_task::{NextStep, Step};
# use sqlx::PgPool;
# #[derive(Debug, serde::Deserialize, serde::Serialize)]
# struct StepA;
# #[derive(Debug, serde::Deserialize, serde::Serialize)]
# struct StepB;
# #[derive(Debug, serde::Deserialize, serde::Serialize)]
# struct StepC;
# pg_task::task!(Task1 { StepA, StepB });
# pg_task::task!(Task2 { StepC });
# #[async_trait]
# impl Step<Task1> for StepA {
# async fn step(self, _db: &PgPool) -> pg_task::StepResult<Task1> {
# NextStep::none()
# }
# }
# #[async_trait]
# impl Step<Task1> for StepB {
# async fn step(self, _db: &PgPool) -> pg_task::StepResult<Task1> {
# NextStep::none()
# }
# }
# #[async_trait]
# impl Step<Task2> for StepC {
# async fn step(self, _db: &PgPool) -> pg_task::StepResult<Task2> {
# NextStep::none()
# }
# }
pg_task::scheduler!(Tasks { Task1, Task2 });
```
Now we can run the worker:
```rust
# async fn demo(db: sqlx::PgPool) -> pg_task::Result<()> {
# use async_trait::async_trait;
# use pg_task::{NextStep, Step};
# #[derive(Debug, serde::Deserialize, serde::Serialize)]
# struct StepA;
# pg_task::task!(Task1 { StepA });
# pg_task::scheduler!(Tasks { Task1 });
# #[async_trait]
# impl Step<Task1> for StepA {
# async fn step(self, _db: &sqlx::PgPool) -> pg_task::StepResult<Task1> {
# NextStep::none()
# }
# }
pg_task::Worker::<Tasks>::new(db).run().await?;
# Ok(())
# }
```
Workers coordinate through Postgres, so you can run one or many of them, either
in separate processes or with [`tokio::spawn`].
### Stopping Workers
Gracefully stop workers by sending a notification through the database:
```sql
SELECT pg_notify('pg_task_changed', 'stop_worker');
```
Workers finish their current steps before exiting. To wait for them, check for
live leases:
```sql
SELECT EXISTS(
SELECT 1
FROM pg_task
WHERE lock_expires_at > now()
);
```
### Delaying Steps
Sometimes you need to delay the next step. Using [`tokio::time::sleep`] before
returning the next step creates a couple of issues:
- if the process crashes while sleeping it won't be considered done and will
rerun on restart
- you'd have to wait for the sleeping task to finish on
[graceful shutdown](#stopping-workers)
Use [`NextStep::delay`] instead - it schedules the next step with the delay and
finishes the current one right away.
You can find a runnable example in the [examples/delay.rs][delay-example]
### Retrying Steps
Use [`Step::RETRY_LIMIT`] and [`Step::RETRY_DELAY`] when you need to retry a
task on errors:
```rust
# use async_trait::async_trait;
# use pg_task::{NextStep, Step, StepResult};
# use serde::{Deserialize, Serialize};
# use sqlx::PgPool;
# use std::time::Duration;
# #[derive(Debug, Deserialize, Serialize)]
# struct ProcessResult {
# result: String,
# }
# #[derive(Debug, Deserialize, Serialize)]
# struct ApiRequest;
# pg_task::task!(MyTask { ApiRequest, ProcessResult });
# async fn api_request() -> Result<String, std::io::Error> {
# Ok(String::from("ok"))
# }
# #[async_trait]
impl Step<MyTask> for ApiRequest {
const RETRY_LIMIT: i32 = 5;
const RETRY_DELAY: Duration = Duration::from_secs(5);
async fn step(self, _db: &PgPool) -> StepResult<MyTask> {
let result = api_request().await?;
NextStep::now(ProcessResult { result })
}
}
# #[async_trait]
# impl Step<MyTask> for ProcessResult {
# async fn step(self, _db: &PgPool) -> StepResult<MyTask> {
# NextStep::none()
# }
# }
```
[delay-example]: https://github.com/imbolc/pg_task/blob/main/examples/delay.rs
[tutorial-example]:
https://github.com/imbolc/pg_task/blob/main/examples/tutorial.rs
*/
pub use ;
pub use NextStep;
pub use ;
pub use ;
pub use Worker;
use ;
use ;
use Duration;
const LOST_CONNECTION_SLEEP: Duration = from_secs;
/// Enqueues the task to be run immediately
pub async
/// Schedules a task to be run after a specified delay
pub async
/// Schedules a task to run at a specified time in the future
pub async