pg_task 1.0.0

Resumable state machine based Postgres tasks
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
use crate::{
    util::{db_error, ordinal, std_duration_to_chrono},
    Error, NextStep, Result, Step, StepError,
};
use chrono::{DateTime, Utc};
use serde::Serialize;
use sqlx::{
    postgres::{types::PgInterval, PgConnection, PgPool},
    PgExecutor,
};
use std::{
    fmt,
    time::{Duration, Instant},
};
use tracing::{debug, error, info, trace, warn};
use uuid::Uuid;

#[derive(Debug)]
pub struct Task {
    pub id: Uuid,
    step: String,
    tried: i32,
}

#[derive(Clone, Copy, Debug)]
pub(crate) struct WorkerLease {
    worker_id: Uuid,
    timeout: PgInterval,
    timeout_duration: Duration,
}

#[derive(Debug)]
pub(crate) struct ClaimedStep<S> {
    pub(crate) step: S,
    pub(crate) lock_expires_at: DateTime<Utc>,
    pub(crate) lease_refreshed_at: Instant,
}

#[derive(Debug)]
pub(crate) struct RenewedTaskLease {
    pub(crate) task_id: Uuid,
    pub(crate) lock_expires_at: DateTime<Utc>,
    pub(crate) lease_refreshed_at: Instant,
}

impl WorkerLease {
    pub(crate) fn new(worker_id: Uuid, timeout: Duration) -> Self {
        Self {
            worker_id,
            timeout: duration_to_pg_interval(timeout),
            timeout_duration: timeout,
        }
    }

    pub(crate) fn timeout_duration(self) -> Duration {
        self.timeout_duration
    }
}

fn duration_to_pg_interval(duration: Duration) -> PgInterval {
    let microseconds = duration.as_nanos().div_ceil(1_000);
    PgInterval {
        months: 0,
        days: 0,
        microseconds: microseconds.min(i64::MAX as u128) as i64,
    }
}

impl Task {
    /// Returns a delay before a task should run at the given time.
    pub fn delay_until(wakeup_at: DateTime<Utc>) -> Option<Duration> {
        (wakeup_at - Utc::now())
            .to_std()
            .ok()
            .filter(|delay| !delay.is_zero())
    }

    /// Fetches the closest ready task to run.
    pub async fn fetch_ready(con: &mut PgConnection) -> Result<Option<Self>> {
        trace!("Fetching the closest ready task to run");
        sqlx::query_as!(
            Task,
            r#"
            SELECT
                id,
                step,
                tried
            FROM pg_task
            WHERE error IS NULL
              AND (
                CASE
                    WHEN locked_by IS NOT NULL THEN
                        GREATEST(wakeup_at, lock_expires_at)
                    ELSE
                        wakeup_at
                END
              ) <= now()
            ORDER BY
                CASE
                    WHEN locked_by IS NOT NULL THEN
                        GREATEST(wakeup_at, lock_expires_at)
                    ELSE
                        wakeup_at
                END
            LIMIT 1
            FOR UPDATE SKIP LOCKED
            "#,
        )
        .fetch_optional(con)
        .await
        .map_err(db_error!())
    }

    /// Fetches the closest time when a task may become claimable.
    pub async fn fetch_next_available_at(con: &mut PgConnection) -> Result<Option<DateTime<Utc>>> {
        trace!("Fetching the closest task availability time");
        sqlx::query_scalar!(
            r#"
            SELECT
                CASE
                    WHEN locked_by IS NOT NULL THEN
                        GREATEST(wakeup_at, lock_expires_at)
                    ELSE
                        wakeup_at
                END AS "next_at!"
            FROM pg_task
            WHERE error IS NULL
            ORDER BY 1
            LIMIT 1
            "#,
        )
        .fetch_optional(con)
        .await
        .map_err(db_error!())
    }

    /// Claims the task lease for this worker.
    pub(crate) async fn claim_lease(
        &self,
        con: &mut PgConnection,
        lease: WorkerLease,
    ) -> Result<(DateTime<Utc>, Instant)> {
        trace!("[{}] claim lease", self.id);
        let lease_refreshed_at = Instant::now();
        sqlx::query!(
            r#"
            UPDATE pg_task
            SET locked_by = $2,
                lock_expires_at = now() + $3::interval
            WHERE id = $1
            RETURNING lock_expires_at AS "lock_expires_at!"
            "#,
            self.id,
            lease.worker_id,
            lease.timeout,
        )
        .fetch_one(con)
        .await
        .map(|row| (row.lock_expires_at, lease_refreshed_at))
        .map_err(db_error!("claim lease"))
    }

    /// Renews live task leases owned by a worker.
    pub(crate) async fn renew_leases(
        db: &PgPool,
        lease: WorkerLease,
        task_ids: &[Uuid],
    ) -> Result<Vec<RenewedTaskLease>> {
        if task_ids.is_empty() {
            return Ok(Vec::new());
        }
        trace!("Renewing task leases for worker {}", lease.worker_id);
        let lease_refreshed_at = Instant::now();
        sqlx::query!(
            r#"
            UPDATE pg_task
            SET lock_expires_at = now() + $2::interval
            WHERE locked_by = $1
              AND id = ANY($3)
              AND lock_expires_at > now()
              AND error IS NULL
            RETURNING id, lock_expires_at AS "lock_expires_at!"
            "#,
            lease.worker_id,
            lease.timeout,
            task_ids,
        )
        .fetch_all(db)
        .await
        .map(|rows| {
            rows.into_iter()
                .map(|row| RenewedTaskLease {
                    task_id: row.id,
                    lock_expires_at: row.lock_expires_at,
                    lease_refreshed_at,
                })
                .collect()
        })
        .map_err(db_error!("renew leases"))
    }

    /// Deserializes the current task step and claims its lease.
    /// If deserialization fails, stores the error instead and leaves the task
    /// unleased.
    pub(crate) async fn claim<S: Step<S>>(
        &self,
        con: &mut PgConnection,
        lease: WorkerLease,
    ) -> Result<Option<ClaimedStep<S>>> {
        let step = match self.parse_step() {
            Ok(step) => step,
            Err(e) => {
                self.save_claim_error(&mut *con, e.into()).await?;
                return Ok(None);
            }
        };

        let (lock_expires_at, lease_refreshed_at) = self.claim_lease(con, lease).await?;
        Ok(Some(ClaimedStep {
            step,
            lock_expires_at,
            lease_refreshed_at,
        }))
    }

    /// Runs the current step of the task to completion
    pub(crate) async fn run_step<S: Step<S>>(
        &self,
        db: &PgPool,
        step: S,
        lease: WorkerLease,
    ) -> Result<()> {
        info!(
            "[{id}]{attempt} run step {step}",
            id = self.id,
            attempt = if self.tried > 0 {
                format!(" {} attempt to", ordinal(self.tried + 1))
            } else {
                "".into()
            },
            step = self.step
        );
        let retry_limit = step.retry_limit();
        let retry_delay = step.retry_delay();
        match step.step(db).await {
            Err(e) => {
                if self.tried < retry_limit {
                    self.retry(db, self.tried, retry_limit, retry_delay, e, lease)
                        .await?;
                } else {
                    self.save_step_error(db, e, lease).await?;
                }
            }
            Ok(NextStep::None) => self.complete(db, lease).await?,
            Ok(NextStep::Now(step)) => {
                self.save_next_step(db, step, Duration::ZERO, lease).await?;
            }
            Ok(NextStep::Delayed(step, delay)) => {
                self.save_next_step(db, step, delay, lease).await?;
            }
        };
        Ok(())
    }

    fn parse_step<S: Step<S>>(&self) -> Result<S> {
        serde_json::from_str(&self.step)
            .map_err(|e| Error::DeserializeStep(e, self.step.to_string()))
    }

    /// Saves a deserialization error for a task before the worker owns it.
    async fn save_claim_error<'e, E>(&self, db: E, err: StepError) -> Result<()>
    where
        E: PgExecutor<'e>,
    {
        let err_str = source_chain::to_string(&*err);
        let step = sqlx::query!(
            r#"
            UPDATE pg_task
            SET error = $2,
                wakeup_at = now(),
                locked_by = NULL,
                lock_expires_at = NULL
            WHERE id = $1
            RETURNING step::TEXT as "step!"
            "#,
            self.id,
            &err_str,
        )
        .fetch_one(db)
        .await
        .map(|r| r.step)
        .map_err(db_error!())?;

        error!(
            "[{id}] couldn't deserialize step {step}: {err_str}",
            id = self.id
        );

        Ok(())
    }

    /// Saves the task error if the worker still owns the task.
    async fn save_step_error(&self, db: &PgPool, err: StepError, lease: WorkerLease) -> Result<()> {
        let err_str = source_chain::to_string(&*err);
        let updated_task = sqlx::query!(
            r#"
            UPDATE pg_task
            SET tried = tried + 1,
                error = $2,
                wakeup_at = now(),
                locked_by = NULL,
                lock_expires_at = NULL
            WHERE id = $1
              AND locked_by = $3
              AND lock_expires_at > now()
            RETURNING step::TEXT as "step!"
            "#,
            self.id,
            &err_str,
            lease.worker_id,
        )
        .fetch_optional(db)
        .await
        .map_err(db_error!())?;

        let Some(updated_task) = updated_task else {
            self.log_lost_lease(lease.worker_id, "save the step error");
            return Ok(());
        };

        let attempt = self.tried + 1;
        error!(
            "[{id}] resulted in an error at step {step} on {attempt} attempt: {err_str}",
            id = self.id,
            step = updated_task.step,
            attempt = ordinal(attempt)
        );

        Ok(())
    }

    /// Updates the tasks step
    async fn save_next_step(
        &self,
        db: &PgPool,
        step: impl Serialize + fmt::Debug,
        delay: Duration,
        lease: WorkerLease,
    ) -> Result<()> {
        let step = match serde_json::to_string(&step)
            .map_err(|e| Error::SerializeStep(e, format!("{step:?}")))
        {
            Ok(x) => x,
            Err(e) => return self.save_step_error(db, e.into(), lease).await,
        };
        let result = sqlx::query!(
            "
            UPDATE pg_task
            SET tried = 0,
                step = $2,
                wakeup_at = $3,
                locked_by = NULL,
                lock_expires_at = NULL
            WHERE id = $1
              AND locked_by = $4
              AND lock_expires_at > now()
            ",
            self.id,
            step,
            Utc::now() + std_duration_to_chrono(delay),
            lease.worker_id,
        )
        .execute(db)
        .await
        .map_err(db_error!())?;
        if result.rows_affected() == 0 {
            self.log_lost_lease(lease.worker_id, "save the next step");
        } else {
            debug!("[{}] moved to the next step {step}", self.id);
        }
        Ok(())
    }

    /// Removes the finished task
    async fn complete(&self, db: &PgPool, lease: WorkerLease) -> Result<()> {
        let result = sqlx::query!(
            r#"
            DELETE FROM pg_task
            WHERE id = $1
              AND locked_by = $2
              AND lock_expires_at > now()
            "#,
            self.id,
            lease.worker_id,
        )
        .execute(db)
        .await
        .map_err(db_error!())?;
        if result.rows_affected() == 0 {
            self.log_lost_lease(lease.worker_id, "complete the task");
        } else {
            info!("[{}] is successfully completed", self.id);
        }
        Ok(())
    }

    /// Schedules the task for retry
    async fn retry(
        &self,
        db: &PgPool,
        tried: i32,
        retry_limit: i32,
        delay: Duration,
        err: StepError,
        lease: WorkerLease,
    ) -> Result<()> {
        let delay = std_duration_to_chrono(delay);

        let result = sqlx::query!(
            "
            UPDATE pg_task
            SET tried = tried + 1,
                wakeup_at = $2,
                locked_by = NULL,
                lock_expires_at = NULL
            WHERE id = $1
              AND locked_by = $3
              AND lock_expires_at > now()
            ",
            self.id,
            Utc::now() + delay,
            lease.worker_id,
        )
        .execute(db)
        .await
        .map_err(db_error!())?;
        if result.rows_affected() == 0 {
            self.log_lost_lease(lease.worker_id, "schedule a retry");
        } else {
            debug!(
                "[{id}] scheduled {attempt} of {retry_limit} retries in {delay:?} on error: {err}",
                id = self.id,
                attempt = ordinal(tried + 1),
                err = source_chain::to_string(&*err),
            );
        }

        Ok(())
    }

    fn log_lost_lease(&self, worker_id: Uuid, action: &str) {
        warn!(
            "[{}] couldn't {action} because worker {worker_id}'s lease expired or is no longer owned by this worker",
            self.id
        );
    }
}

#[cfg(test)]
mod tests;