-
-
Notifications
You must be signed in to change notification settings - Fork 102
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor supervisors to carry out context to share data between children
- Loading branch information
1 parent
06d74ef
commit 26ac7e6
Showing
4 changed files
with
170 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
use bastion::prelude::*; | ||
use std::{fs, thread, time}; | ||
use log::LevelFilter; | ||
|
||
fn main() { | ||
let config = BastionConfig { | ||
log_level: LevelFilter::Debug, | ||
in_test: false | ||
}; | ||
|
||
Bastion::platform_from_config(config); | ||
|
||
|
||
let message = "Main Message".to_string(); | ||
|
||
// Name of the supervisor, and system of the new supervisor | ||
// By default if you don't specify Supervisors use "One for One". | ||
// We are going to take a look at "One For All" strategy. | ||
Bastion::supervisor("background-worker", "new-system") | ||
.strategy(SupervisionStrategy::OneForAll) | ||
.children( | ||
|p: BastionContext, _msg| { | ||
// Children spawned from the body of supervisor. | ||
println!("Started Child"); | ||
|
||
// Spawn child from the child context. | ||
// All rules apply at the supervisor level also to here. | ||
// Supervisor -> Child -> Child | ||
// \ | ||
// ---> Child | ||
for ctx_message in 1..=2 { | ||
p.clone().spawn(|sub_p: BastionContext, sub_msg: Box<dyn Message>| { | ||
receive! { sub_msg, | ||
i32 => |msg| { | ||
if msg == 1 { | ||
println!("First one"); | ||
panic!("Panic over the first one"); | ||
} else { | ||
println!("Second one"); | ||
} | ||
}, | ||
_ => println!("Message not known") | ||
} | ||
|
||
// Use blocking hook to commence restart of children | ||
// that has finished their jobs. | ||
sub_p.blocking_hook(); | ||
}, ctx_message, 1); | ||
} | ||
|
||
// Hook to rebind to the system. | ||
p.hook(); | ||
}, | ||
message, | ||
1_i32, | ||
) | ||
.launch(); | ||
|
||
Bastion::start() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters