pg-taskq 0.3.0

A simple postgres-based distributed and persistent task queue for Rust
Documentation
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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
use chrono::prelude::*;
use serde::Serialize;
use serde_json::Value;
use sqlx::{Acquire, PgExecutor, Pool, Postgres};
use std::collections::HashSet;
use std::time::Instant;
use uuid::Uuid;

use crate::{Error, Result, TaskTableProvider, TaskType};

#[derive(Default)]
pub struct TaskBuilder {
    task_type: String,
    req: Option<Value>,
    parent: Option<Uuid>,
    id: Option<Uuid>,
}

impl TaskBuilder {
    pub fn new(task_type: impl TaskType) -> Self {
        Self {
            task_type: task_type.to_string(),
            ..Default::default()
        }
    }

    pub fn with_request(mut self, req: impl Serialize) -> Result<Self> {
        self.req = Some(serde_json::to_value(req)?);
        Ok(self)
    }

    #[must_use]
    pub fn with_parent(mut self, parent: Uuid) -> Self {
        self.parent = Some(parent);
        self
    }

    #[must_use]
    pub fn with_id(mut self, id: Uuid) -> Self {
        self.id = Some(id);
        self
    }

    pub async fn build<'a, DB, P>(self, db: DB, tables: &P) -> Result<Task>
    where
        P: TaskTableProvider,
        DB: Acquire<'a, Database = Postgres>,
    {
        let Self {
            task_type,
            req,
            parent,
            id,
        } = self;
        Task::create_task(db, tables, id, task_type, req, parent).await
    }
}

#[derive(Debug, Clone, sqlx::FromRow)]
pub struct Task {
    pub id: Uuid,
    pub parent: Option<Uuid>,
    pub created_at: DateTime<Utc>,
    pub updated_at: DateTime<Utc>,
    pub task_type: String,
    pub request: Option<Value>,
    pub result: Option<Value>,
    pub error: Option<Value>,
    pub in_progress: bool,
    pub done: bool,
}

impl Task {
    #[instrument(level = "trace", skip(db, tables, req))]
    pub async fn create_task<'a, DB, P>(
        db: DB,
        tables: &P,
        id: Option<Uuid>,
        task_type: String,
        req: Option<Value>,
        parent: Option<Uuid>,
    ) -> Result<Self>
    where
        P: TaskTableProvider,
        DB: Acquire<'a, Database = Postgres>,
    {
        let id = id.unwrap_or_else(Uuid::new_v4);
        let req = Some(serde_json::to_value(req)?);

        let mut tx = db.begin().await?;

        let table = tables.tasks_table_full_name();
        let notify_fn = tables.tasks_notify_fn_full_name();

        let sql = format!(
            "
INSERT INTO {table} (id, task_type, request, parent, in_progress, done)
VALUES ($1, $2, $3, $4, false, false)
RETURNING *
"
        );
        let task: Self = sqlx::query_as(&sql)
            .bind(id)
            .bind(task_type.to_string())
            .bind(req)
            .bind(parent)
            .fetch_one(&mut *tx)
            .await?;

        let sql = format!("SELECT {notify_fn}($1)");
        sqlx::query(&sql).bind(task.id).execute(&mut *tx).await?;

        tx.commit().await?;

        debug!("created task {task_type:?} {id}");

        Ok(task)
    }

    pub async fn load(
        db: impl PgExecutor<'_>,
        tables: &dyn TaskTableProvider,
        id: Uuid,
    ) -> Result<Option<Self>> {
        let table = tables.tasks_table_full_name();
        let sql = format!("SELECT * FROM {table} WHERE id = $1");
        Ok(sqlx::query_as(&sql).bind(id).fetch_optional(db).await?)
    }

    pub async fn with_children(
        self,
        db: impl PgExecutor<'_>,
        tables: &dyn TaskTableProvider,
        recursive: bool,
    ) -> Result<Vec<Self>> {
        Self::load_children(db, tables, self.id, true, recursive).await
    }

    pub async fn children(
        &self,
        db: impl PgExecutor<'_>,
        tables: &dyn TaskTableProvider,
        recursive: bool,
    ) -> Result<Vec<Self>> {
        Self::load_children(db, tables, self.id, false, recursive).await
    }

    #[instrument(level = "trace", skip(db, tables))]
    pub async fn load_children(
        db: impl PgExecutor<'_>,
        tables: &dyn TaskTableProvider,
        id: Uuid,
        include_self: bool,
        recursive: bool,
    ) -> Result<Vec<Self>> {
        let table = tables.tasks_table_full_name();
        let sql = if recursive {
            let where_clause = if include_self { "" } else { "WHERE t.id != $1" };
            format!(
                "
WITH RECURSIVE tasks_and_subtasks(id, parent) AS (
     SELECT t.* FROM {table} t
     WHERE t.id = $1
     UNION ALL
     SELECT child_task.*
     FROM tasks_and_subtasks t, {table} child_task
     WHERE t.id = child_task.parent
)
SELECT * FROM tasks_and_subtasks {where_clause}
"
            )
        } else {
            let self_condition = if include_self { "OR t.id = $1" } else { "" };
            format!("SELECT * FROM {table} WHERE parent = $1 {self_condition}")
        };

        Ok(sqlx::query_as(&sql).bind(id).fetch_all(db).await?)
    }

    #[instrument(level = "trace", skip(db, tables))]
    pub async fn load_any_waiting(
        db: impl PgExecutor<'_>,
        tables: &dyn TaskTableProvider,
        allowed_types: &[impl TaskType],
    ) -> Result<Option<Self>> {
        let allowed_types = allowed_types
            .iter()
            .map(|ea| ea.to_string())
            .collect::<Vec<_>>();
        let table = tables.tasks_table_full_name();
        let table_ready = tables.tasks_ready_view_full_name();
        let sql = format!(
            "
UPDATE {table}
SET updated_at = NOW(),
    in_progress = true
WHERE id = (SELECT id
            FROM {table}
            WHERE task_type = ANY($1) AND id IN (SELECT id FROM {table_ready})
            LIMIT 1
            FOR UPDATE SKIP LOCKED)
RETURNING *;
"
        );
        let task: Option<Self> = sqlx::query_as(&sql)
            .bind(allowed_types)
            .fetch_optional(db)
            .await?;

        Ok(task)
    }

    #[instrument(level = "trace", skip(db, tables))]
    pub async fn load_waiting(
        id: Uuid,
        db: impl PgExecutor<'_>,
        tables: &dyn TaskTableProvider,
        allowed_types: &[impl TaskType],
    ) -> Result<Option<Self>> {
        let allowed_types = allowed_types
            .iter()
            .map(|ea| ea.to_string())
            .collect::<Vec<_>>();
        let table = tables.tasks_table_full_name();
        let table_ready = tables.tasks_ready_view_full_name();
        let sql = format!(
            "
UPDATE {table}
SET updated_at = NOW(),
    in_progress = true
WHERE id = (SELECT id
            FROM {table}
            WHERE id = $1 AND task_type = ANY($2) AND id IN (SELECT id FROM {table_ready})
            LIMIT 1
            FOR UPDATE SKIP LOCKED)
RETURNING *;
"
        );
        let task: Option<Self> = sqlx::query_as(&sql)
            .bind(sqlx::types::Uuid::from_u128(id.as_u128()))
            .bind(allowed_types)
            .fetch_optional(db)
            .await?;

        Ok(task)
    }

    /// Sets the error value for the task
    #[instrument(level = "trace", skip(db, tables))]
    pub async fn set_error(
        id: Uuid,
        db: impl Acquire<'_, Database = Postgres>,
        tables: &dyn TaskTableProvider,
        error: Value,
    ) -> Result<()> {
        let mut tx = db.begin().await?;

        let table = tables.tasks_table_full_name();
        let sql = format!(
            "
UPDATE {table}
SET updated_at = NOW(),
    error = $2,
    in_progress = false
WHERE id = $1"
        );

        sqlx::query(&sql)
            .bind(id)
            .bind(error)
            .execute(&mut *tx)
            .await?;

        // if self.done {
        //     debug!("notifying about task done {}", self.id);
        //     let notify_fn = tables.tasks_notify_done_fn_full_name();
        //     let sql = format!("SELECT {notify_fn}($1)");
        //     sqlx::query(&sql).bind(self.id).execute(&mut *tx).await?;
        // } else {
        //     debug!("notifying about task ready again {}", self.id);
        //     let notify_fn = tables.tasks_notify_done_fn_full_name();
        //     let sql = format!("SELECT {notify_fn}($1)");
        //     sqlx::query(&sql).bind(self.id).execute(&mut *tx).await?;
        // }

        tx.commit().await?;

        Ok(())
    }

    /// Saves current state in DB
    #[instrument(level = "trace", skip(self, db,tables), fields(id=%self.id))]
    pub async fn save(
        &self,
        db: impl Acquire<'_, Database = Postgres>,
        tables: &dyn TaskTableProvider,
    ) -> Result<()> {
        let mut tx = db.begin().await?;

        let table = tables.tasks_table_full_name();
        let sql = format!(
            "
UPDATE {table}
SET updated_at = NOW(),
    request = $2,
    result = $3,
    error = $4,
    in_progress = $5,
    done = $6
WHERE id = $1"
        );

        sqlx::query(&sql)
            .bind(self.id)
            .bind(&self.request)
            .bind(&self.result)
            .bind(&self.error)
            .bind(self.in_progress)
            .bind(self.done)
            .execute(&mut *tx)
            .await?;

        if self.done {
            debug!("notifying about task done {}", self.id);
            let notify_fn = tables.tasks_notify_done_fn_full_name();
            let sql = format!("SELECT {notify_fn}($1)");
            sqlx::query(&sql).bind(self.id).execute(&mut *tx).await?;
        } else {
            debug!("notifying about task ready again {}", self.id);
            let notify_fn = tables.tasks_notify_fn_full_name();
            let sql = format!("SELECT {notify_fn}($1)");
            sqlx::query(&sql).bind(self.id).execute(&mut *tx).await?;
        }

        tx.commit().await?;

        Ok(())
    }

    /// Queries state of this task from DB and updates self.
    #[instrument(level = "trace", skip(self, db,tables), fields(id=%self.id))]
    pub async fn update(
        &mut self,
        db: impl PgExecutor<'_>,
        tables: &dyn TaskTableProvider,
    ) -> Result<()> {
        info!("UPDATING {}", self.id);
        let table = tables.tasks_table_full_name();
        let sql = format!("SELECT * FROM {table} WHERE id = $1");
        let me: Self = sqlx::query_as(&sql).bind(self.id).fetch_one(db).await?;

        let _ = std::mem::replace(self, me);

        Ok(())
    }

    /// Deletes this and all child tasks (recursively) from the DB. Call this
    /// when the task is done.
    #[instrument(level = "trace", skip(self, db,tables), fields(id=%self.id))]
    pub async fn delete(
        &self,
        db: impl Acquire<'_, Database = Postgres>,
        tables: &dyn TaskTableProvider,
    ) -> Result<()> {
        let mut tx = db.begin().await?;
        let con = tx.acquire().await?;

        let table = tables.tasks_table_full_name();
        let sql = format!("DELETE FROM {table} WHERE id = $1");
        sqlx::query(&sql).bind(self.id).execute(&mut *con).await?;

        let notify_fn = tables.tasks_notify_done_fn_full_name();
        let sql = format!("SELECT {notify_fn}($1)");
        sqlx::query(&sql).bind(self.id).execute(&mut *con).await?;

        tx.commit().await?;

        Ok(())
    }

    /// Fullfill task with result/error and mark as done
    #[instrument(level = "trace", skip(self, db, tables, result, error), fields(id=%self.id))]
    pub async fn fullfill(
        &mut self,
        db: impl Acquire<'_, Database = Postgres>,
        tables: &dyn TaskTableProvider,
        result: Option<impl Into<Value>>,
        error: Option<impl Into<Value>>,
    ) -> Result<()> {
        self.result = result.map(Into::into);
        self.error = error.map(Into::into);
        self.in_progress = false;
        self.done = true;
        self.save(db, tables).await
    }

    /// Takes `tasks` and listens for notifications until all tasks are done.
    /// Inbetween notifications, at `poll_interval`, will manually query for
    /// updated tasks. Will ensure that those tasks are updated when this method
    /// returns.
    #[instrument(level = "trace", skip(pool, tables))]
    async fn wait_for_tasks_to_be_done(
        tasks: Vec<&mut Self>,
        pool: &Pool<Postgres>,
        tables: &dyn TaskTableProvider,
        poll_interval: Option<std::time::Duration>,
    ) -> Result<()> {
        fn update<'a>(
            tasks_pending: Vec<&'a mut Task>,
            tasks_done: &mut Vec<&'a mut Task>,
            ids: std::collections::HashSet<Uuid>,
        ) -> Vec<&'a mut Task> {
            let (done, rest): (Vec<_>, Vec<_>) = tasks_pending
                .into_iter()
                .partition(|task| ids.contains(&task.id));
            tasks_done.extend(done);
            trace!("still waiting for {} tasks", rest.len());
            rest
        }

        let start_time = Instant::now();
        let mut tasks_pending = tasks.into_iter().collect::<Vec<_>>();
        let mut tasks_done = Vec::new();

        let mut listener = sqlx::postgres::PgListener::connect_with(pool).await?;
        let queue_name = tables.tasks_queue_done_name();
        listener.listen(&queue_name).await?;

        let tasks_table = tables.tasks_table();
        let ready_sql = format!("SELECT id FROM {tasks_table} WHERE id = ANY($1) AND done = true");
        let existing_sql = format!("SELECT id FROM {tasks_table} WHERE id = ANY($1)");

        loop {
            trace!("waiting for task {} to be done", tasks_pending.len());

            let ready: Vec<(Uuid,)> = sqlx::query_as(&ready_sql)
                .bind(tasks_pending.iter().map(|ea| ea.id).collect::<Vec<_>>())
                .fetch_all(pool)
                .await?;

            let existing: Vec<(Uuid,)> = sqlx::query_as(&existing_sql)
                .bind(tasks_pending.iter().map(|ea| ea.id).collect::<Vec<_>>())
                .fetch_all(pool)
                .await?;
            let existing = existing.into_iter().map(|(id,)| id).collect::<HashSet<_>>();

            tasks_pending = update(
                tasks_pending,
                &mut tasks_done,
                HashSet::from_iter(ready.into_iter().map(|(id,)| id)),
            );

            if tasks_pending.is_empty() {
                break;
            }

            // in case one of the tasks we are waiting for was deleted
            for ea in &tasks_pending {
                if !existing.contains(&ea.id) {
                    return Err(Error::TaskDeleted { task: ea.id });
                }
            }

            let notification = if let Some(poll_interval) = poll_interval {
                tokio::select! {
                    _ = tokio::time::sleep(poll_interval) => {
                        continue;
                    },
                    notification = listener.recv() => notification,
                }
            } else {
                listener.recv().await
            };

            if let Ok(notification) = notification {
                if let Ok(id) = Uuid::parse_str(notification.payload()) {
                    tasks_pending =
                        update(tasks_pending, &mut tasks_done, HashSet::from_iter([id]));
                    if tasks_pending.is_empty() {
                        break;
                    }
                }
            }
        }

        for task in &mut tasks_done {
            task.update(pool, tables).await?;
        }

        debug!(
            "{} tasks done, wait time: {}ms",
            tasks_done.len(),
            (Instant::now() - start_time).as_millis()
        );

        Ok(())
    }

    #[instrument(level = "trace", skip(self, db,tables), fields(id=%self.id))]
    pub async fn wait_until_done(
        &mut self,
        db: &Pool<Postgres>,
        tables: &dyn TaskTableProvider,
        poll_interval: Option<std::time::Duration>,
    ) -> Result<()> {
        Self::wait_for_tasks_to_be_done(vec![self], db, tables, poll_interval).await?;
        Ok(())
    }

    pub async fn wait_until_done_and_delete(
        &mut self,
        pool: &Pool<Postgres>,
        tables: &dyn TaskTableProvider,
        poll_interval: Option<std::time::Duration>,
    ) -> Result<()> {
        self.wait_until_done(pool, tables, poll_interval).await?;
        self.delete(pool, tables).await?;
        Ok(())
    }

    /// Takes request returns it deserialized.
    pub fn request<R: serde::de::DeserializeOwned>(&mut self) -> Result<R> {
        let request = match self.request.take() {
            None => {
                return Err(Error::TaskError {
                    task: self.id,
                    message: "Task has no request JSON data".to_string(),
                })
            }
            Some(request) => request,
        };

        serde_json::from_value(request).map_err(|err| Error::TaskError {
            task: self.id,
            message: format!("Error deserializing request JSON from task: {err}"),
        })
    }

    /// Converts self into the result payload (or error).
    fn error(&mut self) -> Option<Error> {
        self.error.take().map(|error| Error::TaskError {
            task: self.id,
            message: match error.get("error").and_then(|msg| msg.as_str()) {
                Some(msg) => msg.to_string(),
                None => error.to_string(),
            },
        })
    }

    /// Takes the result returns it deserialized.
    fn result<R: serde::de::DeserializeOwned>(&mut self) -> Result<Option<R>> {
        match self.result.take() {
            None => Ok(None),
            Some(request) => Ok(Some(serde_json::from_value(request)?)),
        }
    }

    /// Takes the result returns it deserialized.
    #[allow(dead_code)]
    pub fn result_cloned<R: serde::de::DeserializeOwned>(&self) -> Result<Option<R>> {
        if let Some(result) = &self.result {
            Ok(Some(serde_json::from_value(result.clone())?))
        } else {
            Ok(None)
        }
    }

    /// Converts self into the result payload (or error).
    pub fn as_result<R: serde::de::DeserializeOwned>(&mut self) -> Result<Option<R>> {
        self.error().map(Err).unwrap_or_else(|| self.result())
    }
}