Skip to content

Add simulated failure handler and/or improve error message #239

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 25 additions & 3 deletions rust/templates/rust/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,42 @@ trait Greeter {
async fn greet(name: String) -> Result<String, HandlerError>;
}

#[restate_sdk::service]
trait GreeterFailure {
async fn greet(name: String) -> Result<String, HandlerError>;
}


struct GreeterImpl;
struct GreeterFailureImpl;

impl Greeter for GreeterImpl {
async fn greet(&self, mut ctx: Context<'_>, name: String) -> Result<String, HandlerError> {
// Durably execute a set of steps; resilient against failures
let greeting_id = ctx.rand_uuid().to_string();
ctx.run(|| send_notification(&greeting_id, &name)).await?;
ctx.run(|| send_notification(&greeting_id, &name, false)).await?;
ctx.sleep(Duration::from_millis(1000)).await?;
ctx.run(|| send_reminder(&greeting_id, false)).await?;

// Respond to caller
Ok(format!("Greetings {name}"))
}
}

impl GreeterFailure for GreeterFailureImpl {
async fn greet(&self, mut ctnx: Context<'_>, name: String) -> Result<String, HandlerError> {
// Durably execute a set of steps; resilient against failures
let greeting_id = ctx.rand_uuid().to_string();
ctx.run(|| send_notification(&greeting_id, &name, true)).await?;
ctx.sleep(Duration::from_millis(1000)).await?;
ctx.run(|| send_reminder(&greeting_id)).await?;
ctx.run(|| send_reminder(&greeting_id, true)).await?;

// Respond to caller
Ok(format!("Greetings {name}"))
}
}


#[tokio::main]
async fn main() {
// To enable logging
Expand All @@ -32,8 +53,9 @@ async fn main() {
HttpServer::new(
Endpoint::builder()
.bind(GreeterImpl.serve())
.bind(GreeterFailureImpl.serve())
.build(),
)
.listen_and_serve("0.0.0.0:9080".parse().unwrap())
.await;
}
}
18 changes: 9 additions & 9 deletions rust/templates/rust/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@ use rand::random;
use anyhow::{anyhow, Result};
use restate_sdk::errors::HandlerError;

pub async fn send_notification(greeting_id: &str, name: &str) -> Result<(), HandlerError> {
if random::<f32>() < 0.5 {
println!("👻 Failed to send notification: {} - {}", greeting_id, name);
return Err(HandlerError::from(anyhow!("Failed to send notification: {} - {}", greeting_id, name)));
pub async fn send_notification(greeting_id: &str, name: &str, simulate_faulure:bool) -> Result<(), HandlerError> {
if simulate_faulure && random::<f32>() < 0.5 {
println!("[👻 SIMULATED] Failed to send notification: {} - {}", greeting_id, name);
return Err(HandlerError::from(anyhow!("[SIMULATED] Failed to send notification: {} - {}", greeting_id, name)));
}
println!("Notification sent: {} - {}", greeting_id, name);
Ok(())
}

pub async fn send_reminder(greeting_id: &str)-> Result<(), HandlerError> {
if random::<f32>() < 0.5 {
println!("👻 Failed to send reminder: - {}", greeting_id);
return Err(HandlerError::from(anyhow!("Failed to send reminder: {}", greeting_id)));
pub async fn send_reminder(greeting_id: &str, simulate_faulure:bool)-> Result<(), HandlerError> {
if simulate_faulure && random::<f32>() < 0.5 {
println!("[👻 SIMULATED] Failed to send reminder: - {}", greeting_id);
return Err(HandlerError::from(anyhow!("[SIMULATED] Failed to send reminder: {}", greeting_id)));
}
println!("Reminder sent: {}", greeting_id);
Ok(())
}
}