Skip to content

Commit 485a264

Browse files
committed
Fix doc tests
1 parent 24a770f commit 485a264

File tree

9 files changed

+94
-62
lines changed

9 files changed

+94
-62
lines changed

src/job.rs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -650,7 +650,7 @@ impl Job {
650650
/// .with_retry_strategy(RetryStrategy::exponential(
651651
/// Duration::from_secs(1),
652652
/// 2.0,
653-
/// Some(Duration::from_minutes(10))
653+
/// Some(Duration::from_secs(10 * 60))
654654
/// ));
655655
/// ```
656656
pub fn with_retry_strategy(mut self, strategy: RetryStrategy) -> Self {
@@ -681,7 +681,7 @@ impl Job {
681681
/// .with_exponential_backoff(
682682
/// Duration::from_secs(1),
683683
/// 2.0,
684-
/// Duration::from_minutes(10)
684+
/// Duration::from_secs(10 * 60)
685685
/// );
686686
/// ```
687687
pub fn with_exponential_backoff(
@@ -720,7 +720,7 @@ impl Job {
720720
/// .with_linear_backoff(
721721
/// Duration::from_secs(10),
722722
/// Duration::from_secs(10),
723-
/// Some(Duration::from_minutes(2))
723+
/// Some(Duration::from_secs(2 * 60))
724724
/// );
725725
/// ```
726726
pub fn with_linear_backoff(
@@ -753,7 +753,7 @@ impl Job {
753753
/// let job = Job::new("file_processing".to_string(), json!({"file": "data.csv"}))
754754
/// .with_fibonacci_backoff(
755755
/// Duration::from_secs(2),
756-
/// Some(Duration::from_minutes(5))
756+
/// Some(Duration::from_secs(5 * 60))
757757
/// );
758758
/// ```
759759
pub fn with_fibonacci_backoff(
@@ -1161,9 +1161,8 @@ impl Job {
11611161
/// use serde_json::json;
11621162
///
11631163
/// let job1 = Job::new("step1".to_string(), json!({"data": "step1"}));
1164-
/// let mut job2 = Job::new("step2".to_string(), json!({"data": "step2"}));
1165-
///
1166-
/// job2.depends_on(&job1.id);
1164+
/// let job2 = Job::new("step2".to_string(), json!({"data": "step2"}))
1165+
/// .depends_on(&job1.id);
11671166
/// assert!(job2.has_dependencies());
11681167
/// ```
11691168
pub fn depends_on(mut self, job_id: &JobId) -> Self {
@@ -1188,9 +1187,8 @@ impl Job {
11881187
///
11891188
/// let job1 = Job::new("step1".to_string(), json!({}));
11901189
/// let job2 = Job::new("step2".to_string(), json!({}));
1191-
/// let mut final_job = Job::new("final".to_string(), json!({}));
1192-
///
1193-
/// final_job.depends_on_jobs(&[job1.id, job2.id]);
1190+
/// let final_job = Job::new("final".to_string(), json!({}))
1191+
/// .depends_on_jobs(&[job1.id, job2.id]);
11941192
/// assert_eq!(final_job.depends_on.len(), 2);
11951193
/// ```
11961194
pub fn depends_on_jobs(mut self, job_ids: &[JobId]) -> Self {

src/lib.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
//! ## Quick Start
1919
//!
2020
//! ```rust,no_run
21-
//! use hammerwork::{Job, Worker, WorkerPool, JobQueue, Result, worker::JobHandler};
21+
//! use hammerwork::{Job, Worker, WorkerPool, JobQueue, Result, worker::JobHandler, queue::DatabaseQueue};
2222
//! use serde_json::json;
2323
//! use std::sync::Arc;
2424
//!
@@ -32,12 +32,8 @@
3232
//!
3333
//! let queue = Arc::new(JobQueue::new(pool));
3434
//!
35-
//! // Initialize database tables
36-
//! # #[cfg(any(feature = "postgres", feature = "mysql"))]
37-
//! {
38-
//! use hammerwork::queue::DatabaseQueue;
39-
//! queue.create_tables().await?;
40-
//! }
35+
//! // Note: Run database migrations first using `cargo hammerwork migrate`
36+
//! // or use the migration manager programmatically
4137
//!
4238
//! // Create job handler
4339
//! let handler: JobHandler = Arc::new(|job: Job| {

src/migrations/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
//! Once migrations are complete, your application simply connects to the database:
2525
//!
2626
//! ```rust,no_run
27-
//! use hammerwork::{Job, JobQueue, DatabaseQueue};
27+
//! use hammerwork::{Job, JobQueue, queue::DatabaseQueue};
2828
//! use serde_json::json;
2929
//! use std::sync::Arc;
3030
//!

src/queue/mod.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,8 +171,9 @@ pub trait DatabaseQueue: Send + Sync {
171171
/// use chrono::{Utc, Duration};
172172
///
173173
/// # async fn example(queue: &impl DatabaseQueue) -> hammerwork::Result<()> {
174+
/// # let job_id = uuid::Uuid::new_v4();
174175
/// let result_data = json!({"status": "success", "count": 42});
175-
/// let expires_at = Some(Utc::now() + Duration::hours(24));
176+
/// let expires_at = Some(Utc::now() + chrono::Duration::hours(24));
176177
///
177178
/// queue.store_job_result(job_id, result_data, expires_at).await?;
178179
/// # Ok(())
@@ -302,7 +303,7 @@ pub trait DatabaseQueue: Send + Sync {
302303
/// # Examples
303304
///
304305
/// ```rust,no_run
305-
/// use hammerwork::{JobQueue, Job};
306+
/// use hammerwork::{JobQueue, Job, queue::DatabaseQueue};
306307
/// use serde_json::json;
307308
///
308309
/// # #[tokio::main]

src/queue/mysql.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,8 +186,8 @@ impl DatabaseQueue for crate::queue::JobQueue<MySql> {
186186
sqlx::query(
187187
r#"
188188
INSERT INTO hammerwork_jobs
189-
(id, queue_name, payload, status, priority, attempts, max_attempts, timeout_seconds, created_at, scheduled_at, started_at, completed_at, failed_at, timed_out_at, error_message, cron_schedule, next_run_at, recurring, timezone, batch_id, result_storage_type, result_ttl_seconds, result_max_size_bytes)
190-
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
189+
(id, queue_name, payload, status, priority, attempts, max_attempts, timeout_seconds, created_at, scheduled_at, started_at, completed_at, failed_at, timed_out_at, error_message, cron_schedule, next_run_at, recurring, timezone, batch_id, result_storage_type, result_ttl_seconds, result_max_size_bytes, depends_on, dependents, dependency_status, workflow_id, workflow_name)
190+
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
191191
"#
192192
)
193193
.bind(job.id.to_string())
@@ -217,6 +217,11 @@ impl DatabaseQueue for crate::queue::JobQueue<MySql> {
217217
})
218218
.bind(job.result_config.ttl.map(|d| d.as_secs() as i64))
219219
.bind(job.result_config.max_size_bytes.map(|s| s as i64))
220+
.bind(serde_json::to_value(&job.depends_on)?)
221+
.bind(serde_json::to_value(&job.dependents)?)
222+
.bind(job.dependency_status.as_str())
223+
.bind(job.workflow_id.map(|id| id.to_string()))
224+
.bind(&job.workflow_name)
220225
.execute(&self.pool)
221226
.await?;
222227

src/queue/postgres.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,8 +183,9 @@ impl DatabaseQueue for crate::queue::JobQueue<Postgres> {
183183
id, queue_name, payload, status, priority, attempts, max_attempts,
184184
timeout_seconds, created_at, scheduled_at, error_message,
185185
cron_schedule, next_run_at, recurring, timezone, batch_id,
186-
result_storage_type, result_ttl_seconds, result_max_size_bytes
187-
) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19)
186+
result_storage_type, result_ttl_seconds, result_max_size_bytes,
187+
depends_on, dependents, dependency_status, workflow_id, workflow_name
188+
) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19, $20, $21, $22, $23, $24)
188189
"#,
189190
)
190191
.bind(job.id)
@@ -210,6 +211,11 @@ impl DatabaseQueue for crate::queue::JobQueue<Postgres> {
210211
})
211212
.bind(job.result_config.ttl.map(|d| d.as_secs() as i64))
212213
.bind(job.result_config.max_size_bytes.map(|s| s as i64))
214+
.bind(serde_json::to_value(&job.depends_on)?)
215+
.bind(serde_json::to_value(&job.dependents)?)
216+
.bind(job.dependency_status.as_str())
217+
.bind(job.workflow_id)
218+
.bind(&job.workflow_name)
213219
.execute(&self.pool)
214220
.await?;
215221

src/retry.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
//! .with_exponential_backoff(
2727
//! Duration::from_secs(1), // base delay
2828
//! 2.0, // multiplier
29-
//! Duration::from_minutes(10) // max delay
29+
//! Duration::from_secs(10 * 60) // max delay
3030
//! );
3131
//! ```
3232
//!
@@ -41,7 +41,7 @@
4141
//! let strategy = RetryStrategy::Exponential {
4242
//! base: Duration::from_secs(2),
4343
//! multiplier: 1.5,
44-
//! max_delay: Some(Duration::from_minutes(5)),
44+
//! max_delay: Some(Duration::from_secs(5 * 60)),
4545
//! jitter: Some(JitterType::Multiplicative(0.1)), // ±10% jitter
4646
//! };
4747
//!
@@ -160,14 +160,14 @@ impl JitterType {
160160
/// let linear = RetryStrategy::Linear {
161161
/// base: Duration::from_secs(10),
162162
/// increment: Duration::from_secs(10),
163-
/// max_delay: Some(Duration::from_minutes(5)),
163+
/// max_delay: Some(Duration::from_secs(5 * 60)),
164164
/// };
165165
///
166166
/// // Exponential backoff: 1s, 2s, 4s, 8s, 16s...
167167
/// let exponential = RetryStrategy::Exponential {
168168
/// base: Duration::from_secs(1),
169169
/// multiplier: 2.0,
170-
/// max_delay: Some(Duration::from_minutes(10)),
170+
/// max_delay: Some(Duration::from_secs(10 * 60)),
171171
/// jitter: None,
172172
/// };
173173
/// ```
@@ -213,7 +213,7 @@ pub enum RetryStrategy {
213213
/// let strategy = RetryStrategy::Linear {
214214
/// base: Duration::from_secs(5),
215215
/// increment: Duration::from_secs(10),
216-
/// max_delay: Some(Duration::from_minutes(2)),
216+
/// max_delay: Some(Duration::from_secs(2 * 60)),
217217
/// };
218218
///
219219
/// // Delays: 5s, 15s, 25s, 35s, 45s, 55s, 65s, 75s, 85s, 95s, 120s (capped)...
@@ -248,7 +248,7 @@ pub enum RetryStrategy {
248248
/// let strategy = RetryStrategy::Exponential {
249249
/// base: Duration::from_secs(1),
250250
/// multiplier: 2.0,
251-
/// max_delay: Some(Duration::from_minutes(10)),
251+
/// max_delay: Some(Duration::from_secs(10 * 60)),
252252
/// jitter: Some(JitterType::Multiplicative(0.1)), // ±10% jitter
253253
/// };
254254
///
@@ -284,7 +284,7 @@ pub enum RetryStrategy {
284284
///
285285
/// let strategy = RetryStrategy::Fibonacci {
286286
/// base: Duration::from_secs(2),
287-
/// max_delay: Some(Duration::from_minutes(5)),
287+
/// max_delay: Some(Duration::from_secs(5 * 60)),
288288
/// };
289289
///
290290
/// // Delays: 2s, 2s, 4s, 6s, 10s, 16s, 26s, 42s, 68s, 110s, 178s, 288s (capped at 300s)...
@@ -453,7 +453,7 @@ impl RetryStrategy {
453453
/// let strategy = RetryStrategy::linear(
454454
/// Duration::from_secs(10),
455455
/// Duration::from_secs(5),
456-
/// Some(Duration::from_minutes(2))
456+
/// Some(Duration::from_secs(2 * 60))
457457
/// );
458458
/// ```
459459
pub fn linear(base: Duration, increment: Duration, max_delay: Option<Duration>) -> Self {
@@ -481,7 +481,7 @@ impl RetryStrategy {
481481
/// let strategy = RetryStrategy::exponential(
482482
/// Duration::from_secs(1),
483483
/// 2.0,
484-
/// Some(Duration::from_minutes(10))
484+
/// Some(Duration::from_secs(10 * 60))
485485
/// );
486486
/// ```
487487
pub fn exponential(base: Duration, multiplier: f64, max_delay: Option<Duration>) -> Self {
@@ -511,7 +511,7 @@ impl RetryStrategy {
511511
/// let strategy = RetryStrategy::exponential_with_jitter(
512512
/// Duration::from_secs(1),
513513
/// 2.0,
514-
/// Some(Duration::from_minutes(10)),
514+
/// Some(Duration::from_secs(10 * 60)),
515515
/// JitterType::Multiplicative(0.1)
516516
/// );
517517
/// ```
@@ -544,7 +544,7 @@ impl RetryStrategy {
544544
///
545545
/// let strategy = RetryStrategy::fibonacci(
546546
/// Duration::from_secs(2),
547-
/// Some(Duration::from_minutes(5))
547+
/// Some(Duration::from_secs(5 * 60))
548548
/// );
549549
/// ```
550550
pub fn fibonacci(base: Duration, max_delay: Option<Duration>) -> Self {

src/worker.rs

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -677,7 +677,8 @@ where
677677
/// use hammerwork::Worker;
678678
/// use std::time::Duration;
679679
/// # use std::sync::Arc;
680-
/// # let pool = sqlx::PgPool::connect("postgresql://localhost/test").await.unwrap();
680+
/// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
681+
/// # let pool = sqlx::PgPool::connect("postgresql://localhost/test").await?;
681682
/// # let queue = Arc::new(hammerwork::JobQueue::new(pool));
682683
/// # let handler: hammerwork::worker::JobHandler = Arc::new(|job| Box::pin(async move { Ok(()) }));
683684
///
@@ -688,6 +689,8 @@ where
688689
/// // Lower frequency polling for reduced load
689690
/// let slow_worker = Worker::new(queue, "slow".to_string(), handler)
690691
/// .with_poll_interval(Duration::from_secs(5));
692+
/// # Ok(())
693+
/// # }
691694
/// ```
692695
pub fn with_poll_interval(mut self, interval: Duration) -> Self {
693696
self.poll_interval = interval;
@@ -709,13 +712,16 @@ where
709712
/// ```rust,no_run
710713
/// use hammerwork::Worker;
711714
/// # use std::sync::Arc;
712-
/// # let pool = sqlx::PgPool::connect("postgresql://localhost/test").await.unwrap();
715+
/// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
716+
/// # let pool = sqlx::PgPool::connect("postgresql://localhost/test").await?;
713717
/// # let queue = Arc::new(hammerwork::JobQueue::new(pool));
714718
/// # let handler: hammerwork::worker::JobHandler = Arc::new(|job| Box::pin(async move { Ok(()) }));
715719
///
716720
/// // Critical jobs get more retry attempts
717721
/// let critical_worker = Worker::new(queue, "critical".to_string(), handler)
718722
/// .with_max_retries(10);
723+
/// # Ok(())
724+
/// # }
719725
/// ```
720726
pub fn with_max_retries(mut self, max_retries: i32) -> Self {
721727
self.max_retries = max_retries;
@@ -737,13 +743,16 @@ where
737743
/// use hammerwork::Worker;
738744
/// use std::time::Duration;
739745
/// # use std::sync::Arc;
740-
/// # let pool = sqlx::PgPool::connect("postgresql://localhost/test").await.unwrap();
746+
/// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
747+
/// # let pool = sqlx::PgPool::connect("postgresql://localhost/test").await?;
741748
/// # let queue = Arc::new(hammerwork::JobQueue::new(pool));
742749
/// # let handler: hammerwork::worker::JobHandler = Arc::new(|job| Box::pin(async move { Ok(()) }));
743750
///
744751
/// // Longer delay for API rate limit recovery
745752
/// let api_worker = Worker::new(queue, "api".to_string(), handler)
746753
/// .with_retry_delay(Duration::from_secs(300)); // 5 minutes
754+
/// # Ok(())
755+
/// # }
747756
/// ```
748757
pub fn with_retry_delay(mut self, delay: Duration) -> Self {
749758
self.retry_delay = delay;
@@ -769,7 +778,8 @@ where
769778
/// use hammerwork::{Worker, retry::RetryStrategy};
770779
/// use std::time::Duration;
771780
/// # use std::sync::Arc;
772-
/// # let pool = sqlx::PgPool::connect("postgresql://localhost/test").await.unwrap();
781+
/// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
782+
/// # let pool = sqlx::PgPool::connect("postgresql://localhost/test").await?;
773783
/// # let queue = Arc::new(hammerwork::JobQueue::new(pool));
774784
/// # let handler: hammerwork::worker::JobHandler = Arc::new(|job| Box::pin(async move { Ok(()) }));
775785
///
@@ -778,8 +788,10 @@ where
778788
/// .with_default_retry_strategy(RetryStrategy::exponential(
779789
/// Duration::from_secs(1),
780790
/// 2.0,
781-
/// Some(Duration::from_minutes(10))
791+
/// Some(Duration::from_secs(10 * 60))
782792
/// ));
793+
/// # Ok(())
794+
/// # }
783795
/// ```
784796
pub fn with_default_retry_strategy(mut self, strategy: RetryStrategy) -> Self {
785797
self.default_retry_strategy = Some(strategy);
@@ -801,13 +813,16 @@ where
801813
/// use hammerwork::Worker;
802814
/// use std::time::Duration;
803815
/// # use std::sync::Arc;
804-
/// # let pool = sqlx::PgPool::connect("postgresql://localhost/test").await.unwrap();
816+
/// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
817+
/// # let pool = sqlx::PgPool::connect("postgresql://localhost/test").await?;
805818
/// # let queue = Arc::new(hammerwork::JobQueue::new(pool));
806819
/// # let handler: hammerwork::worker::JobHandler = Arc::new(|job| Box::pin(async move { Ok(()) }));
807820
///
808821
/// // Set 5 minute default timeout for all jobs
809822
/// let worker = Worker::new(queue, "processing".to_string(), handler)
810823
/// .with_default_timeout(Duration::from_secs(300));
824+
/// # Ok(())
825+
/// # }
811826
/// ```
812827
pub fn with_default_timeout(mut self, timeout: Duration) -> Self {
813828
self.default_timeout = Some(timeout);
@@ -886,12 +901,15 @@ where
886901
/// ```rust,no_run
887902
/// use hammerwork::Worker;
888903
/// # use std::sync::Arc;
889-
/// # let pool = sqlx::PgPool::connect("postgresql://localhost/test").await.unwrap();
904+
/// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
905+
/// # let pool = sqlx::PgPool::connect("postgresql://localhost/test").await?;
890906
/// # let queue = Arc::new(hammerwork::JobQueue::new(pool));
891907
/// # let handler: hammerwork::worker::JobHandler = Arc::new(|job| Box::pin(async move { Ok(()) }));
892908
///
893909
/// let worker = Worker::new(queue, "batch_queue".to_string(), handler)
894910
/// .with_batch_processing_enabled(true);
911+
/// # Ok(())
912+
/// # }
895913
/// ```
896914
pub fn with_batch_processing_enabled(mut self, enabled: bool) -> Self {
897915
self.batch_processing_enabled = enabled;

0 commit comments

Comments
 (0)