Skip to content

Commit

Permalink
quality: fmt and unused import
Browse files Browse the repository at this point in the history
  • Loading branch information
doubleailes committed Sep 11, 2024
1 parent ee4331d commit 5f9133f
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 27 deletions.
26 changes: 9 additions & 17 deletions crates/spfs-cli/cmd-monitor/src/cmd_monitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,18 @@ use clap::Parser;
#[cfg(feature = "sentry")]
use cli::configure_sentry;
use miette::{Context, IntoDiagnostic, Result};
use spfs::Error;
use spfs_cli_common as cli;
use spfs_cli_common::CommandName;
use tokio::io::AsyncReadExt;
#[cfg(unix)]
use tokio::signal::unix::{signal, SignalKind};
#[cfg(windows)]
use tokio::signal::windows::ctrl_c;
use tokio::time::timeout;

mod signal;
#[cfg(unix)]
use signal::unix_signal_handler::UnixSignalHandler as SignalHandlerImpl;
use signal::SignalHandler;
#[cfg(windows)]
use windows_signal_handler::WindowsSignalHandler as SignalHandlerImpl;

use signal::SignalHandler;



fn main() -> Result<()> {
// because this function exits right away it does not
// properly handle destruction of data, so we put the actual
Expand Down Expand Up @@ -155,15 +147,15 @@ impl CmdMonitor {

pub async fn run_async(&mut self, config: &spfs::Config) -> Result<i32> {
let signal_future = SignalHandlerImpl::build_signal_future();

let repo = spfs::open_repository(&self.runtime_storage).await?;
let storage = spfs::runtime::Storage::new(repo)?;
let runtime = storage.read_runtime(&self.runtime).await?;
tracing::trace!("read runtime from storage repo");

let mut owned = spfs::runtime::OwnedRuntime::upgrade_as_monitor(runtime).await?;
tracing::trace!("upgraded to owned runtime, waiting for empty runtime");

let fut = spfs::monitor::wait_for_empty_runtime(&owned, config);
let res = tokio::select! {
res = fut => {
Expand All @@ -175,13 +167,13 @@ impl CmdMonitor {
_ = signal_future => Err(spfs::Error::String("Signal received, cleaning up runtime early".to_string())),
};
tracing::trace!("runtime empty of processes ");

// need to reload the runtime here to get any changes made to
// the runtime while it was running so we don't blast them the
// next time this process saves the runtime state.
tracing::trace!("reloading runtime data before cleanup");
owned.reload_state_from_storage().await?;

// try to set the running to false to make this
// runtime easier to identify as safe to delete
// if the automatic cleanup fails. Any error
Expand All @@ -190,12 +182,12 @@ impl CmdMonitor {
if let Err(err) = owned.save_state_to_storage().await {
tracing::error!("failed to save runtime: {err:?}");
}

tracing::trace!("tearing down and exiting");
if let Err(err) = spfs::exit_runtime(&owned).await {
tracing::error!("failed to tear down runtime: {err:?}");
}

tracing::trace!(
"{} runtime data",
if owned.is_durable() {
Expand All @@ -216,7 +208,7 @@ impl CmdMonitor {
} else if let Err(err) = owned.delete().await {
tracing::error!("failed to clean up runtime data: {err:?}")
}

res?;
Ok(0)
}
Expand Down
23 changes: 13 additions & 10 deletions crates/spfs-cli/cmd-monitor/src/signal.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use futures::future::Future;
use std::pin::Pin;

use futures::future::Future;
use spfs::Error;

pub trait SignalHandler {
Expand All @@ -8,9 +9,10 @@ pub trait SignalHandler {

#[cfg(unix)]
pub mod unix_signal_handler {
use super::*;
use tokio::signal::unix::{signal, SignalKind};

use super::*;

pub struct UnixSignalHandler;

impl SignalHandler for UnixSignalHandler {
Expand Down Expand Up @@ -38,20 +40,21 @@ pub mod unix_signal_handler {

#[cfg(windows)]
pub mod windows_signal_handler {
use super::*;
use tokio::signal::ctrl_c;

use super::*;

pub struct WindowsSignalHandler;

impl SignalHandler for WindowsSignalHandler {
fn build_signal_future() -> Pin<Box<dyn Future<Output = Result<(), Error>> + Send>> {
Box::pin(async move {
let mut interrupt = ctrl_c()
.map_err(|err| Error::process_spawn_error("ctrl_c()", err, None))?;
let mut quit = ctrl_c()
.map_err(|err| Error::process_spawn_error("ctrl_c()", err, None))?;
let mut terminate = ctrl_c()
.map_err(|err| Error::process_spawn_error("ctrl_c()", err, None))?;
let mut interrupt =
ctrl_c().map_err(|err| Error::process_spawn_error("ctrl_c()", err, None))?;
let mut quit =
ctrl_c().map_err(|err| Error::process_spawn_error("ctrl_c()", err, None))?;
let mut terminate =
ctrl_c().map_err(|err| Error::process_spawn_error("ctrl_c()", err, None))?;

futures::future::select_all(vec![
Box::pin(interrupt),
Expand All @@ -64,4 +67,4 @@ pub mod windows_signal_handler {
})
}
}
}
}

0 comments on commit 5f9133f

Please sign in to comment.