-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2145 from oasisprotocol/kostko/feature/rofl-watchdog
runtime-sdk/modules/rofl: Add watchdog task
- Loading branch information
Showing
6 changed files
with
92 additions
and
3 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[package] | ||
name = "rofl-containers" | ||
version = "0.3.3" | ||
version = "0.3.5" | ||
edition = "2021" | ||
|
||
[dependencies] | ||
|
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,77 @@ | ||
use anyhow::Result; | ||
use tokio::{sync::mpsc, time}; | ||
|
||
use crate::core::common::{logger::get_logger, process}; | ||
|
||
/// Interval in which at least one keep-alive must be delivered to avoid the watchdog from | ||
/// terminating the application. | ||
const WATCHDOG_TRIGGER_INTERVAL: u64 = 6 * 3600; // 6 hours | ||
|
||
/// Application watchdog task. | ||
pub(super) struct Task { | ||
imp: Option<Impl>, | ||
tx: mpsc::Sender<()>, | ||
} | ||
|
||
impl Task { | ||
/// Create an application watchdog task. | ||
pub(super) fn new() -> Self { | ||
let (tx, rx) = mpsc::channel(16); | ||
|
||
let imp = Impl { | ||
logger: get_logger("modules/rofl/app/watchdog"), | ||
notify: rx, | ||
}; | ||
|
||
Self { imp: Some(imp), tx } | ||
} | ||
|
||
/// Start the application watchdog task. | ||
pub(super) fn start(&mut self) { | ||
if let Some(imp) = self.imp.take() { | ||
imp.start(); | ||
} | ||
} | ||
|
||
/// Notify the watchdog that we are still alive. | ||
pub(super) async fn keep_alive(&self) -> Result<()> { | ||
self.tx.send(()).await?; | ||
Ok(()) | ||
} | ||
} | ||
|
||
struct Impl { | ||
logger: slog::Logger, | ||
|
||
notify: mpsc::Receiver<()>, | ||
} | ||
|
||
impl Impl { | ||
/// Start the application watchdog task. | ||
pub(super) fn start(self) { | ||
tokio::task::spawn(self.run()); | ||
} | ||
|
||
/// Run the application watchdog task. | ||
async fn run(mut self) { | ||
slog::info!(self.logger, "starting watchdog task"); | ||
|
||
loop { | ||
tokio::select! { | ||
Some(()) = self.notify.recv() => { | ||
// Keep-alive received, reset watchdog. | ||
}, | ||
|
||
_ = time::sleep(time::Duration::from_secs(WATCHDOG_TRIGGER_INTERVAL)) => { | ||
// Watchdog triggered, kill the process. | ||
slog::error!(self.logger, "keep-alive not received, terminating application"); | ||
process::abort(); | ||
}, | ||
|
||
else => break, | ||
} | ||
} | ||
|
||
slog::info!(self.logger, "watchdog task stopped"); | ||
} | ||
} |