rullst-core 12.2.0

Core runtime, routing engine, and telemetry for the Rullst framework suite.
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
// src/queue/sqlite.rs — SQLite-backed queue driver with automatic schema migrations.

use super::{
    QueueDriver, QueueError, QueuedJob, QueuedJobDetail, unix_timestamp_millis_ceil,
    unix_timestamp_millis_floor,
};
use async_trait::async_trait;
use std::time::{Duration, SystemTime};

const MAX_COMPLETED_HISTORY: usize = 100_000;

mod recovery;
mod schema;

/// Queue driver backed by a SQLite database.
///
/// Uses an auto-created `rullst_jobs` table. Perfect for local development
/// and small-to-medium production workloads. Zero external dependencies.
pub struct SqliteDriver {
    pub(crate) pool: sqlx::SqlitePool,
    completed_history_limit: usize,
    max_stalled_leases: u32,
}

impl SqliteDriver {
    /// Create a new SQLite queue driver. Automatically creates the `rullst_jobs`
    /// table if it doesn't exist.
    pub async fn new(database_url: impl Into<String>) -> Result<Self, QueueError> {
        let database_url = database_url.into();
        let pool = sqlx::SqlitePool::connect(&database_url)
            .await
            .map_err(|e| QueueError::Driver(format!("Failed to connect to SQLite: {}", e)))?;
        schema::prepare(&pool).await?;

        Ok(Self {
            pool,
            completed_history_limit: 0,
            max_stalled_leases: super::DEFAULT_MAX_STALLED_LEASES,
        })
    }

    /// Enables bounded retention of successful jobs for monitoring.
    ///
    /// Retention is disabled by default so successful payloads are deleted. When enabled, the
    /// completion transition and pruning are committed atomically.
    pub fn try_with_completed_history_limit(
        mut self,
        retained_jobs: usize,
    ) -> Result<Self, QueueError> {
        if !(1..=MAX_COMPLETED_HISTORY).contains(&retained_jobs) {
            return Err(QueueError::InvalidConfiguration(format!(
                "completed job history must retain between 1 and {MAX_COMPLETED_HISTORY} records"
            )));
        }
        self.completed_history_limit = retained_jobs;
        Ok(self)
    }

    /// Returns a reference to the internal SQLite pool.
    pub fn get_pool(&self) -> &sqlx::SqlitePool {
        &self.pool
    }

    /// Retrieves a list of all jobs up to the specified limit, sorted by creation time.
    pub async fn list_all_jobs(&self, limit: u32) -> Result<Vec<QueuedJobDetail>, QueueError> {
        #[derive(sqlx::FromRow)]
        struct JobRow {
            id: String,
            name: String,
            payload: String,
            status: String,
            error: Option<String>,
            attempts: i32,
            created_at: String,
            updated_at: String,
        }

        let rows: Vec<JobRow> = sqlx::query_as(
            "SELECT id, name, payload, status, error, attempts, created_at, updated_at FROM rullst_jobs ORDER BY created_at DESC LIMIT ?"
        )
        .bind(limit as i64)
        .fetch_all(&self.pool)
        .await
        .map_err(|e| QueueError::Driver(e.to_string()))?;

        Ok(rows
            .into_iter()
            .map(|row| QueuedJobDetail {
                id: row.id,
                name: row.name,
                payload: row.payload,
                status: row.status,
                error: row.error,
                attempts: row.attempts,
                created_at: row.created_at,
                updated_at: row.updated_at,
            })
            .collect())
    }

    /// Retries a failed job by resetting its status to 'pending' and clearing error details.
    pub async fn retry_failed_job(&self, job_id: &str) -> Result<(), QueueError> {
        let result = sqlx::query("UPDATE rullst_jobs SET status = 'pending', attempts = 0, stalled_recoveries = 0, error = NULL, available_at_ms = 0, updated_at = datetime('now') WHERE id = ? AND status = 'failed'")
            .bind(job_id)
            .execute(&self.pool)
            .await
            .map_err(|e| QueueError::Driver(e.to_string()))?;
        ensure_transition(result.rows_affected(), job_id, "retry_failed")?;
        Ok(())
    }

    /// Purges all failed jobs from the database.
    pub async fn purge_failed_jobs(&self) -> Result<(), QueueError> {
        sqlx::query("DELETE FROM rullst_jobs WHERE status = 'failed'")
            .execute(&self.pool)
            .await
            .map_err(|e| QueueError::Driver(e.to_string()))?;
        Ok(())
    }

    /// Purges successful jobs retained by the explicit history policy.
    pub async fn purge_completed_history(&self) -> Result<(), QueueError> {
        sqlx::query("DELETE FROM rullst_jobs WHERE status = 'completed'")
            .execute(&self.pool)
            .await
            .map_err(|error| {
                QueueError::Driver(format!("Failed to purge completed job history: {error}"))
            })?;
        Ok(())
    }

    /// Completes a processing job. With `attempt`, only the claim with that
    /// attempt number matches, so a recovered and re-claimed lease is fenced.
    async fn complete_claim(&self, job_id: &str, attempt: Option<u32>) -> Result<(), QueueError> {
        let attempt = attempt.map(i64::from);
        if self.completed_history_limit == 0 {
            let result = sqlx::query(
                "DELETE FROM rullst_jobs WHERE id = ? AND status = 'processing' AND attempts = COALESCE(?, attempts)",
            )
            .bind(job_id)
            .bind(attempt)
            .execute(&self.pool)
            .await
            .map_err(|error| QueueError::Driver(format!("Failed to mark job complete: {error}")))?;
            ensure_claim_transition(result.rows_affected(), job_id, "mark_complete", attempt)?;
            return Ok(());
        }

        let retained_jobs = i64::try_from(self.completed_history_limit).map_err(|_| {
            QueueError::InvalidConfiguration(
                "completed job history exceeds SQLite integer range".to_string(),
            )
        })?;
        let mut transaction = self.pool.begin().await.map_err(|error| {
            QueueError::Driver(format!(
                "Failed to begin job completion transaction: {error}"
            ))
        })?;
        let result = sqlx::query(
            "UPDATE rullst_jobs SET status = 'completed', error = NULL, updated_at = strftime('%Y-%m-%dT%H:%M:%fZ', 'now') WHERE id = ? AND status = 'processing' AND attempts = COALESCE(?, attempts)",
        )
        .bind(job_id)
        .bind(attempt)
        .execute(&mut *transaction)
        .await
        .map_err(|error| QueueError::Driver(format!("Failed to retain completed job: {error}")))?;
        ensure_claim_transition(result.rows_affected(), job_id, "mark_complete", attempt)?;
        sqlx::query(
            "DELETE FROM rullst_jobs WHERE status = 'completed' AND id NOT IN (SELECT id FROM rullst_jobs WHERE status = 'completed' ORDER BY updated_at DESC, rowid DESC LIMIT ?)",
        )
        .bind(retained_jobs)
        .execute(&mut *transaction)
        .await
        .map_err(|error| {
            QueueError::Driver(format!("Failed to prune completed job history: {error}"))
        })?;
        transaction.commit().await.map_err(|error| {
            QueueError::Driver(format!("Failed to commit completed job history: {error}"))
        })?;
        Ok(())
    }

    async fn fail_claim(
        &self,
        job_id: &str,
        attempt: Option<u32>,
        error: &str,
    ) -> Result<(), QueueError> {
        let attempt = attempt.map(i64::from);
        let result = sqlx::query(
            "UPDATE rullst_jobs SET status = 'failed', error = ?, updated_at = datetime('now') WHERE id = ? AND status = 'processing' AND attempts = COALESCE(?, attempts)",
        )
        .bind(error)
        .bind(job_id)
        .bind(attempt)
        .execute(&self.pool)
        .await
        .map_err(|e| QueueError::Driver(format!("Failed to mark job failed: {}", e)))?;
        ensure_claim_transition(result.rows_affected(), job_id, "mark_failed", attempt)
    }

    /// Returns a processing job to pending, claimable from `available_at_ms`
    /// (`0` means immediately).
    async fn requeue_claim(
        &self,
        job_id: &str,
        attempt: Option<u32>,
        reason: &str,
        available_at_ms: i64,
    ) -> Result<(), QueueError> {
        let attempt = attempt.map(i64::from);
        let result = sqlx::query(
            "UPDATE rullst_jobs SET status = 'pending', error = ?, available_at_ms = ?, updated_at = datetime('now') WHERE id = ? AND status = 'processing' AND attempts = COALESCE(?, attempts)",
        )
        .bind(reason)
        .bind(available_at_ms)
        .bind(job_id)
        .bind(attempt)
        .execute(&self.pool)
        .await
        .map_err(|error| QueueError::StateTransition {
            job_id: job_id.to_string(),
            operation: "requeue",
            message: error.to_string(),
        })?;
        ensure_claim_transition(result.rows_affected(), job_id, "requeue", attempt)
    }
}

#[async_trait]
impl QueueDriver for SqliteDriver {
    async fn push(&self, id: &str, job_name: &str, payload: &str) -> Result<(), QueueError> {
        sqlx::query("INSERT INTO rullst_jobs (id, name, payload) VALUES (?, ?, ?)")
            .bind(id)
            .bind(job_name)
            .bind(payload)
            .execute(&self.pool)
            .await
            .map_err(|error| QueueError::Driver(format!("Failed to push job: {error}")))?;
        Ok(())
    }

    async fn push_at(
        &self,
        id: &str,
        job_name: &str,
        payload: &str,
        available_at: SystemTime,
    ) -> Result<(), QueueError> {
        let available_at_ms =
            i64::try_from(unix_timestamp_millis_ceil(available_at)?).map_err(|_| {
                QueueError::InvalidConfiguration(
                    "scheduled timestamp exceeds SQLite integer range".to_string(),
                )
            })?;
        sqlx::query(
            "INSERT INTO rullst_jobs (id, name, payload, available_at_ms) VALUES (?, ?, ?, ?)",
        )
        .bind(id)
        .bind(job_name)
        .bind(payload)
        .bind(available_at_ms)
        .execute(&self.pool)
        .await
        .map_err(|e| QueueError::Driver(format!("Failed to push job: {}", e)))?;
        Ok(())
    }

    async fn pop(&self) -> Result<Option<QueuedJob>, QueueError> {
        let now_ms =
            i64::try_from(unix_timestamp_millis_floor(SystemTime::now())?).map_err(|_| {
                QueueError::Driver("current timestamp exceeds SQLite integer range".to_string())
            })?;
        // Atomically select and mark the oldest pending job as 'processing'
        let row: Option<(String, String, String, i32)> = sqlx::query_as(
            r#"UPDATE rullst_jobs
               SET status = 'processing', attempts = attempts + 1, updated_at = datetime('now')
               WHERE id = (
                   SELECT id FROM rullst_jobs
                   WHERE status = 'pending' AND available_at_ms <= ?
                   ORDER BY available_at_ms ASC, created_at ASC LIMIT 1
               )
               RETURNING id, name, payload, attempts"#,
        )
        .bind(now_ms)
        .fetch_optional(&self.pool)
        .await
        .map_err(|e| QueueError::Driver(format!("Failed to pop job: {}", e)))?;

        let Some((id, name, payload_str, attempts)) = row else {
            return Ok(None);
        };

        let payload = match serde_json::from_str(&payload_str) {
            Ok(payload) => payload,
            Err(error) => {
                let message = format!("invalid JSON payload: {error}");
                self.mark_failed(&id, &message)
                    .await
                    .map_err(|transition| QueueError::StateTransition {
                        job_id: id.clone(),
                        operation: "reject_invalid_payload",
                        message: format!("{message}; {transition}"),
                    })?;
                return Err(QueueError::Serialization(format!(
                    "job '{id}' contains invalid JSON: {error}"
                )));
            }
        };
        let attempts = match u32::try_from(attempts) {
            Ok(attempts) => attempts,
            Err(_) => {
                let message = "job attempts counter is negative";
                self.mark_failed(&id, message).await.map_err(|transition| {
                    QueueError::StateTransition {
                        job_id: id.clone(),
                        operation: "reject_invalid_attempts",
                        message: transition.to_string(),
                    }
                })?;
                return Err(QueueError::Driver(format!("job '{id}' has {message}")));
            }
        };

        Ok(Some(QueuedJob {
            id,
            name,
            payload,
            attempts,
        }))
    }

    async fn mark_complete(&self, job_id: &str) -> Result<(), QueueError> {
        self.complete_claim(job_id, None).await
    }

    async fn mark_failed(&self, job_id: &str, error: &str) -> Result<(), QueueError> {
        self.fail_claim(job_id, None, error).await
    }

    async fn requeue(&self, job_id: &str, reason: &str) -> Result<(), QueueError> {
        self.requeue_claim(job_id, None, reason, 0).await
    }

    async fn mark_complete_attempt(&self, job_id: &str, attempt: u32) -> Result<(), QueueError> {
        self.complete_claim(job_id, Some(attempt)).await
    }

    async fn mark_failed_attempt(
        &self,
        job_id: &str,
        attempt: u32,
        error: &str,
    ) -> Result<(), QueueError> {
        self.fail_claim(job_id, Some(attempt), error).await
    }

    async fn requeue_attempt(
        &self,
        job_id: &str,
        attempt: u32,
        reason: &str,
    ) -> Result<(), QueueError> {
        self.requeue_claim(job_id, Some(attempt), reason, 0).await
    }

    async fn requeue_attempt_after(
        &self,
        job_id: &str,
        attempt: u32,
        reason: &str,
        delay: Duration,
    ) -> Result<(), QueueError> {
        let due = SystemTime::now().checked_add(delay).ok_or_else(|| {
            QueueError::InvalidConfiguration("requeue delay exceeds the clock range".to_string())
        })?;
        let available_at_ms = i64::try_from(unix_timestamp_millis_ceil(due)?).map_err(|_| {
            QueueError::InvalidConfiguration(
                "requeue timestamp exceeds SQLite integer range".to_string(),
            )
        })?;
        self.requeue_claim(job_id, Some(attempt), reason, available_at_ms)
            .await
    }

    async fn recover_stalled(&self, stale_after: Duration) -> Result<u64, QueueError> {
        recovery::recover_stalled(&self.pool, stale_after, self.max_stalled_leases).await
    }

    async fn pending_count(&self) -> Result<u64, QueueError> {
        let (count,): (i64,) =
            sqlx::query_as("SELECT COUNT(*) FROM rullst_jobs WHERE status = 'pending'")
                .fetch_one(&self.pool)
                .await
                .map_err(|e| QueueError::Driver(format!("Failed to count pending jobs: {}", e)))?;
        Ok(count as u64)
    }

    async fn list_all_jobs(&self, limit: u32) -> Result<Vec<QueuedJobDetail>, QueueError> {
        self.list_all_jobs(limit).await
    }

    async fn retry_failed_job(&self, job_id: &str) -> Result<(), QueueError> {
        self.retry_failed_job(job_id).await
    }

    async fn purge_completed_jobs(&self) -> Result<(), QueueError> {
        SqliteDriver::purge_failed_jobs(self).await
    }

    async fn purge_failed_jobs(&self) -> Result<(), QueueError> {
        SqliteDriver::purge_failed_jobs(self).await
    }

    async fn purge_completed_history(&self) -> Result<(), QueueError> {
        SqliteDriver::purge_completed_history(self).await
    }
}

/// Like [`ensure_transition`], naming the claim attempt when a fenced
/// transition matched nothing, which usually means a stale lease.
fn ensure_claim_transition(
    rows_affected: u64,
    job_id: &str,
    operation: &'static str,
    attempt: Option<i64>,
) -> Result<(), QueueError> {
    match attempt {
        Some(attempt) if rows_affected != 1 => Err(QueueError::StateTransition {
            job_id: job_id.to_string(),
            operation,
            message: format!(
                "expected one processing job at claim attempt {attempt}, affected {rows_affected}; \
                 the lease may have been recovered and claimed again"
            ),
        }),
        _ => ensure_transition(rows_affected, job_id, operation),
    }
}

fn ensure_transition(
    rows_affected: u64,
    job_id: &str,
    operation: &'static str,
) -> Result<(), QueueError> {
    if rows_affected == 1 {
        Ok(())
    } else {
        Err(QueueError::StateTransition {
            job_id: job_id.to_string(),
            operation,
            message: format!("expected one processing job, affected {rows_affected}"),
        })
    }
}