-
Notifications
You must be signed in to change notification settings - Fork 127
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
watchdog
feature to allow cleanup on kill
The watchdog will spawn a background thread that registers signal handlers for SIGTERM, SIGINT, and SIGQUIT, if any such signal is observed it will clean up currently running containers that are not marked as `Keep`. Signed-off-by: Heinz N. Gies <heinz@licenser.net>
- Loading branch information
Showing
8 changed files
with
78 additions
and
0 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
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
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
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,52 @@ | ||
use crate::{clients::Cli, core::Docker}; | ||
use conquer_once::Lazy; | ||
use signal_hook::{ | ||
consts::{SIGINT, SIGQUIT, SIGTERM}, | ||
iterator::Signals, | ||
}; | ||
use std::{collections::BTreeSet, sync::Mutex, thread}; | ||
|
||
static WATCHDOG: Lazy<Mutex<Watchdog>> = Lazy::new(|| { | ||
thread::spawn(move || { | ||
let signal_docker = Cli::default(); | ||
let mut signals = | ||
Signals::new(&[SIGTERM, SIGINT, SIGQUIT]).expect("failed to register signal handler"); | ||
|
||
for signal in &mut signals { | ||
for container_id in WATCHDOG | ||
.lock() | ||
.map(|s| s.containers.clone()) | ||
.unwrap_or_default() | ||
{ | ||
signal_docker.stop(&container_id); | ||
signal_docker.rm(&container_id); | ||
} | ||
|
||
let _ = signal_hook::low_level::emulate_default_handler(signal); | ||
} | ||
}); | ||
|
||
Mutex::new(Watchdog::default()) | ||
}); | ||
|
||
#[derive(Default)] | ||
pub(crate) struct Watchdog { | ||
containers: BTreeSet<String>, | ||
} | ||
|
||
/// Register a container for observation | ||
pub(crate) fn register(container_id: String) { | ||
WATCHDOG | ||
.lock() | ||
.expect("failed to access watchdog") | ||
.containers | ||
.insert(container_id); | ||
} | ||
/// Unregisters a container for observation | ||
pub(crate) fn unregister(container_id: &str) { | ||
WATCHDOG | ||
.lock() | ||
.expect("failed to access watchdog") | ||
.containers | ||
.remove(container_id); | ||
} |