Skip to content

Commit

Permalink
Rely on shutdown ack from the master when shutting down
Browse files Browse the repository at this point in the history
  • Loading branch information
isbm committed Dec 14, 2024
1 parent 21ceb9c commit 02d5b7c
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 5 deletions.
4 changes: 4 additions & 0 deletions libsysinspect/src/proto/rqtypes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ pub enum RequestType {
#[serde(rename = "b")]
Bye,

/// Bye Ack
#[serde(rename = "ba")]
ByeAck,

/// Retry connect (e.g. after the registration)
#[serde(rename = "retry")]
Reconnect,
Expand Down
13 changes: 12 additions & 1 deletion sysmaster/src/master.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,14 @@ impl SysMaster {
m
}

fn msg_bye_ack(&mut self, mid: String, sid: String) -> MasterMessage {
let mut m = MasterMessage::new(RequestType::ByeAck, json!(sid));
m.set_target(MinionTarget::new(&mid, &sid));
m.set_retcode(ProtoErrorCode::Success);

m
}

pub fn get_session(&self) -> Arc<Mutex<session::SessionKeeper>> {
Arc::clone(&self.session)
}
Expand Down Expand Up @@ -282,10 +290,13 @@ impl SysMaster {

RequestType::Bye => {
let c_master = Arc::clone(&master);
let c_bcast = bcast.clone();
log::info!("Minion {} disconnects", req.id());
tokio::spawn(async move {
let guard = c_master.lock().await;
let mut guard = c_master.lock().await;
guard.get_session().lock().await.remove(req.id());
let m = guard.msg_bye_ack(req.id().to_string(), req.payload().to_string());
_ = c_bcast.send(m.sendable().unwrap());
});
}

Expand Down
10 changes: 6 additions & 4 deletions sysminion/src/minion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use libsysinspect::{
SysinspectError,
};
use once_cell::sync::Lazy;
use std::{fs, sync::Arc, time::Duration, vec};
use std::{fs, sync::Arc, vec};
use tokio::io::AsyncReadExt;
use tokio::net::{tcp::OwnedReadHalf, TcpStream};
use tokio::sync::Mutex;
Expand Down Expand Up @@ -219,6 +219,10 @@ impl SysMinion {
RequestType::Ping => {
self.request(proto::msg::get_pong()).await;
}
RequestType::ByeAck => {
log::info!("Master confirmed shutdown, terminating");
std::process::exit(0);
}
_ => {
log::error!("Unknown request type");
}
Expand Down Expand Up @@ -388,10 +392,8 @@ impl SysMinion {
let cmd = cmd.strip_prefix(SCHEME_COMMAND).unwrap_or_default();
match cmd {
libsysinspect::proto::query::commands::CLUSTER_SHUTDOWN => {
log::info!("Shutting down minion");
log::info!("Requesting minion shutdown from a master");
self.as_ptr().send_bye().await;
tokio::time::sleep(Duration::from_secs(2)).await;
std::process::exit(0);
}
libsysinspect::proto::query::commands::CLUSTER_REBOOT => {
log::warn!("Command \"reboot\" is not implemented yet");
Expand Down

0 comments on commit 02d5b7c

Please sign in to comment.