Skip to content

Commit 59a3ea2

Browse files
committed
add support to customizable startup delay for processes
1 parent ff6dbbc commit 59a3ea2

File tree

3 files changed

+18
-3
lines changed

3 files changed

+18
-3
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ clap = { version = "4.4.10", features = ["derive"] }
4343
colored = "2.1.0"
4444
ctrlc = "3.4.5"
4545
dotenvy = "0.15.7"
46+
duration-str = "0.13.0"
4647
futures-util = "0.3.30"
4748
http = "1.1.0"
4849
itertools = "0.13.0"

src/config.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ use serde::{Deserialize, Serialize};
44
use std::{
55
collections::HashSet,
66
fs::{self, File},
7-
io::{Read, Write},
7+
io::{Read, Write}, time::Duration,
88
};
9+
use duration_str::deserialize_option_duration;
910

1011
#[cfg(not(windows))]
1112
static EXAMPLE_CONFIG: &str = include_str!("templates/cardamon.unix.toml");
@@ -215,6 +216,9 @@ pub struct Process {
215216
pub redirect: Option<Redirect>,
216217
#[serde(rename = "process")]
217218
pub process_type: ProcessType,
219+
#[serde(deserialize_with = "deserialize_option_duration")]
220+
#[serde(default)]
221+
pub startup_grace: Option<Duration>,
218222
}
219223

220224
#[derive(Debug, Deserialize, PartialEq, Serialize)]

src/lib.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -567,10 +567,18 @@ pub async fn run<'a>(
567567
) -> anyhow::Result<DatasetRows> {
568568
let mut processes_to_observe = exec_plan.external_processes_to_observe.unwrap_or(vec![]); // external procs to observe are cloned here.
569569

570+
// Set as minimum grace period, keeping it to stay backwards compatibile
571+
let mut max_grace_millis = 2000_u64;
570572
// run the application if there is anything to run
571573
if !exec_plan.processes_to_execute.is_empty() {
572574
for proc in exec_plan.processes_to_execute {
573575
print!("> starting process {}", proc.name.green());
576+
if let Some(startup_grace) = proc.startup_grace {
577+
let millis = startup_grace.as_millis() as u64;
578+
if millis > max_grace_millis {
579+
max_grace_millis = millis;
580+
}
581+
}
574582

575583
let process_to_observe = run_process(proc)?;
576584

@@ -581,9 +589,9 @@ pub async fn run<'a>(
581589
}
582590
}
583591

584-
print!("> waiting for application to settle");
592+
print!("> waiting for application to settle: {}ms",max_grace_millis);
585593
std::io::stdout().flush()?;
586-
tokio::time::sleep(tokio::time::Duration::from_millis(2000)).await;
594+
tokio::time::sleep(tokio::time::Duration::from_millis(max_grace_millis)).await;
587595
println!(" {}", "\t✓".green());
588596

589597
let start_time = Utc::now().timestamp_millis();
@@ -810,6 +818,7 @@ pub mod tests {
810818
down: None,
811819
redirect: Some(Redirect::Null),
812820
process_type: ProcessType::BareMetal,
821+
startup_grace: None,
813822
};
814823
let proc_to_observe = run_process(&proc)?;
815824

@@ -843,6 +852,7 @@ pub mod tests {
843852
down: None,
844853
redirect: Some(Redirect::Null),
845854
process_type: ProcessType::BareMetal,
855+
startup_grace: None,
846856
};
847857
let procs_to_observe = run_process(&proc)?;
848858
let stop_handle = metrics_logger::start_logging(vec![procs_to_observe])?;

0 commit comments

Comments
 (0)