Skip to content

Commit

Permalink
Remove actor name from StopSupervisor
Browse files Browse the repository at this point in the history
Instead use the NewActor::name or SyncActor::name implementation.
  • Loading branch information
Thomasdezeeuw committed Apr 14, 2024
1 parent 242c8c6 commit c70b336
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 21 deletions.
6 changes: 2 additions & 4 deletions rt/examples/9_systemd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,8 @@ fn main() -> Result<(), rt::Error> {
Err(err) => return Err(rt::Error::setup(format!("failed to parse port: {err}"))),
};
let address = (Ipv4Addr::LOCALHOST, port).into();
let supervisor = StopSupervisor::for_actor("connection actor");
let actor = actor_fn(conn_actor);
let server = tcp::server::setup(address, supervisor, actor, ActorOptions::default())
let server = tcp::server::setup(address, StopSupervisor, actor, ActorOptions::default())
.map_err(rt::Error::setup)?;

let mut runtime = Runtime::setup()
Expand All @@ -33,12 +32,11 @@ fn main() -> Result<(), rt::Error> {

#[cfg(target_os = "linux")]
{
let supervisor = StopSupervisor::for_actor("systemd actor");
let actor = actor_fn(heph_rt::systemd::watchdog);
// NOTE: this should do a proper health check of you application.
let health_check = || -> Result<(), !> { Ok(()) };
let options = ActorOptions::default().with_priority(Priority::HIGH);
let systemd_ref = runtime.spawn(supervisor, actor, health_check, options);
let systemd_ref = runtime.spawn(StopSupervisor, actor, health_check, options);
runtime.receive_signals(systemd_ref.try_map());
}

Expand Down
30 changes: 13 additions & 17 deletions src/supervisor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -354,14 +354,7 @@ where
/// This supervisor can be used for quick prototyping or for one off actors that
/// shouldn't be restarted.
#[derive(Copy, Clone, Debug)]
pub struct StopSupervisor(&'static str);

impl StopSupervisor {
/// Create a new `StopSupervisor` for the actor with `actor_name`.
pub const fn for_actor(actor_name: &'static str) -> StopSupervisor {
StopSupervisor(actor_name)
}
}
pub struct StopSupervisor;

impl<NA> Supervisor<NA> for StopSupervisor
where
Expand All @@ -370,30 +363,31 @@ where
<NA::Actor as Actor>::Error: fmt::Display,
{
fn decide(&mut self, err: <NA::Actor as Actor>::Error) -> SupervisorStrategy<NA::Argument> {
warn!("{} failed, stopping it: {err}", self.0);
let name = NA::name();
warn!("{name} failed, stopping it: {err}");
SupervisorStrategy::Stop
}

fn decide_on_restart_error(&mut self, err: NA::Error) -> SupervisorStrategy<NA::Argument> {
// Shouldn't be called, but it should still have an implementation.
warn!("{} failed to restart, stopping it: {err}", self.0);
let name = NA::name();
warn!("{name} failed to restart, stopping it: {err}");
SupervisorStrategy::Stop
}

fn second_restart_error(&mut self, err: NA::Error) {
// Shouldn't be called, but it should still have an implementation.
warn!(
"{} failed to restart a second time, stopping it: {err}",
self.0
);
let name = NA::name();
warn!("{name} failed to restart a second time, stopping it: {err}");
}

fn decide_on_panic(
&mut self,
panic: Box<dyn Any + Send + 'static>,
) -> SupervisorStrategy<NA::Argument> {
let msg = panic_message(&*panic);
warn!("{} panicked, stopping it: {msg}", self.0);
let name = NA::name();
warn!("{name} panicked, stopping it: {msg}");
SupervisorStrategy::Stop
}
}
Expand All @@ -404,16 +398,18 @@ where
A::Error: fmt::Display,
{
fn decide(&mut self, err: A::Error) -> SupervisorStrategy<A::Argument> {
warn!("{} failed, stopping it: {err}", self.0);
let name = A::name();
warn!("{name} failed, stopping it: {err}");
SupervisorStrategy::Stop
}

fn decide_on_panic(
&mut self,
panic: Box<dyn Any + Send + 'static>,
) -> SupervisorStrategy<A::Argument> {
let name = A::name();
let msg = panic_message(&*panic);
warn!("{} panicked, stopping it: {msg}", self.0);
warn!("{name} panicked, stopping it: {msg}");
SupervisorStrategy::Stop
}
}
Expand Down

0 comments on commit c70b336

Please sign in to comment.