Skip to content
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

chore: Return a CancellationToken when shutting down an Actor #2

Closed
wants to merge 2 commits into from
Closed
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
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ edition = "2021"

[dependencies]
tokio = { version = "1.21.2", features = ["full"] }
tokio-util = "0.7.8"
theater_derive = { path = "./theater_derive" }
theater_types = { path = "./theater_types" }
async-trait = "0.1.58"
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/// Tiny, embeddable actor based system utilities
mod theater;
pub use theater::*;
pub use self::theater::*;
pub use theater_types::*;

// TODO: revisit derive API later
Expand Down
9 changes: 5 additions & 4 deletions src/theater.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::marker::PhantomData;
use async_trait::async_trait;
use theater_types::{Actor, ActorId, ActorLabel, ActorState, Handler, Result};
use tokio::sync::broadcast::Receiver;
use tokio_util::sync::CancellationToken;

#[derive(Debug, Clone)]
pub struct ActorImpl<H, M>
Expand All @@ -29,7 +30,7 @@ where
}
}

fn set_early_stop(&mut self, val: bool) {
fn _set_early_stop(&mut self, val: bool) {
self.stop_early = val
}
}
Expand All @@ -52,7 +53,7 @@ where
self.handler.status()
}

async fn start(&mut self, message_rx: &mut Receiver<M>) -> Result<()> {
async fn start(&mut self, message_rx: &mut Receiver<M>) -> Result<CancellationToken> {
self.handler.set_status(ActorState::Starting);

self.handler.on_start();
Expand All @@ -74,14 +75,14 @@ where
self.handler.set_status(ActorState::Terminating);
self.handler.on_stop();

return Ok(());
return Ok(CancellationToken::new());
}
},
}
}

self.handler.set_status(ActorState::Stopped);

Ok(())
Ok(CancellationToken::new())
}
}
1 change: 1 addition & 0 deletions theater_types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ edition = "2021"
syn = "1.0"
quote = "1.0"
tokio = { version = "1.21.2", features = ["full"] }
tokio-util = "0.7.8"
anyhow = "1.0.65"
thiserror = "1.0.37"
async-trait = "0.1.58"
4 changes: 2 additions & 2 deletions theater_types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ where
fn set_status(&mut self, state: ActorState);

/// Called every time a message is received by an actor
// async fn handle(&mut self, msg: impl std::fmt::Debug + Clone) -> Result<ActorState>;
async fn handle(&mut self, msg: M) -> Result<ActorState>;

/// Called before starting the message processing loop
Expand Down Expand Up @@ -68,5 +67,6 @@ where
/// Optional human-readable label
fn label(&self) -> ActorLabel;
fn status(&self) -> ActorState;
async fn start(&mut self, message_rx: &mut Receiver<M>) -> Result<()>;
async fn start(&mut self, message_rx: &mut Receiver<M>) -> Result<tokio_util::sync::CancellationToken>;
}