add timestamps for every commancd run

Signed-off-by: Lars Wirzenius <liw@liw.fi>
This commit is contained in:
Lars Wirzenius
2024-03-31 17:03:40 +03:00
parent 89f0b970e3
commit dd7496fa70
3 changed files with 55 additions and 3 deletions
+7 -1
View File
@@ -1,4 +1,4 @@
use std::path::Path;
use std::{path::Path, time::SystemTime};
use radicle_ci_broker::msg::{Oid, RepoId};
@@ -21,12 +21,16 @@ fn main() {
run_log.runspec(RunSpec {
shell: "echo hello, world".into(),
});
let started = SystemTime::now();
let ended = SystemTime::now();
run_log.runcmd(
&["git", "pull"],
Path::new("/tmp"),
0,
"This is stdout".as_bytes(),
"Error messages go here".as_bytes(),
started,
ended,
);
run_log.runcmd(
&["cargo", "check", "--all-targets", "--workspace"],
@@ -34,6 +38,8 @@ fn main() {
0,
"This is stdout".as_bytes(),
"Error messages go here".as_bytes(),
started,
ended,
);
run_log.write().expect("write html log");
+5
View File
@@ -1,6 +1,7 @@
use std::{
path::{Path, PathBuf},
process::Command,
time::SystemTime,
};
use radicle_ci_broker::msg::{Oid, RepoId};
@@ -196,12 +197,14 @@ impl Run {
return Err(RunError::EmptyArgv);
}
let started = SystemTime::now();
let argv0 = argv[0];
let output = Command::new(argv0)
.args(&argv[1..])
.current_dir(cwd)
.output()
.map_err(|e| RunError::Command(argv.iter().map(|s| s.to_string()).collect(), e))?;
let ended = SystemTime::now();
let exit = output.status.code().unwrap_or(NO_EXIT);
self.run_log.runcmd(
@@ -211,6 +214,8 @@ impl Run {
exit,
&output.stdout,
&output.stderr,
started,
ended,
);
Ok(exit)
}
+43 -2
View File
@@ -1,6 +1,10 @@
use std::path::{Path, PathBuf};
use std::{
path::{Path, PathBuf},
time::{Duration, SystemTime},
};
use html_page::{Document, Element, Tag};
use time::{macros::format_description, OffsetDateTime};
use radicle_ci_broker::msg::{Oid, RepoId};
@@ -65,8 +69,22 @@ impl RunLog {
self.runspec_error = Some(format!("{}", e));
}
pub fn runcmd(&mut self, argv: &[&str], cwd: &Path, exit: i32, stdout: &[u8], stderr: &[u8]) {
// FIXME: refactor this to maybe use a builder pattern?
#[allow(clippy::too_many_arguments)]
pub fn runcmd(
&mut self,
argv: &[&str],
cwd: &Path,
exit: i32,
stdout: &[u8],
stderr: &[u8],
started: SystemTime,
ended: SystemTime,
) {
self.commands.push(Command {
started,
ended,
duration: ended.duration_since(started).expect("compute duration"),
argv: argv.iter().map(|a| a.to_string()).collect(),
cwd: cwd.into(),
exit,
@@ -182,6 +200,9 @@ impl RunLog {
#[derive(Debug)]
struct Command {
started: SystemTime,
ended: SystemTime,
duration: Duration,
argv: Vec<String>,
cwd: PathBuf,
exit: i32,
@@ -203,6 +224,21 @@ impl Command {
),
);
body.push_child(
&Element::new(Tag::Ul)
.with_child(
Element::new(Tag::Li)
.with_text(&format!("Started: {}", timestamp(&self.started))),
)
.with_child(
Element::new(Tag::Li).with_text(&format!("Ended: {}", timestamp(&self.ended))),
)
.with_child(
Element::new(Tag::Li)
.with_text(&format!("Duration: {} s", self.duration.as_secs())),
),
);
body.push_child(&Element::new(Tag::P).with_text("Command arguments:"));
let mut ul = Element::new(Tag::Ul);
for arg in self.argv.iter() {
@@ -236,6 +272,11 @@ impl Command {
}
}
fn timestamp(when: &SystemTime) -> String {
let fmt = format_description!("[year]-[month]-[day] [hour]:[minute]:[second]Z");
OffsetDateTime::from(*when).format(fmt).ok().unwrap()
}
#[derive(Debug, Default)]
struct ToC {
counter: usize,