-
Notifications
You must be signed in to change notification settings - Fork 269
Signal shutdown #1599
Signal shutdown #1599
Changes from all commits
610dd56
3387719
d46cd64
c8ca0e1
9c58876
12c3679
e2f4699
59c4912
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,14 +17,16 @@ | |
extern crate holochain_conductor_api; | ||
extern crate holochain_core_types; | ||
extern crate lib3h_sodium; | ||
extern crate signal_hook; | ||
extern crate structopt; | ||
|
||
use holochain_conductor_api::{ | ||
conductor::{mount_conductor_from_config, Conductor, CONDUCTOR}, | ||
config::{self, load_configuration, Configuration}, | ||
}; | ||
use holochain_core_types::error::HolochainError; | ||
use std::{fs::File, io::prelude::*, path::PathBuf, sync::Arc, thread::sleep, time::Duration}; | ||
use signal_hook::{iterator::Signals, SIGINT, SIGTERM}; | ||
use std::{fs::File, io::prelude::*, path::PathBuf, sync::Arc}; | ||
use structopt::StructOpt; | ||
|
||
#[derive(StructOpt, Debug)] | ||
|
@@ -43,6 +45,8 @@ fn main() { | |
.config | ||
.unwrap_or(config::default_persistence_dir().join("conductor-config.toml")); | ||
let config_path_str = config_path.to_str().unwrap(); | ||
let termination_signals = | ||
Signals::new(&[SIGINT, SIGTERM]).expect("Couldn't create signals list"); | ||
println!("Using config path: {}", config_path_str); | ||
match bootstrap_from_config(config_path_str) { | ||
Ok(()) => { | ||
|
@@ -66,9 +70,24 @@ fn main() { | |
.expect("Could not start UI servers!"); | ||
} | ||
|
||
// TODO wait for a SIGKILL or SIGINT instead here. | ||
loop { | ||
sleep(Duration::from_secs(1)) | ||
for _sig in termination_signals.forever() { | ||
let mut conductor_guard = CONDUCTOR.lock().unwrap(); | ||
let conductor = std::mem::replace(&mut *conductor_guard, None); | ||
let refs = Arc::strong_count(&CONDUCTOR); | ||
if refs == 1 { | ||
println!("Gracefully shutting down conductor..."); | ||
} else { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what's the difference between 'graceful' shutdown and 'explicit' shutdown? (context, I've lost the ability to fully track/understand some of the more sophisticated Rust code that's being written by y'all now) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess the idea is that if the reference count for the But I don't completely get the difference either since we take ownership of the conductor in line 75 with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, this bit is a little confusing, but I figured it's better to have something than nothing. I was thinking that other threads would still have access to a conductor in the explicit shutdown case, but @lucksus is right that the conductor gets dropped either way. However, if there are other threads referencing CONDUCTOR at this point, they would wind up with a |
||
println!( | ||
"Explicitly shutting down conductor. {} other threads were referencing it, so if unwrap errors follow, that might be why.", | ||
refs - 1 | ||
); | ||
conductor | ||
.expect("No conductor running") | ||
.shutdown() | ||
.expect("Error shutting down conductor"); | ||
} | ||
break; | ||
// NB: conductor is dropped here and should shut down itself | ||
} | ||
} | ||
Err(error) => println!("Error while trying to boot from config: {:?}", error), | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do we care about dropping the value in the conductor_guard?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@AshantiMutinta it will get dropped at the end of the block. What's your concern?