use std::future::{Future, IntoFuture};
use std::marker::PhantomData;
use std::sync::Arc;
use crate::BoxError;
use crate::OperationSubType;
use crate::RetryStrategy;
use crate::Serdes;
use crate::context::DurableContext;
use crate::error::OperationError;
use crate::future::DurableFuture;
use crate::serdes::JsonSerdes;
use crate::with_retry::{LoopFailure, RetryWrap};
#[must_use = "builders do nothing unless awaited or spawned"]
#[non_exhaustive]
pub struct WithRetryBuilder<O, F, Fut, S = JsonSerdes> {
ctx: DurableContext,
name: Option<String>,
wrap: RetryWrap,
sub_type: Option<OperationSubType>,
retry_strategy: Option<RetryStrategy>,
serdes: S,
closure: F,
summary: Option<crate::child::ChildSummary<O>>,
_marker: PhantomData<fn() -> (O, Fut)>,
}
impl<O, F, Fut, S> std::fmt::Debug for WithRetryBuilder<O, F, Fut, S> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("WithRetryBuilder")
.field("name", &self.name)
.finish_non_exhaustive()
}
}
impl<O, F, Fut> WithRetryBuilder<O, F, Fut>
where
O: Send + 'static,
F: Fn(DurableContext, u32) -> Fut + Send + Sync + 'static,
Fut: Future<Output = Result<O, BoxError>> + Send + 'static,
{
pub(crate) fn new_internal(ctx: DurableContext, closure: F) -> Self {
Self {
ctx,
name: None,
wrap: RetryWrap::Child,
sub_type: None,
retry_strategy: None,
serdes: JsonSerdes,
closure,
summary: None,
_marker: PhantomData,
}
}
}
impl<O, F, Fut, S> WithRetryBuilder<O, F, Fut, S>
where
O: Send + 'static,
F: Fn(DurableContext, u32) -> Fut + Send + Sync + 'static,
Fut: Future<Output = Result<O, BoxError>> + Send + 'static,
{
pub fn name(mut self, name: impl Into<String>) -> Self {
self.name = Some(name.into());
self
}
pub fn sub_type(mut self, sub_type: OperationSubType) -> Self {
self.sub_type = Some(sub_type);
self
}
pub fn virtual_context(mut self) -> Self {
self.wrap = RetryWrap::Virtual;
self
}
pub fn retry_strategy<R>(mut self, strategy: R) -> Self
where
R: Fn(&crate::StepError, u32) -> crate::RetryDecision + Send + Sync + 'static,
{
self.retry_strategy = Some(Box::new(strategy));
self
}
pub fn retry_strategy_config(mut self, config: crate::builders::RetryStrategyConfig) -> Self {
self.retry_strategy = Some(config.into_retry_strategy());
self
}
pub fn serdes<S2>(self, serdes: S2) -> WithRetryBuilder<O, F, Fut, S2>
where
S2: Serdes<O>,
{
WithRetryBuilder {
ctx: self.ctx,
name: self.name,
wrap: self.wrap,
sub_type: self.sub_type,
retry_strategy: self.retry_strategy,
serdes,
closure: self.closure,
summary: self.summary,
_marker: PhantomData,
}
}
pub fn summary(mut self, summary: impl Fn(&O) -> String + Send + Sync + 'static) -> Self {
self.summary = Some(Box::new(summary));
self
}
pub fn future(self) -> DurableFuture<O>
where
S: Serdes<O>,
{
<Self as IntoFuture>::into_future(self)
}
pub fn spawn(self) -> DurableFuture<O>
where
S: Serdes<O>,
{
spawn_terminal!(self, started)
}
}
impl<O, F, Fut, S> WithRetryBuilder<O, F, Fut, S>
where
O: Send + 'static,
F: Fn(DurableContext, u32) -> Fut + Send + Sync + 'static,
Fut: Future<Output = Result<O, BoxError>> + Send + 'static,
S: Serdes<O>,
{
fn build(self, started: bool) -> DurableFuture<O> {
match self.wrap {
RetryWrap::Child => self.build_child(started),
RetryWrap::Virtual => self.build_virtual(),
}
}
fn build_virtual(self) -> DurableFuture<O> {
use crate::child::ChildExecution;
let op_id = self.ctx.claim_operation_id("with_retry");
let sub_type = self
.sub_type
.unwrap_or(OperationSubType::RUN_IN_CHILD_CONTEXT);
if let Err(err) =
self.ctx
.reject_record_at_virtual_child(&op_id, sub_type.as_str(), self.name.as_deref())
{
return DurableFuture::from_async(async move { Err(err) });
}
let closure = Arc::new(self.closure);
let strategy: Arc<RetryStrategy> = Arc::new(
self.retry_strategy
.unwrap_or_else(crate::step::default_retry_strategy),
);
let serdes = Arc::new(self.serdes);
let loop_serdes = Arc::clone(&serdes);
let loop_name = self.name.clone();
let execution = ChildExecution {
ctx: self.ctx,
op_id,
name: self.name,
sub_type,
serdes,
user_function: crate::child::UserFunctionPolicy::Hooks,
summary: None,
closure: move |outer_ctx| async move {
crate::with_retry::retry_loop(
outer_ctx,
loop_name,
RetryWrap::Virtual,
closure,
strategy,
loop_serdes,
)
.await
.map_err(LoopFailure::into_child_error)
},
_marker: PhantomData,
};
DurableFuture::for_operation(&execution.ctx.clone(), execution.execute_virtual())
}
fn build_child(self, started: bool) -> DurableFuture<O> {
use crate::child::ChildExecution;
let op_id = self.ctx.claim_operation_id("with_retry");
if let Err(err) = self.ctx.preflight_replay_identity(
&op_id,
"Context",
Some(
self.sub_type
.as_ref()
.map_or(crate::child::CHILD_SUB_TYPE, OperationSubType::as_str),
),
self.name.as_deref(),
) {
return DurableFuture::from_async(async move { Err(err) });
}
let closure = Arc::new(self.closure);
let strategy: Arc<RetryStrategy> = Arc::new(
self.retry_strategy
.unwrap_or_else(crate::step::default_retry_strategy),
);
let serdes = Arc::new(self.serdes);
let loop_serdes = Arc::clone(&serdes);
let loop_name = self.name.clone();
let execution = ChildExecution {
ctx: self.ctx,
op_id,
name: self.name,
sub_type: self
.sub_type
.unwrap_or(OperationSubType::RUN_IN_CHILD_CONTEXT),
serdes,
user_function: crate::child::UserFunctionPolicy::Hooks,
summary: self.summary,
closure: move |outer_ctx| async move {
crate::with_retry::retry_loop(
outer_ctx,
loop_name,
RetryWrap::Child,
closure,
strategy,
loop_serdes,
)
.await
.map_err(LoopFailure::into_child_error)
},
_marker: PhantomData,
};
if started {
DurableFuture::for_started_operation(
&execution.ctx.clone(),
execution.execute_started(),
)
} else {
DurableFuture::for_start_step(&execution.ctx.clone(), move || {
execution.execute_started()
})
}
}
fn started_future(self) -> DurableFuture<O> {
self.build(true)
}
}
impl<O, F, Fut, S> IntoFuture for WithRetryBuilder<O, F, Fut, S>
where
O: Send + 'static,
F: Fn(DurableContext, u32) -> Fut + Send + Sync + 'static,
Fut: Future<Output = Result<O, BoxError>> + Send + 'static,
S: Serdes<O>,
{
type Output = Result<O, OperationError>;
type IntoFuture = DurableFuture<O>;
fn into_future(self) -> Self::IntoFuture {
self.build(false)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[tokio::test(start_paused = true)]
async fn with_retry_block_end_stops_direct_block_task() {
use std::sync::Mutex;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::time::Duration;
use crate::child::ChildExecution;
use crate::driver::InvocationOutcome;
use crate::driver::test_support::{RefreshClient, drive_handler, refresh_ctx, wire_err};
let client = Arc::new(RefreshClient::new(|_| Ok(Vec::new())));
let ctx = refresh_ctx(&client);
let effects = Arc::new(AtomicUsize::new(0));
let block_effects = Arc::clone(&effects);
let outcome = drive_handler(ctx, None, move |ctx| async move {
let holder: Arc<Mutex<Vec<DurableFuture<()>>>> = Arc::new(Mutex::new(Vec::new()));
let block_holder = Arc::clone(&holder);
let attempt = Arc::new(|_attempt_ctx: DurableContext, _attempt: u32| async {
Ok::<(), BoxError>(())
});
let execution = ChildExecution {
ctx: ctx.clone(),
op_id: ctx.claim_operation_id("with_retry"),
name: Some("block".to_owned()),
sub_type: OperationSubType::RUN_IN_CHILD_CONTEXT,
serdes: Arc::new(JsonSerdes),
user_function: crate::child::UserFunctionPolicy::Hooks,
summary: None,
closure: move |outer_ctx: DurableContext| async move {
let handle = outer_ctx
.step(move |_| async move {
tokio::time::sleep(Duration::from_millis(50)).await;
block_effects.fetch_add(1, Ordering::SeqCst);
Ok(())
})
.name("escaped")
.spawn();
block_holder.lock().expect("lock").push(handle);
tokio::time::sleep(Duration::from_millis(10)).await;
crate::with_retry::retry_loop(
outer_ctx,
Some("block".to_owned()),
RetryWrap::Child,
attempt,
Arc::new(crate::step::default_retry_strategy()),
Arc::new(JsonSerdes),
)
.await
.map_err(LoopFailure::into_child_error)
},
_marker: PhantomData,
};
execution.execute().await.map_err(wire_err)?;
ctx.step(|_| async {
tokio::time::sleep(Duration::from_millis(200)).await;
Ok(())
})
.name("keep")
.await
.map_err(wire_err)?;
drop(holder);
Ok("done".to_owned())
})
.await
.expect("no panic");
assert_eq!(outcome, InvocationOutcome::Complete("done".to_owned()));
assert_eq!(
effects.load(Ordering::SeqCst),
0,
"the escaped body stopped"
);
}
#[test]
fn serdes_keeps_wrap_mode() {
let ctx = DurableContext::test_context();
let virtual_block = ctx
.with_retry(|_child, _attempt| async { Ok(1_u32) })
.virtual_context()
.serdes(Arc::new(JsonSerdes));
assert_eq!(virtual_block.wrap, RetryWrap::Virtual);
let wrapped = ctx
.with_retry(|_child, _attempt| async { Ok(1_u32) })
.serdes(Arc::new(JsonSerdes));
assert_eq!(wrapped.wrap, RetryWrap::Child);
}
#[test]
fn sub_type_setter_replaces_and_survives_serdes() {
let ctx = DurableContext::test_context();
let builder = ctx
.with_retry(|_child, _attempt| async { Ok(1_u32) })
.sub_type(OperationSubType::new("First").unwrap())
.sub_type(OperationSubType::new("Second").unwrap())
.serdes(Arc::new(JsonSerdes));
assert_eq!(
builder.sub_type.as_ref().map(OperationSubType::as_str),
Some("Second")
);
}
#[test]
fn summary_setter_replaces_and_survives_serdes() {
let ctx = DurableContext::test_context();
let builder = ctx
.with_retry(|_child, _attempt| async { Ok(1_u32) })
.summary(|v: &u32| format!("first {v}"))
.summary(|v: &u32| format!("second {v}"))
.serdes(Arc::new(JsonSerdes));
let summary = builder.summary.as_ref().expect("the summary is kept");
assert_eq!(summary(&7), "second 7");
}
}