Skip to content

Commit

Permalink
feat: error! output from behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
Autoparallel committed Feb 25, 2024
1 parent 509b624 commit 2d09f54
Showing 1 changed file with 22 additions and 4 deletions.
26 changes: 22 additions & 4 deletions engine/src/machine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use anyhow::Result;
use arbiter_core::middleware::ArbiterMiddleware;
use futures_util::{Stream, StreamExt};
use tokio::task::JoinHandle;
use tracing::error;

use super::*;

Expand Down Expand Up @@ -216,23 +217,40 @@ where
async fn execute(&mut self, instruction: MachineInstruction) -> Result<()> {
// NOTE: The unwraps here are safe because the `Behavior` in an engine is only
// accessed here and it is private.
let mut id: Option<String>;
match instruction {
MachineInstruction::Start(client, messager) => {
id = messager.id.clone();
let id_clone = id.clone();
self.state = State::Starting;
let mut behavior = self.behavior.take().unwrap();
let behavior_task: JoinHandle<Result<(Option<EventStream<E>>, B)>> =
tokio::spawn(async move {
let id = messager.id.clone();
let stream = behavior.startup(client, messager).await?;
debug!("startup complete for behavior {:?}", id);
let stream = match behavior.startup(client, messager).await {
Ok(stream) => stream,
Err(e) => {
error!(
"startup failed for behavior {:?}: \n reason: {:?}",
id_clone, e
);
// Throw a panic as we cannot recover from this for now.
panic!();
}
};
debug!("startup complete for behavior {:?}", id_clone);
Ok((stream, behavior))
});
let (stream, behavior) = behavior_task.await??;
match stream {
Some(stream) => {
self.event_stream = Some(stream);
self.behavior = Some(behavior);
self.execute(MachineInstruction::Process).await?;
match self.execute(MachineInstruction::Process).await {
Ok(_) => {}
Err(e) => {
error!("process failed for behavior {:?}: \n reason: {:?}", id, e);
}
}
Ok(())
}
None => {
Expand Down

0 comments on commit 2d09f54

Please sign in to comment.