Skip to content

Commit ee8557d

Browse files
committed
Add signal handlers for SIGTERM and SIGUSR1
Closes #71 SIGTERM now properly shuts down the system instead of aborting. SIGUSR1 will reload the config, but right now this only updates the encoding of data. More work is needed to actually reload the 0-db's used to encoded data. Signed-off-by: Lee Smet <[email protected]>
1 parent a927052 commit ee8557d

File tree

3 files changed

+38
-7
lines changed

3 files changed

+38
-7
lines changed

zstor/src/actors/config.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use crate::{
44
ZstorError,
55
};
66
use actix::prelude::*;
7-
use log::error;
7+
use log::{debug, error};
88
use std::{convert::TryInto, ops::Deref, path::PathBuf, sync::Arc};
99
use tokio::fs;
1010

@@ -73,6 +73,7 @@ impl Handler<ReloadConfig> for ConfigActor {
7373

7474
fn handle(&mut self, _: ReloadConfig, _: &mut Self::Context) -> Self::Result {
7575
let path = self.config_path.clone();
76+
debug!("Config actor reloading running config from {:?}", path);
7677
AtomicResponse::new(Box::pin(
7778
async move {
7879
fs::read(path)
@@ -83,6 +84,7 @@ impl Handler<ReloadConfig> for ConfigActor {
8384
.map(|res, this, _| {
8485
let config = toml::from_slice(&res?)?;
8586
this.config = Arc::new(config);
87+
debug!("Config actor finished reloading running config");
8688
Ok(())
8789
}),
8890
))

zstor/src/actors/zstor.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ use std::{
2121
};
2222
use tokio::{fs, io, task::JoinHandle};
2323

24+
use super::config::ReloadConfig;
25+
2426
#[derive(Serialize, Deserialize, Debug)]
2527
/// All possible commands zstor operates on.
2628
pub enum ZstorCommand {
@@ -365,6 +367,15 @@ impl Handler<Check> for ZstorActor {
365367
}
366368
}
367369

370+
impl Handler<ReloadConfig> for ZstorActor {
371+
type Result = ResponseFuture<Result<(), ZstorError>>;
372+
373+
fn handle(&mut self, _: ReloadConfig, _: &mut Self::Context) -> Self::Result {
374+
let cfg = self.cfg.clone();
375+
Box::pin(async move { cfg.send(ReloadConfig).await? })
376+
}
377+
}
378+
368379
async fn load_data(metadata: &MetaData) -> ZstorResult<Vec<Option<Vec<u8>>>> {
369380
// attempt to retrieve all shards
370381
let mut shard_loads: Vec<JoinHandle<(usize, Result<(_, _), ZstorError>)>> =

zstor/src/main.rs

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use actix::Addr;
2+
use actix_rt::signal::unix::SignalKind;
23
use futures::future::join_all;
34
use log::LevelFilter;
45
use log::{debug, error, info, trace};
@@ -16,6 +17,7 @@ use std::process;
1617
use structopt::StructOpt;
1718
use tokio::io::{AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt};
1819
use tokio::net::{UnixListener, UnixStream};
20+
use zstor_v2::actors::config::ReloadConfig;
1921
use zstor_v2::actors::zstor::{
2022
Check, Rebuild, Retrieve, Store, ZstorActor, ZstorCommand, ZstorResponse,
2123
};
@@ -314,6 +316,14 @@ async fn real_main() -> ZstorResult<()> {
314316

315317
let zstor = zstor_v2::setup_system(opts.config, cfg).await?;
316318

319+
// setup signal handlers
320+
let mut sigints = actix_rt::signal::unix::signal(SignalKind::interrupt())
321+
.expect("Failed to install SIGINT handler");
322+
let mut sigterms = actix_rt::signal::unix::signal(SignalKind::terminate())
323+
.expect("Failed to install SIGTERM handler");
324+
let mut siguserone = actix_rt::signal::unix::signal(SignalKind::user_defined1())
325+
.expect("Failed to install SIGUSR1 handler");
326+
317327
loop {
318328
tokio::select! {
319329
accepted = server.accept() => {
@@ -332,14 +342,22 @@ async fn real_main() -> ZstorResult<()> {
332342
}
333343
});
334344
}
335-
_ = actix_rt::signal::ctrl_c() => break,
345+
_ = sigints.recv() => {
346+
info!("Shutting down zstor daemon after receiving SIGINT");
347+
break
348+
},
349+
_ = sigterms.recv() => {
350+
info!("Shutting down zstor daemon after receiving SIGTERM");
351+
break
352+
},
353+
_ = siguserone.recv() => {
354+
info!("Reloading config after receiving SIGUSR1");
355+
if let Err(e) = zstor.send(ReloadConfig).await {
356+
error!("could not reload config: {}", e)
357+
}
358+
},
336359
}
337360
}
338-
actix_rt::signal::ctrl_c()
339-
.await
340-
.map_err(|e| ZstorError::new_io("Failed to wait for CTRL-C".to_string(), e))?;
341-
342-
info!("Shutting down zstor daemon after receiving CTRL-C");
343361
}
344362
};
345363

0 commit comments

Comments
 (0)