Skip to content

Commit

Permalink
bug: fix virtual-worker to not fail
Browse files Browse the repository at this point in the history
when there are no orders to process
  • Loading branch information
pauldotyu committed Jul 5, 2023
1 parent 86f8dfd commit 56ee758
Showing 1 changed file with 49 additions and 35 deletions.
84 changes: 49 additions & 35 deletions src/virtual-worker/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,43 +46,57 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {

match response {
Ok(res) => {
// parse the text into json
let json = res.text()?;

let orders: Vec<Order> = serde_json::from_str(&json)?;

// loop through the orders
for mut order in orders {
// update order status
order.status = OrderStatus::Processing as u32;

// send the order to the order service
let serialized_order = serde_json::to_string(&order)?;
let client = reqwest::blocking::Client::new();

let response = client
.put(format!("{}/order", order_service_url))
.header("Content-Type", "application/json")
.body(serialized_order.clone())
.send();

match response {
Ok(_res) => {
// track the time it takes to generate an order
let elapsed_time = start_time.elapsed();

// print the order details
println!(
"Order {} processed at {:.2?} with status of {}. {}",
order.order_id, elapsed_time, order.status, serialized_order
);
}
Err(err) => {
println!("Error completing the order: {}", err);
let json = res.text().unwrap();

if json == "null" {
println!("No orders to process");
} else {
println!("Processing orders");

let orders: Vec<Order> = serde_json::from_str(&json).unwrap();

if orders.len() == 0 {
println!("No orders to process");
} else {
println!("Processing {} orders", orders.len());

// loop through the orders
for mut order in orders {
// update order status
order.status = OrderStatus::Processing as u32;

// send the order to the order service
let serialized_order = serde_json::to_string(&order)?;
let client = reqwest::blocking::Client::new();

let response = client
.put(format!("{}/order", order_service_url))
.header("Content-Type", "application/json")
.body(serialized_order.clone())
.send();

match response {
Ok(_res) => {
// track the time it takes to generate an order
let elapsed_time = start_time.elapsed();

// print the order details
println!(
"Order {} processed at {:.2?} with status of {}. {}",
order.order_id,
elapsed_time,
order.status,
serialized_order
);
}
Err(err) => {
println!("Error completing the order: {}", err);
}
}

thread::sleep(sleep_duration);
}
}

thread::sleep(sleep_duration);
}
}
Err(e) => {
Expand Down

0 comments on commit 56ee758

Please sign in to comment.