@@ -11,7 +11,8 @@ use uuid::Uuid;
1111use crate :: error:: { DurableError , DurableResult } ;
1212use crate :: task:: { Task , TaskRegistry } ;
1313use crate :: types:: {
14- CancellationPolicy , RetryStrategy , SpawnOptions , SpawnResult , SpawnResultRow , WorkerOptions ,
14+ CancellationPolicy , RetryStrategy , SpawnDefaults , SpawnOptions , SpawnResult , SpawnResultRow ,
15+ WorkerOptions ,
1516} ;
1617
1718/// Internal struct for serializing spawn options to the database.
@@ -110,7 +111,7 @@ where
110111 pool : PgPool ,
111112 owns_pool : bool ,
112113 queue_name : String ,
113- default_max_attempts : u32 ,
114+ spawn_defaults : SpawnDefaults ,
114115 registry : Arc < RwLock < TaskRegistry < State > > > ,
115116 state : State ,
116117}
@@ -120,11 +121,23 @@ where
120121/// # Example
121122///
122123/// ```ignore
124+ /// use std::time::Duration;
125+ /// use durable::{Durable, RetryStrategy, CancellationPolicy};
126+ ///
123127/// // Without state
124128/// let client = Durable::builder()
125129/// .database_url("postgres://localhost/myapp")
126130/// .queue_name("orders")
127131/// .default_max_attempts(3)
132+ /// .default_retry_strategy(RetryStrategy::Exponential {
133+ /// base_delay: Duration::from_secs(5),
134+ /// factor: 2.0,
135+ /// max_backoff: Duration::from_secs(300),
136+ /// })
137+ /// .default_cancellation(CancellationPolicy {
138+ /// max_pending_time: Some(Duration::from_secs(3600)),
139+ /// max_running_time: None,
140+ /// })
128141/// .build()
129142/// .await?;
130143///
@@ -138,7 +151,7 @@ pub struct DurableBuilder {
138151 database_url : Option < String > ,
139152 pool : Option < PgPool > ,
140153 queue_name : String ,
141- default_max_attempts : u32 ,
154+ spawn_defaults : SpawnDefaults ,
142155}
143156
144157impl DurableBuilder {
@@ -147,7 +160,11 @@ impl DurableBuilder {
147160 database_url : None ,
148161 pool : None ,
149162 queue_name : "default" . to_string ( ) ,
150- default_max_attempts : 5 ,
163+ spawn_defaults : SpawnDefaults {
164+ max_attempts : 5 ,
165+ retry_strategy : None ,
166+ cancellation : None ,
167+ } ,
151168 }
152169 }
153170
@@ -171,7 +188,19 @@ impl DurableBuilder {
171188
172189 /// Set default max attempts for spawned tasks (default: 5)
173190 pub fn default_max_attempts ( mut self , attempts : u32 ) -> Self {
174- self . default_max_attempts = attempts;
191+ self . spawn_defaults . max_attempts = attempts;
192+ self
193+ }
194+
195+ /// Set default retry strategy for spawned tasks (default: Fixed with 5s delay)
196+ pub fn default_retry_strategy ( mut self , strategy : RetryStrategy ) -> Self {
197+ self . spawn_defaults . retry_strategy = Some ( strategy) ;
198+ self
199+ }
200+
201+ /// Set default cancellation policy for spawned tasks (default: no auto-cancellation)
202+ pub fn default_cancellation ( mut self , policy : CancellationPolicy ) -> Self {
203+ self . spawn_defaults . cancellation = Some ( policy) ;
175204 self
176205 }
177206
@@ -226,7 +255,7 @@ impl DurableBuilder {
226255 pool,
227256 owns_pool,
228257 queue_name : self . queue_name ,
229- default_max_attempts : self . default_max_attempts ,
258+ spawn_defaults : self . spawn_defaults ,
230259 registry : Arc :: new ( RwLock :: new ( HashMap :: new ( ) ) ) ,
231260 state,
232261 } )
@@ -471,7 +500,19 @@ where
471500 #[ cfg( feature = "telemetry" ) ]
472501 tracing:: Span :: current ( ) . record ( "queue" , & self . queue_name ) ;
473502
474- let max_attempts = options. max_attempts . unwrap_or ( self . default_max_attempts ) ;
503+ // Apply defaults if not set
504+ let max_attempts = options
505+ . max_attempts
506+ . unwrap_or ( self . spawn_defaults . max_attempts ) ;
507+ let options = SpawnOptions {
508+ retry_strategy : options
509+ . retry_strategy
510+ . or_else ( || self . spawn_defaults . retry_strategy . clone ( ) ) ,
511+ cancellation : options
512+ . cancellation
513+ . or_else ( || self . spawn_defaults . cancellation . clone ( ) ) ,
514+ ..options
515+ } ;
475516
476517 let db_options = Self :: serialize_spawn_options ( & options, max_attempts) ?;
477518
@@ -649,6 +690,7 @@ where
649690 self . registry . clone ( ) ,
650691 options,
651692 self . state . clone ( ) ,
693+ self . spawn_defaults . clone ( ) ,
652694 )
653695 . await )
654696 }
0 commit comments