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

Remove actor name arguments from StopSupervisor and restart_supervisor! #631

Merged
merged 2 commits into from
Apr 14, 2024
Merged
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: 0 additions & 1 deletion examples/4_restart_supervisor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ fn main() {
// Create a restart supervisor for the actors below.
restart_supervisor!(
PrintSupervisor, // Name of the supervisor type.
"print actor", // Name of the actor.
&'static str, // Argument for the actor.
5, // Maximum number of restarts.
Duration::from_secs(30), // Time to reset the max. reset counter.
Expand Down
2 changes: 1 addition & 1 deletion remote/src/net_relay/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
//!
//! // Next we're going to spawn our net relay actor.
//! // First it needs a supervisor.
//! restart_supervisor!(RelaySupervisor, "relay actor", SocketAddr);
//! restart_supervisor!(RelaySupervisor, SocketAddr);
//! let supervisor = RelaySupervisor::new(local_address);
//! // It needs a way to route all incoming messages, here we're direct them to
//! // our local actor using the `local_actor_ref`.
Expand Down
1 change: 0 additions & 1 deletion rt/examples/7_restart_supervisor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ fn main() -> Result<(), rt::Error> {
// Create a restart supervisor for the [`print_actor`].
restart_supervisor!(
PrintSupervisor,
"print actor",
String,
5,
Duration::from_secs(5),
Expand Down
8 changes: 3 additions & 5 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 All @@ -54,7 +52,7 @@ fn main() -> Result<(), rt::Error> {
runtime.start()
}

restart_supervisor!(ServerSupervisor, "TCP server actor", ());
restart_supervisor!(ServerSupervisor, ());

async fn conn_actor(_: actor::Context<!, ThreadLocal>, stream: TcpStream) -> io::Result<()> {
let address = stream.peer_addr()?;
Expand Down
15 changes: 8 additions & 7 deletions rt/tests/examples.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,36 +125,37 @@ fn test_6_process_signals() {
#[test]
fn test_7_restart_supervisor() {
// Index of the "?" in the string below.
const LEFT_INDEX: usize = 51;
const SYNC_LEFT_INDEX: usize = 56;
const ASYNC_LEFT_INDEX: usize = 51;

let output = run_example_output("7_restart_supervisor");
let mut lines = output.lines();

let mut expected = "lvl=\"WARN\" msg=\"print actor failed, restarting it (?/5 restarts left): can't print message synchronously 'Hello world!': actor message 'Hello world!'\" target=\"7_restart_supervisor\" module=\"7_restart_supervisor\"".to_owned();
let mut expected = "lvl=\"WARN\" msg=\"sync_print_actor failed, restarting it (?/5 restarts left): can't print message synchronously 'Hello world!': actor message 'Hello world!'\" target=\"7_restart_supervisor\" module=\"7_restart_supervisor\"".to_owned();
for left in (0..5).rev() {
let line = lines.next().unwrap();

unsafe {
expected.as_bytes_mut()[LEFT_INDEX] = b'0' + left;
expected.as_bytes_mut()[SYNC_LEFT_INDEX] = b'0' + left;
}
assert_eq!(line, expected);
}

let expected = "lvl=\"WARN\" msg=\"print actor failed, stopping it (no restarts left): can't print message synchronously 'Hello world!': actor message 'Hello world!'\" target=\"7_restart_supervisor\" module=\"7_restart_supervisor\"";
let expected = "lvl=\"WARN\" msg=\"sync_print_actor failed, stopping it (no restarts left): can't print message synchronously 'Hello world!': actor message 'Hello world!'\" target=\"7_restart_supervisor\" module=\"7_restart_supervisor\"";
let last_line = lines.next().unwrap();
assert_eq!(last_line, expected);

let mut expected = "lvl=\"WARN\" msg=\"print actor failed, restarting it (?/5 restarts left): can't print message 'Hello world!': actor message 'Hello world!'\" target=\"7_restart_supervisor\" module=\"7_restart_supervisor\"".to_owned();
let mut expected = "lvl=\"WARN\" msg=\"print_actor failed, restarting it (?/5 restarts left): can't print message 'Hello world!': actor message 'Hello world!'\" target=\"7_restart_supervisor\" module=\"7_restart_supervisor\"".to_owned();
for left in (0..5).rev() {
let line = lines.next().unwrap();

unsafe {
expected.as_bytes_mut()[LEFT_INDEX] = b'0' + left;
expected.as_bytes_mut()[ASYNC_LEFT_INDEX] = b'0' + left;
}
assert_eq!(line, expected);
}

let expected = "lvl=\"WARN\" msg=\"print actor failed, stopping it (no restarts left): can't print message 'Hello world!': actor message 'Hello world!'\" target=\"7_restart_supervisor\" module=\"7_restart_supervisor\"";
let expected = "lvl=\"WARN\" msg=\"print_actor failed, stopping it (no restarts left): can't print message 'Hello world!': actor message 'Hello world!'\" target=\"7_restart_supervisor\" module=\"7_restart_supervisor\"";
let last_line = lines.next().unwrap();
assert_eq!(last_line, expected);

Expand Down
55 changes: 18 additions & 37 deletions rt/tests/functional/restart_supervisor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const DEFAULT_MAX_DURATION: Duration = Duration::from_secs(5);

#[test]
fn new_actor_unit_argument() {
restart_supervisor!(Supervisor, "my actor");
restart_supervisor!(Supervisor);
// Should be able to create it without arguments passed to `new`.
let _supervisor = Supervisor::new();
assert_eq!(Supervisor::MAX_RESTARTS, DEFAULT_MAX_RESTARTS);
Expand All @@ -28,7 +28,7 @@ fn new_actor_unit_argument() {

#[test]
fn new_actor_unit_argument_explicit() {
restart_supervisor!(Supervisor, "my actor", ());
restart_supervisor!(Supervisor, ());
// Should be able to create it without arguments passed to `new`.
let _supervisor = Supervisor::new();
assert_eq!(Supervisor::MAX_RESTARTS, DEFAULT_MAX_RESTARTS);
Expand All @@ -37,7 +37,7 @@ fn new_actor_unit_argument_explicit() {

#[test]
fn new_actor_single_argument() {
restart_supervisor!(Supervisor, "my actor", String);
restart_supervisor!(Supervisor, String);
// Should be able to directly pass argument.
let _supervisor = Supervisor::new("Hello World".to_owned());
assert_eq!(Supervisor::MAX_RESTARTS, DEFAULT_MAX_RESTARTS);
Expand All @@ -46,7 +46,7 @@ fn new_actor_single_argument() {

#[test]
fn new_actor_tuple_argument() {
restart_supervisor!(Supervisor, "my actor", (String, usize));
restart_supervisor!(Supervisor, (String, usize));
// Should be able to directly pass argument.
let _supervisor = Supervisor::new("Hello World".to_owned(), 123);
assert_eq!(Supervisor::MAX_RESTARTS, DEFAULT_MAX_RESTARTS);
Expand All @@ -55,44 +55,31 @@ fn new_actor_tuple_argument() {

#[test]
fn no_log_unit_argument() {
restart_supervisor!(Supervisor, "my actor", (), 2, Duration::from_secs(10));
restart_supervisor!(Supervisor, (), 2, Duration::from_secs(10));
let _supervisor = Supervisor::new();
assert_eq!(Supervisor::MAX_RESTARTS, 2);
assert_eq!(Supervisor::MAX_DURATION, Duration::from_secs(10));
}

#[test]
fn no_log_single_argument() {
restart_supervisor!(Supervisor, "my actor", usize, 2, Duration::from_secs(10));
restart_supervisor!(Supervisor, usize, 2, Duration::from_secs(10));
let _supervisor = Supervisor::new(123);
assert_eq!(Supervisor::MAX_RESTARTS, 2);
assert_eq!(Supervisor::MAX_DURATION, Duration::from_secs(10));
}

#[test]
fn no_log_tuple_argument() {
restart_supervisor!(
Supervisor,
"my actor",
(u8, u16),
2,
Duration::from_secs(10)
);
restart_supervisor!(Supervisor, (u8, u16), 2, Duration::from_secs(10));
let _supervisor = Supervisor::new(123, 456);
assert_eq!(Supervisor::MAX_RESTARTS, 2);
assert_eq!(Supervisor::MAX_DURATION, Duration::from_secs(10));
}

#[test]
fn all_unit_argument() {
restart_supervisor!(
Supervisor,
"my actor",
(),
2,
Duration::from_secs(10),
": log extra",
);
restart_supervisor!(Supervisor, (), 2, Duration::from_secs(10), ": log extra",);
let _supervisor = Supervisor::new();
assert_eq!(Supervisor::MAX_RESTARTS, 2);
assert_eq!(Supervisor::MAX_DURATION, Duration::from_secs(10));
Expand All @@ -102,7 +89,6 @@ fn all_unit_argument() {
fn all_single_argument() {
restart_supervisor!(
Supervisor,
"my actor",
usize,
2,
Duration::from_secs(10),
Expand All @@ -118,7 +104,6 @@ fn all_single_argument() {
fn all_tuple_argument() {
restart_supervisor!(
Supervisor,
"my actor",
(u8, u16),
2,
Duration::from_secs(10),
Expand All @@ -133,46 +118,42 @@ fn all_tuple_argument() {

#[test]
fn tuple_2() {
restart_supervisor!(Supervisor, "my actor", (String, usize));
restart_supervisor!(Supervisor, (String, usize));
// Should be able to directly pass argument.
let _supervisor = Supervisor::new("Hello World".to_owned(), 123);
}

#[test]
fn tuple_3() {
restart_supervisor!(Supervisor, "my actor", (String, usize, u8));
restart_supervisor!(Supervisor, (String, usize, u8));
// Should be able to directly pass argument.
let _supervisor = Supervisor::new("Hello World".to_owned(), 123, 1);
}

#[test]
fn tuple_4() {
restart_supervisor!(Supervisor, "my actor", (String, usize, u8, &'static str));
restart_supervisor!(Supervisor, (String, usize, u8, &'static str));
// Should be able to directly pass argument.
let _supervisor = Supervisor::new("Hello World".to_owned(), 123, 1, "arg");
}

#[test]
fn tuple_5() {
restart_supervisor!(
Supervisor,
"my actor",
(String, usize, u8, &'static str, u8)
);
restart_supervisor!(Supervisor, (String, usize, u8, &'static str, u8));
// Should be able to directly pass argument.
let _supervisor = Supervisor::new("Hello World".to_owned(), 123, 1, "arg", 1);
}

#[test]
fn tuple_6() {
restart_supervisor!(Supervisor, "my actor", (String, usize, u8, u8, u8, u8));
restart_supervisor!(Supervisor, (String, usize, u8, u8, u8, u8));
// Should be able to directly pass argument.
let _supervisor = Supervisor::new("Hello World".to_owned(), 123, 1, 2, 3, 4);
}

#[test]
fn tuple_7() {
restart_supervisor!(Supervisor, "my actor", (String, usize, u8, u8, u8, u8, u8));
restart_supervisor!(Supervisor, (String, usize, u8, u8, u8, u8, u8));
// Need to use tuple format.
let _supervisor = Supervisor::new(("Hello World".to_owned(), 123, 1, 2, 3, 4, 5));
}
Expand Down Expand Up @@ -257,7 +238,7 @@ fn decide_for_restart_second<NA, S>(

#[test]
fn decide() {
restart_supervisor!(Supervisor, "my actor", bool, 1, Duration::from_secs(60));
restart_supervisor!(Supervisor, bool, 1, Duration::from_secs(60));

let arg = true;
let mut supervisor = Supervisor::new(arg);
Expand All @@ -274,7 +255,7 @@ fn decide() {

#[test]
fn decide_max_duration_elapsed() {
restart_supervisor!(Supervisor, "my actor", bool, 1, Duration::from_millis(100));
restart_supervisor!(Supervisor, bool, 1, Duration::from_millis(100));

let arg = true;
let mut supervisor = Supervisor::new(arg);
Expand Down Expand Up @@ -302,7 +283,7 @@ fn decide_max_duration_elapsed() {

#[test]
fn decide_on_restart_error() {
restart_supervisor!(Supervisor, "my actor", bool, 1, Duration::from_secs(60));
restart_supervisor!(Supervisor, bool, 1, Duration::from_secs(60));

let arg = true;
let mut supervisor = Supervisor::new(arg);
Expand All @@ -319,7 +300,7 @@ fn decide_on_restart_error() {

#[test]
fn decide_on_second_restart_error() {
restart_supervisor!(Supervisor, "my actor", bool, 1, Duration::from_secs(60));
restart_supervisor!(Supervisor, bool, 1, Duration::from_secs(60));

let arg = true;
let mut supervisor = Supervisor::new(arg);
Expand Down
Loading