Repository: paritytech/substrate
File: client/storage-monitor/src/lib.rs (Rust)

Code before:

const LOG_TARGET: &str = "storage-monitor";

/// Result type used in this crate.
pub type Result<T> = std::result::Result<T, Error>;

/// Error type used in this crate.
#[derive(Debug, thiserror::Error)]
pub enum Error {
	#[error("IO Error")]
	IOError(#[from] io::Error),
	#[error("Out of storage space: available {0}MiB, required {1}MiB")]
	StorageOutOfSpace(u64, u64),
}

/// Parameters used to create the storage monitor.
#[derive(Default, Debug, Clone, Args)]
pub struct StorageMonitorParams {
	/// Required available space on database storage. If available space for DB storage drops below
	/// the given threshold, node will be gracefully terminated. If `0` is given monitoring will be
	/// disabled.
	#[arg(long = "db-storage-threshold", value_name = "MiB", default_value_t = 1000)]
	pub threshold: u64,

	/// How often available space is polled.
	#[arg(long = "db-storage-polling-period", value_name = "SECONDS", default_value_t = 6, value_parser = clap::value_parser!(u32).range(1..))]
	pub polling_period: u32,
}

/// Storage monitor service: checks the available space for the filesystem for given path.
pub struct StorageMonitorService {
	/// watched path
	path: PathBuf,
	/// number of megabytes that shall be free on the filesystem for watched path
	threshold: u64,
	/// storage space polling period
	polling_period: Duration,
}

impl StorageMonitorService {
	/// Creates new StorageMonitorService for given client config
	pub fn try_spawn(
		parameters: StorageMonitorParams,
		database: DatabaseSource,
		spawner: &impl SpawnEssentialNamed,
	) -> Result<()> {
		Ok(match (parameters.threshold, database.path()) {
			(0, _) => {
				log::info!(
					target: LOG_TARGET,
					"StorageMonitorService: threshold `0` given, storage monitoring disabled",

Review comment (suggestion):
```suggestion
	#[arg(long = "db-storage-polling-period", value_name = "SECONDS", default_value_t = 5, value_parser = clap::value_parser!(u32).range(1..))]
```

Code after:

const LOG_TARGET: &str = "storage-monitor";

/// Result type used in this crate.
pub type Result<T> = std::result::Result<T, Error>;

/// Error type used in this crate.
#[derive(Debug, thiserror::Error)]
pub enum Error {
	#[error("IO Error")]
	IOError(#[from] io::Error),
	#[error("Out of storage space: available {0}MiB, required {1}MiB")]
	StorageOutOfSpace(u64, u64),
}

/// Parameters used to create the storage monitor.
#[derive(Default, Debug, Clone, Args)]
pub struct StorageMonitorParams {
	/// Required available space on database storage. If available space for DB storage drops below
	/// the given threshold, node will be gracefully terminated. If `0` is given monitoring will be
	/// disabled.
	#[arg(long = "db-storage-threshold", value_name = "MiB", default_value_t = 1024)]
	pub threshold: u64,

	/// How often available space is polled.
	#[arg(long = "db-storage-polling-period", value_name = "SECONDS", default_value_t = 5, value_parser = clap::value_parser!(u32).range(1..))]
	pub polling_period: u32,
}

/// Storage monitor service: checks the available space for the filesystem for given path.
pub struct StorageMonitorService {
	/// watched path
	path: PathBuf,
	/// number of megabytes that shall be free on the filesystem for watched path
	threshold: u64,
	/// storage space polling period
	polling_period: Duration,
}

impl StorageMonitorService {
	/// Creates new StorageMonitorService for given client config
	pub fn try_spawn(
		parameters: StorageMonitorParams,
		database: DatabaseSource,
		spawner: &impl SpawnEssentialNamed,
	) -> Result<()> {
		Ok(match (parameters.threshold, database.path()) {
			(0, _) => {
				log::info!(
					target: LOG_TARGET,
					"StorageMonitorService: threshold `0` given, storage monitoring disabled",