tideway 0.7.17

A batteries-included Rust web framework built on Axum for building SaaS applications quickly
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
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
//! Redis-backed job queue implementation
//!
//! This implementation uses Redis lists and sorted sets for distributed
//! job processing. Multiple workers can compete for jobs from the same queue.

use crate::error::{Result, TidewayError};
use crate::traits::job::{Job, JobData, JobQueue};
use async_trait::async_trait;
#[cfg(feature = "jobs")]
use chrono::{DateTime, Duration, Utc};
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, Ordering};
use uuid::Uuid;

const PENDING_JOBS_KEY: &str = "jobs:pending";
const SCHEDULED_JOBS_KEY: &str = "jobs:scheduled";
const WORKERS_KEY: &str = "jobs:workers";
const PROCESSING_KEY_PREFIX: &str = "jobs:processing:";
const WORKER_HEARTBEAT_KEY_PREFIX: &str = "jobs:worker:heartbeat:";
const WORKER_HEARTBEAT_TTL_SECONDS: u64 = 30;

/// Atomically move all due scheduled jobs into the pending queue.
///
/// KEYS[1] = scheduled zset key
/// KEYS[2] = pending list key
/// ARGV[1] = unix timestamp cutoff
const MOVE_DUE_JOBS_LUA: &str = r#"
local scheduled_key = KEYS[1]
local pending_key = KEYS[2]
local cutoff = tonumber(ARGV[1])
local jobs = redis.call('ZRANGEBYSCORE', scheduled_key, '-inf', cutoff)

for _, job in ipairs(jobs) do
  redis.call('ZREM', scheduled_key, job)
  redis.call('LPUSH', pending_key, job)
end

return #jobs
"#;

/// Reclaim jobs from stale workers whose heartbeat has expired.
///
/// KEYS[1] = workers set key
/// KEYS[2] = pending list key
/// ARGV[1] = heartbeat key prefix
/// ARGV[2] = processing list key prefix
const RECLAIM_STALE_WORKERS_LUA: &str = r#"
local workers_key = KEYS[1]
local pending_key = KEYS[2]
local heartbeat_prefix = ARGV[1]
local processing_prefix = ARGV[2]
local reclaimed = 0

local workers = redis.call('SMEMBERS', workers_key)
for _, worker in ipairs(workers) do
  local heartbeat_key = heartbeat_prefix .. worker
  if redis.call('EXISTS', heartbeat_key) == 0 then
    local processing_key = processing_prefix .. worker
    while true do
      local job = redis.call('RPOPLPUSH', processing_key, pending_key)
      if not job then
        break
      end
      reclaimed = reclaimed + 1
    end
    redis.call('SREM', workers_key, worker)
  end
end

return reclaimed
"#;

fn processing_key(worker_id: &str) -> String {
    format!("{PROCESSING_KEY_PREFIX}{worker_id}")
}

fn worker_heartbeat_key(worker_id: &str) -> String {
    format!("{WORKER_HEARTBEAT_KEY_PREFIX}{worker_id}")
}

/// Redis-backed job queue implementation
///
/// Uses Redis data structures:
/// - `jobs:pending` - List of jobs ready to be processed
/// - `jobs:processing:{worker_id}` - List of jobs being processed by a worker
/// - `jobs:scheduled` - Sorted set of scheduled jobs (score = timestamp)
/// - `jobs:failed` - List of permanently failed jobs
/// - `jobs:completed` - List of completed jobs (optional, for history)
///
/// # Shutdown Behavior
///
/// The scheduler task runs in the background and will automatically stop when
/// `shutdown()` is called. Always call `shutdown()` before dropping the queue
/// to ensure clean resource cleanup.
#[derive(Clone)]
pub struct RedisJobQueue {
    client: redis::Client,
    worker_id: String,
    max_retries: u32,
    retry_backoff_seconds: u64,
    /// Cached health status (updated by ping operations)
    health_status: Arc<AtomicBool>,
    /// Shutdown flag for background scheduler task
    shutdown: Arc<AtomicBool>,
    /// Handle to the scheduler task for cleanup
    scheduler_handle: Arc<tokio::sync::Mutex<Option<tokio::task::JoinHandle<()>>>>,
}

impl RedisJobQueue {
    /// Create a new Redis job queue
    ///
    /// # Arguments
    ///
    /// * `url` - Redis connection URL (e.g., "redis://127.0.0.1:6379")
    /// * `worker_id` - Optional worker identifier (auto-generated if None)
    /// * `max_retries` - Maximum retry attempts for failed jobs
    /// * `retry_backoff_seconds` - Base backoff duration (exponentially increased)
    ///
    /// # Important
    ///
    /// Call `shutdown()` when done to cleanly stop background tasks.
    pub fn new(
        url: &str,
        worker_id: Option<String>,
        max_retries: u32,
        retry_backoff_seconds: u64,
    ) -> Result<Self> {
        let client = redis::Client::open(url)
            .map_err(|e| TidewayError::internal(format!("Failed to create Redis client: {}", e)))?;

        let worker_id = worker_id.unwrap_or_else(|| Uuid::new_v4().to_string());
        let shutdown = Arc::new(AtomicBool::new(false));

        let queue = Self {
            client,
            worker_id,
            max_retries,
            retry_backoff_seconds,
            health_status: Arc::new(AtomicBool::new(true)),
            shutdown,
            scheduler_handle: Arc::new(tokio::sync::Mutex::new(None)),
        };

        // Start background task to move scheduled jobs to pending
        queue.start_scheduler_task();

        Ok(queue)
    }

    /// Gracefully shutdown the job queue
    ///
    /// Signals the background scheduler task to stop and waits for it to finish.
    /// This should be called before dropping the queue to ensure clean cleanup.
    ///
    /// # Example
    ///
    /// ```rust,ignore
    /// let queue = RedisJobQueue::new("redis://localhost", None, 3, 5)?;
    /// // ... use the queue ...
    /// queue.shutdown().await;
    /// ```
    pub async fn shutdown(&self) {
        // Signal shutdown
        self.shutdown.store(true, Ordering::Release);

        // Wait for scheduler task to finish
        let mut handle_guard = self.scheduler_handle.lock().await;
        if let Some(handle) = handle_guard.take() {
            // Give the task a reasonable time to finish, then abort if needed
            match tokio::time::timeout(tokio::time::Duration::from_secs(5), handle).await {
                Ok(_) => tracing::debug!("Redis job queue scheduler stopped cleanly"),
                Err(_) => tracing::warn!("Redis job queue scheduler did not stop within timeout"),
            }
        }
    }

    /// Ping Redis and update health status
    ///
    /// Call this periodically (e.g., every 30 seconds) to keep health status accurate.
    /// The synchronous `is_healthy()` trait method returns the cached status from the
    /// last ping.
    ///
    /// # Example
    ///
    /// ```rust,ignore
    /// // Background health check task
    /// tokio::spawn(async move {
    ///     let mut interval = tokio::time::interval(Duration::from_secs(30));
    ///     loop {
    ///         interval.tick().await;
    ///         queue.ping().await;
    ///     }
    /// });
    /// ```
    pub async fn ping(&self) -> bool {
        match self.get_connection().await {
            Ok(mut conn) => {
                let result: redis::RedisResult<String> =
                    redis::cmd("PING").query_async(&mut conn).await;
                let healthy = result.is_ok();
                self.health_status.store(healthy, Ordering::Release);
                healthy
            }
            Err(e) => {
                tracing::warn!("Redis job queue ping failed: {}", e);
                self.health_status.store(false, Ordering::Release);
                false
            }
        }
    }

    /// Get a Redis connection
    async fn get_connection(&self) -> Result<redis::aio::MultiplexedConnection> {
        self.client
            .get_multiplexed_async_connection()
            .await
            .map_err(|e| TidewayError::internal(format!("Failed to get Redis connection: {}", e)))
    }

    /// Start background task to move scheduled jobs to pending
    ///
    /// This task polls the scheduled jobs set and moves due jobs to the pending list.
    /// It respects the shutdown flag and will exit cleanly when signaled.
    fn start_scheduler_task(&self) {
        let client = self.client.clone();
        let worker_id = self.worker_id.clone();
        let shutdown = self.shutdown.clone();
        let scheduler_handle = self.scheduler_handle.clone();

        let handle = tokio::spawn(async move {
            let mut interval = tokio::time::interval(tokio::time::Duration::from_secs(1));

            loop {
                // Check shutdown flag before waiting
                if shutdown.load(Ordering::Acquire) {
                    tracing::debug!("Redis job queue scheduler shutting down");
                    break;
                }

                interval.tick().await;

                // Check again after waking up
                if shutdown.load(Ordering::Acquire) {
                    break;
                }

                if let Ok(mut conn) = client.get_multiplexed_async_connection().await {
                    let heartbeat_key = worker_heartbeat_key(&worker_id);
                    let heartbeat_update: redis::RedisResult<()> = redis::pipe()
                        .cmd("SADD")
                        .arg(WORKERS_KEY)
                        .arg(&worker_id)
                        .ignore()
                        .cmd("SETEX")
                        .arg(&heartbeat_key)
                        .arg(WORKER_HEARTBEAT_TTL_SECONDS)
                        .arg("1")
                        .ignore()
                        .query_async(&mut conn)
                        .await;
                    if let Err(error) = heartbeat_update {
                        tracing::warn!(
                            error = %error,
                            worker_id = %worker_id,
                            "Failed to update Redis job worker heartbeat"
                        );
                        continue;
                    }

                    let reclaimed: redis::RedisResult<i64> =
                        redis::Script::new(RECLAIM_STALE_WORKERS_LUA)
                            .key(WORKERS_KEY)
                            .key(PENDING_JOBS_KEY)
                            .arg(WORKER_HEARTBEAT_KEY_PREFIX)
                            .arg(PROCESSING_KEY_PREFIX)
                            .invoke_async(&mut conn)
                            .await;
                    match reclaimed {
                        Ok(count) if count > 0 => {
                            tracing::warn!(
                                reclaimed_jobs = count,
                                worker_id = %worker_id,
                                "Reclaimed orphaned jobs from stale workers"
                            );
                        }
                        Ok(_) => {}
                        Err(error) => {
                            tracing::warn!(
                                error = %error,
                                worker_id = %worker_id,
                                "Failed to reclaim jobs from stale workers"
                            );
                        }
                    }

                    let now = Utc::now().timestamp();
                    let moved: redis::RedisResult<i64> = redis::Script::new(MOVE_DUE_JOBS_LUA)
                        .key(SCHEDULED_JOBS_KEY)
                        .key(PENDING_JOBS_KEY)
                        .arg(now)
                        .invoke_async(&mut conn)
                        .await;

                    if let Err(error) = moved {
                        tracing::warn!(
                            error = %error,
                            "Failed to move due scheduled jobs to pending queue"
                        );
                    }
                }
            }
        });

        // Store the handle for later cleanup
        // Note: We use try_lock here since start_scheduler_task is called from new()
        // which is not async. In practice, this will always succeed since we just created
        // the mutex.
        if let Ok(mut guard) = scheduler_handle.try_lock() {
            *guard = Some(handle);
        } else {
            // If we can't store the handle, abort it to prevent orphaned tasks
            handle.abort();
            tracing::error!("Failed to store scheduler handle - this should not happen");
        }
    }
}

#[async_trait]
impl JobQueue for RedisJobQueue {
    async fn enqueue(&self, job: &dyn Job) -> Result<String> {
        let job_id = Uuid::new_v4().to_string();
        let payload = job.serialize()?;

        let job_data = JobData::new(
            job_id.clone(),
            job.job_type().to_string(),
            payload,
            self.max_retries,
        );

        let job_json = serde_json::to_string(&job_data)
            .map_err(|e| TidewayError::internal(format!("Failed to serialize job: {}", e)))?;

        let mut conn = self.get_connection().await?;
        redis::cmd("LPUSH")
            .arg(PENDING_JOBS_KEY)
            .arg(&job_json)
            .query_async::<()>(&mut conn)
            .await
            .map_err(|e| TidewayError::internal(format!("Failed to enqueue job: {}", e)))?;

        Ok(job_id)
    }

    async fn dequeue(&self) -> Result<Option<JobData>> {
        let mut conn = self.get_connection().await?;
        let processing_key = processing_key(&self.worker_id);

        // Use BRPOPLPUSH for atomic move from pending to processing
        // This blocks for up to 5 seconds waiting for a job
        let result: Option<String> = redis::cmd("BRPOPLPUSH")
            .arg(PENDING_JOBS_KEY)
            .arg(&processing_key)
            .arg(5) // timeout in seconds
            .query_async(&mut conn)
            .await
            .map_err(|e| TidewayError::internal(format!("Failed to dequeue job: {}", e)))?;

        if let Some(job_json) = result {
            let job_data: JobData = serde_json::from_str(&job_json)
                .map_err(|e| TidewayError::internal(format!("Failed to deserialize job: {}", e)))?;
            Ok(Some(job_data))
        } else {
            Ok(None)
        }
    }

    async fn complete(&self, job_id: &str) -> Result<()> {
        let mut conn = self.get_connection().await?;
        let processing_key = processing_key(&self.worker_id);

        // Find and remove the job from processing list
        let jobs: Vec<String> = redis::cmd("LRANGE")
            .arg(&processing_key)
            .arg(0)
            .arg(-1)
            .query_async(&mut conn)
            .await
            .map_err(|e| {
                TidewayError::internal(format!("Failed to list processing jobs: {}", e))
            })?;

        for job_json in jobs {
            if let Ok(job_data) = serde_json::from_str::<JobData>(&job_json) {
                if job_data.job_id == job_id {
                    // Remove from processing
                    redis::cmd("LREM")
                        .arg(&processing_key)
                        .arg(1)
                        .arg(&job_json)
                        .query_async::<()>(&mut conn)
                        .await
                        .map_err(|e| {
                            TidewayError::internal(format!(
                                "Failed to remove job from processing: {}",
                                e
                            ))
                        })?;

                    // Optionally add to completed list (for history)
                    // redis::cmd("LPUSH").arg("jobs:completed").arg(&job_json).query_async(&mut conn).await?;

                    return Ok(());
                }
            }
        }

        Ok(())
    }

    async fn fail(&self, job_id: &str, _error: String) -> Result<()> {
        let mut conn = self.get_connection().await?;
        let processing_key = processing_key(&self.worker_id);

        // Find the job in processing list
        let jobs: Vec<String> = redis::cmd("LRANGE")
            .arg(&processing_key)
            .arg(0)
            .arg(-1)
            .query_async(&mut conn)
            .await
            .map_err(|e| {
                TidewayError::internal(format!("Failed to list processing jobs: {}", e))
            })?;

        for job_json in jobs {
            if let Ok(mut job_data) = serde_json::from_str::<JobData>(&job_json) {
                if job_data.job_id == job_id {
                    // Remove from processing
                    redis::cmd("LREM")
                        .arg(&processing_key)
                        .arg(1)
                        .arg(&job_json)
                        .query_async::<()>(&mut conn)
                        .await
                        .map_err(|e| {
                            TidewayError::internal(format!(
                                "Failed to remove job from processing: {}",
                                e
                            ))
                        })?;

                    if job_data.should_retry() {
                        // Schedule retry with exponential backoff
                        let backoff_seconds =
                            self.retry_backoff_seconds * (2_u64.pow(job_data.retry_count));
                        let retry_at = Utc::now() + Duration::seconds(backoff_seconds as i64);

                        job_data.increment_retry();

                        let retry_json = serde_json::to_string(&job_data).map_err(|e| {
                            TidewayError::internal(format!(
                                "Failed to serialize job for retry: {}",
                                e
                            ))
                        })?;

                        // Add to scheduled set
                        redis::cmd("ZADD")
                            .arg(SCHEDULED_JOBS_KEY)
                            .arg(retry_at.timestamp())
                            .arg(&retry_json)
                            .query_async::<()>(&mut conn)
                            .await
                            .map_err(|e| {
                                TidewayError::internal(format!("Failed to schedule retry: {}", e))
                            })?;
                    } else {
                        // Max retries exceeded, move to failed
                        redis::cmd("LPUSH")
                            .arg("jobs:failed")
                            .arg(&job_json)
                            .query_async::<()>(&mut conn)
                            .await
                            .map_err(|e| {
                                TidewayError::internal(format!(
                                    "Failed to add to failed list: {}",
                                    e
                                ))
                            })?;
                    }

                    return Ok(());
                }
            }
        }

        Ok(())
    }

    async fn retry(&self, job_id: &str) -> Result<()> {
        let mut conn = self.get_connection().await?;
        let processing_key = processing_key(&self.worker_id);

        // Find the job in processing list
        let jobs: Vec<String> = redis::cmd("LRANGE")
            .arg(&processing_key)
            .arg(0)
            .arg(-1)
            .query_async(&mut conn)
            .await
            .map_err(|e| {
                TidewayError::internal(format!("Failed to list processing jobs: {}", e))
            })?;

        for job_json in jobs {
            if let Ok(mut job_data) = serde_json::from_str::<JobData>(&job_json) {
                if job_data.job_id == job_id {
                    // Remove from processing
                    redis::cmd("LREM")
                        .arg(&processing_key)
                        .arg(1)
                        .arg(&job_json)
                        .query_async::<()>(&mut conn)
                        .await
                        .map_err(|e| {
                            TidewayError::internal(format!(
                                "Failed to remove job from processing: {}",
                                e
                            ))
                        })?;

                    if job_data.should_retry() {
                        job_data.increment_retry();
                        let retry_json = serde_json::to_string(&job_data).map_err(|e| {
                            TidewayError::internal(format!(
                                "Failed to serialize job for retry: {}",
                                e
                            ))
                        })?;

                        // Re-enqueue to pending
                        redis::cmd("LPUSH")
                            .arg(PENDING_JOBS_KEY)
                            .arg(&retry_json)
                            .query_async::<()>(&mut conn)
                            .await
                            .map_err(|e| {
                                TidewayError::internal(format!("Failed to retry job: {}", e))
                            })?;
                    } else {
                        // Max retries exceeded
                        redis::cmd("LPUSH")
                            .arg("jobs:failed")
                            .arg(&job_json)
                            .query_async::<()>(&mut conn)
                            .await
                            .map_err(|e| {
                                TidewayError::internal(format!(
                                    "Failed to add to failed list: {}",
                                    e
                                ))
                            })?;
                    }

                    return Ok(());
                }
            }
        }

        Ok(())
    }

    #[cfg(feature = "jobs")]
    async fn schedule(&self, job: &dyn Job, run_at: DateTime<Utc>) -> Result<String> {
        let job_id = Uuid::new_v4().to_string();
        let payload = job.serialize()?;

        let job_data = JobData::scheduled(
            job_id.clone(),
            job.job_type().to_string(),
            payload,
            self.max_retries,
            run_at,
        );

        let job_json = serde_json::to_string(&job_data)
            .map_err(|e| TidewayError::internal(format!("Failed to serialize job: {}", e)))?;

        let mut conn = self.get_connection().await?;
        redis::cmd("ZADD")
            .arg(SCHEDULED_JOBS_KEY)
            .arg(run_at.timestamp())
            .arg(&job_json)
            .query_async::<()>(&mut conn)
            .await
            .map_err(|e| TidewayError::internal(format!("Failed to schedule job: {}", e)))?;

        Ok(job_id)
    }

    fn is_healthy(&self) -> bool {
        // Return cached health status from last ping() call
        // Call ping() periodically via a background task for accurate status
        self.health_status.load(Ordering::Acquire)
    }
}