azums 0.1.1

High-performance job queue & streaming engine for Rust — from embedded to cloud
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
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
use crate::{
    backend::PostgresBackend,
    jobs::{
        model::{Job, NewJob},
        retry::classify_error,
        retry::{next_delay_seconds, ErrorClass, RetryConfig},
    },
};
use azums_core::StorageBackend;
use rand::{rngs::StdRng, SeedableRng};
use std::{collections::HashMap, pin::Pin, sync::Arc};
use tokio::sync::RwLock;
use uuid::Uuid;

pub type QuickstartHandlerFuture =
    Pin<Box<dyn std::future::Future<Output = anyhow::Result<()>> + Send>>;
pub type QuickstartHandler = Arc<dyn Fn(Job) -> QuickstartHandlerFuture + Send + Sync>;

/// Main entry point client for `postgresflow`.
pub type Client = QuickstartFlow;

/// In-process worker runtime and admin API launcher built by [`quickstart`].
///
/// `QuickstartFlow` manages job enqueueing, handler registration, background worker leasing loops,
/// and the optional Axum admin web console through an abstract [`StorageBackend`].
#[derive(Clone)]
pub struct QuickstartFlow {
    backend: Arc<dyn StorageBackend>,
    handlers: Arc<RwLock<HashMap<String, QuickstartHandler>>>,
    queue_configs: Arc<RwLock<HashMap<String, azums_core::QueueConfig>>>,
    queue: String,
    worker_id: String,
    lease_seconds: i64,
    retry_cfg: RetryConfig,
}

impl QuickstartFlow {
    /// Creates a `QuickstartFlow` wrapping a custom [`StorageBackend`].
    pub fn new(backend: Arc<dyn StorageBackend>) -> Self {
        let queue = std::env::var("AZUMS_QUEUE").unwrap_or_else(|_| "default".to_string());
        let worker_id =
            std::env::var("AZUMS_WORKER_ID").unwrap_or_else(|_| "quickstart-worker".to_string());
        let lease_seconds = std::env::var("AZUMS_LEASE_SECONDS")
            .ok()
            .and_then(|s| s.parse().ok())
            .unwrap_or(10);

        Self {
            backend,
            handlers: Arc::new(RwLock::new(HashMap::new())),
            queue_configs: Arc::new(RwLock::new(HashMap::new())),
            queue,
            worker_id,
            lease_seconds,
            retry_cfg: RetryConfig::default(),
        }
    }

    /// Sets the target queue name for this [`QuickstartFlow`] worker.
    pub fn with_queue(mut self, queue: impl Into<String>) -> Self {
        self.queue = queue.into();
        self
    }

    /// Sets the unique worker ID string for this [`QuickstartFlow`].
    pub fn with_worker_id(mut self, worker_id: impl Into<String>) -> Self {
        self.worker_id = worker_id.into();
        self
    }

    /// Sets the lease lock duration in seconds for leased jobs.
    pub fn with_lease_seconds(mut self, lease_seconds: i64) -> Self {
        self.lease_seconds = lease_seconds.max(1);
        self
    }

    /// Configures queue options (such as [`QueueOrdering`](azums_core::QueueOrdering)) for a specified queue.
    pub async fn configure_queue(&self, queue: impl Into<String>, config: azums_core::QueueConfig) {
        let mut configs = self.queue_configs.write().await;
        configs.insert(queue.into(), config);
    }

    /// Returns the active [`QueueConfig`](azums_core::QueueConfig) for a specified queue (defaults to FIFO).
    pub async fn get_queue_config(&self, queue: &str) -> azums_core::QueueConfig {
        let configs = self.queue_configs.read().await;
        configs.get(queue).cloned().unwrap_or_default()
    }

    /// Returns reference to the underlying [`StorageBackend`].
    pub fn backend(&self) -> &Arc<dyn StorageBackend> {
        &self.backend
    }

    /// Returns a [`StreamHandle`](crate::StreamHandle) for high-level Redis-style stream log operations.
    pub fn stream(&self, name: impl Into<String>) -> crate::stream_handle::StreamHandle {
        crate::stream_handle::StreamHandle::new(self.backend.clone(), name)
    }

    /// Enqueues a job into the storage backend queue.
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// use azums::{quickstart, Job};
    ///
    /// # async fn doc_test() -> anyhow::Result<()> {
    /// let flow = quickstart("postgres://localhost/flow").await?;
    /// let job_id = flow.enqueue(Job::new("send_email", serde_json::json!({"user_id": 42}))).await?;
    /// println!("Enqueued job: {job_id}");
    /// # Ok(())
    /// # }
    /// ```
    pub async fn enqueue(&self, job: impl Into<NewJob>) -> anyhow::Result<Uuid> {
        let new_job: NewJob = job.into();
        self.backend.enqueue(new_job).await
    }

    /// Enqueues multiple jobs in a batch into the queue backend.
    pub async fn enqueue_batch(
        &self,
        jobs: impl IntoIterator<Item = impl Into<NewJob>>,
    ) -> anyhow::Result<Vec<Uuid>> {
        let mut ids = Vec::new();
        for job in jobs {
            ids.push(self.enqueue(job).await?);
        }
        Ok(ids)
    }

    /// Registers an asynchronous handler closure for a specific `job_type`.
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// use azums::quickstart;
    ///
    /// # async fn doc_test() -> anyhow::Result<()> {
    /// let flow = quickstart("postgres://localhost/flow").await?;
    /// flow.register_handler("greet", |job| async move {
    ///     println!("Hello, {}!", job.payload["name"]);
    ///     Ok(())
    /// }).await;
    /// # Ok(())
    /// # }
    /// ```
    pub async fn register_handler<F, Fut>(&self, job_type: impl Into<String>, handler: F)
    where
        F: Fn(Job) -> Fut + Send + Sync + 'static,
        Fut: std::future::Future<Output = anyhow::Result<()>> + Send + 'static,
    {
        let job_type = job_type.into();
        let entry: QuickstartHandler = Arc::new(move |job: Job| {
            let fut = handler(job);
            Box::pin(fut) as QuickstartHandlerFuture
        });
        self.handlers.write().await.insert(job_type, entry);
    }

    /// Registers a trait-based [`JobProcessor`](azums_core::JobProcessor) for a specific `job_type`.
    pub async fn register_processor<P>(&self, job_type: impl Into<String>, processor: P)
    where
        P: azums_core::JobProcessor + 'static,
    {
        let processor = Arc::new(processor);
        self.register_handler(job_type, move |job| {
            let p = processor.clone();
            async move { p.process(job).await }
        })
        .await;
    }

    /// Gracefully shuts down background resources and connections.
    pub async fn shutdown(&self) -> anyhow::Result<()> {
        Ok(())
    }

    /// Performs database maintenance operations (such as PostgreSQL `VACUUM ANALYZE` or SQLite `PRAGMA incremental_vacuum`).
    pub async fn perform_maintenance(&self) -> anyhow::Result<()> {
        self.backend.perform_maintenance().await
    }

    /// Starts the in-process worker polling loop and admin HTTP API (if `api` feature is active).
    ///
    /// Runs continuously processing jobs until application termination.
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// use azums::quickstart;
    ///
    /// # async fn doc_test() -> anyhow::Result<()> {
    /// let flow = quickstart("postgres://localhost/flow").await?;
    /// flow.run().await?;
    /// # Ok(())
    /// # }
    /// ```
    pub async fn run(&self) -> anyhow::Result<()> {
        let token = tokio_util::sync::CancellationToken::new();
        self.run_with_shutdown(token).await
    }

    /// Starts the in-process worker polling loop with a `CancellationToken` for graceful shutdown.
    pub async fn run_with_shutdown(
        &self,
        shutdown_token: tokio_util::sync::CancellationToken,
    ) -> anyhow::Result<()> {
        use tokio_stream::StreamExt;

        let mut last_reap_at = std::time::Instant::now();
        let mut last_maint_at = std::time::Instant::now();
        let reap_interval = std::time::Duration::from_secs(5);
        let maint_interval = std::time::Duration::from_secs(300);
        let mut stream = self.backend.subscribe(&self.queue).await.ok();

        loop {
            if shutdown_token.is_cancelled() {
                break;
            }

            if last_reap_at.elapsed() >= reap_interval {
                let _ = self.backend.reap_expired_locks().await;
                last_reap_at = std::time::Instant::now();
            }

            if last_maint_at.elapsed() >= maint_interval {
                let _ = self.backend.perform_maintenance().await;
                last_maint_at = std::time::Instant::now();
            }

            let q_config = self.get_queue_config(&self.queue).await;
            let batch = self
                .backend
                .lease_jobs_batch_with_ordering(
                    &self.queue,
                    &self.worker_id,
                    self.lease_seconds,
                    32,
                    q_config.ordering,
                )
                .await?;

            if batch.is_empty() {
                if let Some(s) = stream.as_mut() {
                    tokio::select! {
                        _ = shutdown_token.cancelled() => break,
                        _ = s.next() => {},
                        _ = tokio::time::sleep(reap_interval) => {},
                    }
                } else {
                    tokio::select! {
                        _ = shutdown_token.cancelled() => break,
                        _ = tokio::time::sleep(std::time::Duration::from_millis(100)) => {},
                    }
                }
                continue;
            }

            self.process_batch(batch).await?;
        }

        Ok(())
    }

    /// Runs worker polling loops until all currently queued jobs have been processed, returning total count.
    ///
    /// Useful for batch processing, integration tests, or unit testing job flows.
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// use azums::quickstart;
    ///
    /// # async fn doc_test() -> anyhow::Result<()> {
    /// let flow = quickstart("postgres://localhost/flow").await?;
    /// let processed = flow.run_until_empty().await?;
    /// println!("Processed {processed} jobs");
    /// # Ok(())
    /// # }
    /// ```
    pub async fn run_until_empty(&self) -> anyhow::Result<usize> {
        let mut total_processed = 0;

        loop {
            let q_config = self.get_queue_config(&self.queue).await;
            let batch = self
                .backend
                .lease_jobs_batch_with_ordering(
                    &self.queue,
                    &self.worker_id,
                    self.lease_seconds,
                    32,
                    q_config.ordering,
                )
                .await?;

            if batch.is_empty() {
                break;
            }

            let count = batch.len();
            self.process_batch(batch).await?;
            total_processed += count;
        }

        Ok(total_processed)
    }

    async fn process_batch(&self, batch: Vec<Job>) -> anyhow::Result<()> {
        let dataset_ids: Vec<String> = batch.iter().map(|j| j.dataset_id.clone()).collect();
        let job_ids: Vec<Uuid> = batch.iter().map(|j| j.id).collect();

        let started_attempts = self
            .backend
            .start_attempts_batch(&dataset_ids, &job_ids, &self.worker_id)
            .await?;

        let mut attempts_map: HashMap<Uuid, (Uuid, i32)> = started_attempts
            .into_iter()
            .map(|(jid, aid, ano)| (jid, (aid, ano)))
            .collect();

        for job in batch {
            let (attempt_id, attempt_no) = match attempts_map.remove(&job.id) {
                Some(v) => v,
                None => continue,
            };

            let handler_opt = {
                let guard = self.handlers.read().await;
                guard.get(&job.job_type).cloned()
            };

            // Spawn background heartbeat task to extend lease for long-running jobs
            let (heartbeat_tx, mut heartbeat_rx) = tokio::sync::oneshot::channel::<()>();
            let backend_clone = self.backend.clone();
            let job_id = job.id;
            let worker_id_clone = self.worker_id.clone();
            let lease_secs = self.lease_seconds;

            let _hb_handle = tokio::spawn(async move {
                let interval_duration =
                    std::time::Duration::from_secs((lease_secs as u64 / 2).max(1));
                loop {
                    tokio::select! {
                        _ = &mut heartbeat_rx => break,
                        _ = tokio::time::sleep(interval_duration) => {
                            let _ = backend_clone.extend_lease(job_id, &worker_id_clone, lease_secs).await;
                        }
                    }
                }
            });

            let start = std::time::Instant::now();
            let res_outcome = match handler_opt.as_ref() {
                Some(handler) => {
                    let handler_clone = handler.clone();
                    let job_clone = job.clone();
                    let task_res =
                        tokio::task::spawn(async move { (handler_clone)(job_clone).await }).await;
                    match task_res {
                        Ok(res) => res,
                        Err(join_err) => {
                            if join_err.is_panic() {
                                let panic_msg =
                                    azums_core::format_panic_message(join_err.into_panic());
                                Err(anyhow::anyhow!("PANIC: {}", panic_msg))
                            } else {
                                Err(anyhow::anyhow!("task join error: {}", join_err))
                            }
                        }
                    }
                }
                None => Err(anyhow::anyhow!(
                    "no handler registered for job_type={}",
                    job.job_type
                )),
            };

            // Stop heartbeat task
            let _ = heartbeat_tx.send(());

            let latency_ms = start.elapsed().as_millis() as i32;
            match res_outcome {
                Ok(()) => {
                    self.backend
                        .mark_succeeded(job.id, attempt_id, &self.worker_id, latency_ms)
                        .await?;
                }
                Err(err) => {
                    let err_str = err.to_string();
                    let is_panic = err_str.starts_with("PANIC: ");
                    let (err_code, err_msg) = if is_panic {
                        ("PANIC", err_str.trim_start_matches("PANIC: "))
                    } else if handler_opt.is_some() {
                        ("HANDLER_ERROR", err_str.as_str())
                    } else {
                        ("UNKNOWN_JOB_TYPE", err_str.as_str())
                    };

                    if is_panic {
                        // Immediately route panicked job to DLQ
                        self.backend
                            .mark_dlq(
                                job.id,
                                attempt_id,
                                &self.worker_id,
                                latency_ms,
                                "PANIC",
                                err_code,
                                err_msg,
                                attempt_no,
                            )
                            .await?;
                    } else {
                        self.handle_failure(
                            job.id,
                            attempt_id,
                            latency_ms,
                            err_code,
                            err_msg,
                            attempt_no,
                            job.max_attempts,
                        )
                        .await?;
                    }
                }
            }
        }

        Ok(())
    }

    #[allow(clippy::too_many_arguments)]
    async fn handle_failure(
        &self,
        job_id: Uuid,
        attempt_id: Uuid,
        latency_ms: i32,
        error_code: &str,
        error_message: &str,
        attempt_no: i32,
        max_attempts: i32,
    ) -> anyhow::Result<()> {
        let class = classify_error(error_code);
        let can_retry = class == ErrorClass::Retryable && attempt_no < max_attempts;

        if can_retry {
            let mut rng = StdRng::from_entropy();
            let delay_secs = next_delay_seconds(attempt_no, &self.retry_cfg, &mut rng);
            let next_run_at = chrono::Utc::now() + chrono::Duration::seconds(delay_secs);

            self.backend
                .reschedule_for_retry(
                    job_id,
                    attempt_id,
                    &self.worker_id,
                    latency_ms,
                    next_run_at,
                    error_code,
                    error_message,
                    attempt_no,
                )
                .await
        } else {
            let reason_code = match class {
                ErrorClass::NonRetryable => "NON_RETRYABLE",
                ErrorClass::Retryable => "MAX_ATTEMPTS_EXCEEDED",
            };

            self.backend
                .mark_dlq(
                    job_id,
                    attempt_id,
                    &self.worker_id,
                    latency_ms,
                    reason_code,
                    error_code,
                    error_message,
                    attempt_no,
                )
                .await
        }
    }
}

/// Spawns an in-process worker and admin API with sensible connection defaults.
///
/// Automatically attempts database connections in order:
/// 1. Passed `database_url`
/// 2. `DATABASE_URL` environment variable
/// 3. `TEST_DATABASE_URL` environment variable
/// 4. Common local development Postgres instances (Docker Compose port 5433, local port 5432)
///
/// Runs all SQL schema migrations (`run_migrations`) on successful connection.
///
/// # Examples
///
/// ```rust,no_run
/// use azums::{quickstart, Job};
///
/// #[tokio::main]
/// async fn main() -> anyhow::Result<()> {
///     let flow = quickstart("postgres://localhost/flow").await?;
///     flow.enqueue(Job::new("greet", serde_json::json!({"name": "World"}))).await?;
///     flow.register_handler("greet", |job| async move {
///         println!("Hello, {}!", job.payload["name"]);
///         Ok(())
///     }).await;
///     flow.run().await?;
///     Ok(())
/// }
/// ```
pub async fn quickstart(database_url: impl AsRef<str>) -> anyhow::Result<QuickstartFlow> {
    let _ = dotenvy::dotenv();

    let user_url = database_url.as_ref().trim();

    if user_url == "memory"
        || user_url == "in-memory"
        || user_url.starts_with("memory:")
        || user_url.starts_with("memory://")
    {
        let mem_backend = azums_core::MemoryBackend::new();
        let backend: Arc<dyn StorageBackend> = Arc::new(mem_backend);
        return Ok(QuickstartFlow::new(backend));
    }

    #[cfg(feature = "sqlite")]
    if user_url.starts_with("sqlite:") {
        let pool = crate::backend::sqlite::make_sqlite_pool(user_url).await?;
        let sqlite_backend = crate::backend::SqliteBackend::new(pool);
        sqlite_backend.run_migrations().await?;
        let backend: Arc<dyn StorageBackend> = Arc::new(sqlite_backend);
        return Ok(QuickstartFlow::new(backend));
    }

    #[cfg(feature = "redis")]
    if user_url.starts_with("redis:") || user_url.starts_with("rediss:") {
        let redis_backend = azums_redis::RedisBackend::new(user_url).await?;
        redis_backend.run_migrations().await?;
        let backend: Arc<dyn StorageBackend> = Arc::new(redis_backend);
        return Ok(QuickstartFlow::new(backend));
    }

    let mut candidates = Vec::new();
    if !user_url.is_empty() && !user_url.starts_with("sqlite:") && !user_url.starts_with("memory") {
        candidates.push(user_url.to_string());
    }

    if let Ok(env_url) = std::env::var("DATABASE_URL") {
        let env_url = env_url.trim().to_string();
        if !env_url.is_empty() && !candidates.contains(&env_url) {
            candidates.push(env_url);
        }
    }

    if let Ok(test_url) = std::env::var("TEST_DATABASE_URL") {
        let test_url = test_url.trim().to_string();
        if !test_url.is_empty() && !candidates.contains(&test_url) {
            candidates.push(test_url);
        }
    }

    let defaults = [
        "postgres://postgres:postgres@127.0.0.1:5433/azums_dev",
        "postgres://postgres:postgres@127.0.0.1:5432/azums_dev",
        "postgres://postgres:postgres@localhost:5433/azums_dev",
        "postgres://postgres:postgres@localhost:5432/azums_dev",
        "postgres://postgres:postgres@127.0.0.1:5433/postgresflow_dev",
        "postgres://postgres:postgres@127.0.0.1:5432/postgresflow_dev",
        "postgres://postgres:postgres@127.0.0.1:5432/postgres",
        "postgres://postgres:root@127.0.0.1:5432/postgres",
        "postgres://postgres:admin@127.0.0.1:5432/postgres",
        "postgres://postgres:password@127.0.0.1:5432/postgres",
        "postgres://postgres:123456@127.0.0.1:5432/postgres",
        "postgres://postgres@127.0.0.1:5432/postgres",
        "postgres://127.0.0.1:5432/postgres",
        "postgres://localhost:5432/postgres",
    ];

    for def in defaults {
        let s = def.to_string();
        if !candidates.contains(&s) {
            candidates.push(s);
        }
    }

    let mut pool_opt = None;
    let mut last_err = None;

    for candidate in &candidates {
        let opts = sqlx::postgres::PgPoolOptions::new()
            .max_connections(4)
            .acquire_timeout(std::time::Duration::from_secs(2));

        match opts.connect(candidate).await {
            Ok(pool) => {
                if sqlx::query("SELECT 1").execute(&pool).await.is_ok() {
                    pool_opt = Some((candidate.clone(), pool));
                    break;
                }
            }
            Err(e) => {
                last_err = Some(e);
            }
        }
    }

    let (connected_url, pool) = match pool_opt {
        Some(p) => p,
        None => {
            return Err(anyhow::anyhow!(
                "Failed to connect to any PostgreSQL database. Tried candidates: {:?}. Last error: {:?}",
                candidates,
                last_err
            ));
        }
    };

    let pg_backend = PostgresBackend::new_with_url(pool, connected_url);
    pg_backend.run_migrations().await?;

    let backend: Arc<dyn StorageBackend> = Arc::new(pg_backend.clone());
    let flow = QuickstartFlow::new(backend);
    Ok(flow)
}