attaching file watcher to upgrade-plan.json

This commit is contained in:
Jędrzej Stuczyński
2023-11-02 17:08:17 +00:00
parent 4a98631e93
commit aaeb6a7cbf
7 changed files with 41 additions and 16 deletions
Generated
+1
View File
@@ -7602,6 +7602,7 @@ dependencies = [
"async-file-watcher",
"clap 4.4.7",
"dotenvy",
"futures",
"humantime 2.1.0",
"humantime-serde",
"lazy_static",
+8 -6
View File
@@ -10,6 +10,8 @@ use std::path::{Path, PathBuf};
use std::time::Duration;
use tokio::time::Instant;
pub use notify::{Error as NotifyError, Result as NotifyResult};
pub type FileWatcherEventSender = mpsc::UnboundedSender<Event>;
pub type FileWatcherEventReceiver = mpsc::UnboundedReceiver<Event>;
@@ -22,7 +24,7 @@ pub struct AsyncFileWatcher {
last_received: HashMap<EventKind, Instant>,
tick_duration: Duration,
inner_rx: mpsc::UnboundedReceiver<notify::Result<Event>>,
inner_rx: mpsc::UnboundedReceiver<NotifyResult<Event>>,
event_sender: FileWatcherEventSender,
}
@@ -30,7 +32,7 @@ impl AsyncFileWatcher {
pub fn new_file_changes_watcher<P: AsRef<Path>>(
path: P,
event_sender: FileWatcherEventSender,
) -> notify::Result<Self> {
) -> NotifyResult<Self> {
Self::new(
path,
event_sender,
@@ -48,7 +50,7 @@ impl AsyncFileWatcher {
event_sender: FileWatcherEventSender,
filters: Option<Vec<EventKind>>,
tick_duration: Option<Duration>,
) -> notify::Result<Self> {
) -> NotifyResult<Self> {
let watcher_config = Config::default();
let (inner_tx, inner_rx) = mpsc::unbounded();
let watcher = RecommendedWatcher::new(
@@ -112,17 +114,17 @@ impl AsyncFileWatcher {
false
}
fn start_watching(&mut self) -> notify::Result<()> {
fn start_watching(&mut self) -> NotifyResult<()> {
self.is_watching = true;
self.watcher.watch(&self.path, RecursiveMode::NonRecursive)
}
fn stop_watching(&mut self) -> notify::Result<()> {
fn stop_watching(&mut self) -> NotifyResult<()> {
self.is_watching = false;
self.watcher.unwatch(&self.path)
}
pub async fn watch(&mut self) -> notify::Result<()> {
pub async fn watch(&mut self) -> NotifyResult<()> {
self.start_watching()?;
while let Some(event) = self.inner_rx.next().await {
+1
View File
@@ -14,6 +14,7 @@ license.workspace = true
anyhow = { workspace = true }
clap = { workspace = true, features = ["derive"] }
dotenvy = { workspace = true }
futures = { workspace = true }
humantime = "2.1.0"
humantime-serde = "1.1.1"
lazy_static = { workspace = true }
+18 -6
View File
@@ -5,6 +5,9 @@ use crate::cli::try_load_current_config;
use crate::daemon::Daemon;
use crate::env::Env;
use crate::error::NymvisorError;
use async_file_watcher::AsyncFileWatcher;
use futures::channel::mpsc;
use futures::StreamExt;
use nym_bin_common::logging::setup_tracing_logger;
use std::sync::Arc;
use std::time::Duration;
@@ -41,23 +44,32 @@ pub(crate) fn execute(args: Args) -> Result<(), NymvisorError> {
rt.block_on(async {
println!("run");
let daemon = Daemon::from_config(&config).with_kill_timeout(Duration::from_millis(200));
let daemon = Daemon::from_config(&config);
let interrupt_notify = Arc::new(Notify::new());
let running = daemon.execute_async(args.daemon_args, Arc::clone(&interrupt_notify))?;
let handle = tokio::spawn(async move {
let handle1 = tokio::spawn(async move {
let res = running.await;
println!("the process has finished! with {res:?}");
});
tokio::time::sleep(Duration::from_secs(2)).await;
info!(">>>>>>>>>> NYMVISOR: sending interrupt to the daemon");
let (events_sender, mut events_receiver) = mpsc::unbounded();
let mut watcher = AsyncFileWatcher::new_file_changes_watcher(
config.upgrade_plan_filepath(),
events_sender,
)?;
let handle2 = tokio::spawn(async move { watcher.watch().await });
let event = events_receiver.next().await;
println!("watcher event: {event:?}");
interrupt_notify.notify_one();
handle.await;
handle1.await;
handle2.await;
// println!("{:?}", status);
Ok(())
<Result<_, NymvisorError>>::Ok(())
})
}
+6
View File
@@ -22,6 +22,8 @@ pub(crate) const DEFAULT_STARTUP_PERIOD: Duration = Duration::from_secs(120);
pub(crate) const DEFAULT_MAX_STARTUP_FAILURES: usize = 10;
pub(crate) const DEFAULT_SHUTDOWN_GRACE_PERIOD: Duration = Duration::from_secs(10);
pub(crate) const UPGRADE_PLAN_FILENAME: &str = "upgrade-plan.json";
pub(crate) const UPGRADE_INFO_FILENAME: &str = "upgrade-info.json";
pub(crate) const NYMVISOR_DIR: &str = "nymvisor";
pub(crate) const BACKUP_DIR: &str = "backups";
pub(crate) const GENESIS_DIR: &str = "genesis";
@@ -255,6 +257,10 @@ impl Config {
pub fn upgrades_dir(&self) -> PathBuf {
self.upgrade_data_dir().join(UPGRADES_DIR)
}
pub fn upgrade_plan_filepath(&self) -> PathBuf {
self.upgrade_data_dir().join(UPGRADE_PLAN_FILENAME)
}
}
#[derive(Debug, Deserialize, PartialEq, Serialize)]
-4
View File
@@ -142,10 +142,6 @@ impl ExecutingDaemon {
nix::sys::signal::kill(Pid::from_raw(self.child_id), signal)
.map_err(|source| NymvisorError::DaemonSignalFailure { signal, source })
}
// fn foo(&'static mut self) {
// self.interrupted = Some(Box::pin(self._notify.notified()));
// }
}
impl Future for ExecutingDaemon {
+7
View File
@@ -1,6 +1,7 @@
// Copyright 2023 - Nym Technologies SA <contact@nymtech.net>
// SPDX-License-Identifier: Apache-2.0
use async_file_watcher::NotifyError;
use nix::sys::signal::Signal;
use nym_bin_common::build_information::BinaryBuildInformationOwned;
use std::ffi::OsString;
@@ -138,6 +139,12 @@ pub(crate) enum NymvisorError {
#[source]
source: nix::Error,
},
#[error("failed to watch for changes in the upgrade-plan.json: {source}")]
UpgradePlanFileWatchFailure {
#[from]
source: NotifyError,
},
}
impl From<ExitStatus> for NymvisorError {