Skip to content

Commit

Permalink
finish progress bars
Browse files Browse the repository at this point in the history
  • Loading branch information
viperML committed Jul 10, 2023
1 parent 68f25d6 commit b6eb342
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 43 deletions.
4 changes: 2 additions & 2 deletions pkgs/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ pkgs.empty0 = miq.package {
script = [[
set -x
pwd
sleep
pwd
ls -la
sleep 10
]],
}

Expand Down
13 changes: 3 additions & 10 deletions src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,15 +138,8 @@ impl Args {
let bars = bars.clone();
let fut = tokio::spawn(async move {
trace!("Starting build task");
let build_result = match unit {
Unit::PackageUnit(_) => unit.build(rebuild, &_db_conn, None),
Unit::FetchUnit(_) => {
let pb = ProgressBar::hidden();
unit.build(rebuild, &_db_conn, Some(bars.add(pb)))
}
}
.await;
// let res = unit.build(rebuild, &_db_conn).await;
let pb = ProgressBar::hidden();
let build_result = unit.build(rebuild, &_db_conn, bars.add(pb)).await;
(unit, build_result)
});
futs.push(fut);
Expand Down Expand Up @@ -189,7 +182,7 @@ impl Args {
let u = format!("{unit:?}");
let msg = format!(
"{} <- {}",
unit.result().store_path().to_string_lossy().bright_blue(),
unit.result().store_path().to_string_lossy().bright_green(),
&u.bright_black()
);
bars.println(msg)?;
Expand Down
17 changes: 10 additions & 7 deletions src/build_fetch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::sync::Mutex;
use async_trait::async_trait;
use futures_util::StreamExt;
use indicatif::{ProgressBar, ProgressStyle};
use tokio::io::AsyncWriteExt;
use tracing::debug;

use crate::db::DbConnection;
Expand All @@ -18,7 +19,7 @@ impl Build for Fetch {
&self,
rebuild: bool,
conn: &Mutex<DbConnection>,
pb: Option<ProgressBar>,
pb: ProgressBar,
) -> Result<()> {
let path = self.result.store_path();
let path = path.as_path();
Expand All @@ -41,25 +42,27 @@ impl Build for Fetch {
bail!(status);
}

let pb = pb.wrap_err("Didn't receive a progress bar!")?;

if let Some(total_length) = response.content_length() {
pb.set_length(total_length);
pb.set_message(self.name.clone());
pb.set_style(ProgressStyle::with_template(
"{msg:.blue}>> {percent}% {wide_bar}",
)?);
pb.set_message(self.name.clone());
};

let out_file = tokio::fs::File::create(path).await?;

let mut out_writer = pb.wrap_async_write(out_file);
let pb_writer = pb.wrap_async_write(out_file);
let mut buf_writer = tokio::io::BufWriter::new(pb_writer);
let mut stream = response.bytes_stream();

while let Some(item) = stream.next().await {
let item = item?;
tokio::io::copy(&mut item.as_ref(), &mut out_writer).await?;
tokio::io::copy(&mut item.as_ref(), &mut buf_writer).await?;
}

buf_writer.flush().await?;

let perm = if self.executable {
debug!("Setting as executable exec bit");
Permissions::from_mode(0o555)
Expand All @@ -69,9 +72,9 @@ impl Build for Fetch {

tokio::fs::set_permissions(path, perm).await?;

pb.finish_and_clear();

conn.lock().unwrap().add(&path)?;
pb.finish_and_clear();
Ok(())
}
}
48 changes: 25 additions & 23 deletions src/build_package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,18 @@ use std::os::fd::AsRawFd;
use std::path::Path;
use std::process::Stdio;
use std::sync::Mutex;
use std::time::Duration;

use async_trait::async_trait;
use color_eyre::eyre::{bail, Context};
use futures::StreamExt;
use indicatif::ProgressBar;
use indicatif::{ProgressBar, ProgressStyle};
use nix::libc::uid_t;
use nix::mount::{mount, MsFlags};
use nix::sched::CloneFlags;
use nix::unistd::{Gid, Pid, Uid};
use tokio_process_stream::ProcessLineStream;
use tokio::io::copy;
use tokio_process_stream::{Item, ProcessLineStream};
use tracing::{debug, span, trace, Level};

use crate::db::DbConnection;
Expand All @@ -30,7 +32,7 @@ impl Build for Package {
&self,
rebuild: bool,
conn: &Mutex<DbConnection>,
_pb: Option<ProgressBar>,
pb: ProgressBar,
) -> Result<()> {
let path = self.result.store_path();
let path = path.as_path();
Expand All @@ -44,6 +46,10 @@ impl Build for Package {
}
}

pb.set_message(self.name.clone());
pb.set_style(ProgressStyle::with_template("{msg:.blue}>> {spinner}")?);
pb.enable_steady_tick(Duration::from_millis(500));

crate::build::clean_path(&path)?;
let _build_dir = tempfile::tempdir()?;
let build_path = _build_dir.path().to_owned();
Expand All @@ -58,7 +64,6 @@ impl Build for Package {
let mut cmd = tokio::process::Command::new("/bin/bash");
cmd.args(["--norc", "--noprofile"]);
cmd.arg(BUILD_SCRIPT_LOC);
// cmd.arg("-i");

cmd.env_clear();
cmd.envs([
Expand Down Expand Up @@ -128,34 +133,30 @@ impl Build for Package {
let child = cmd.spawn()?;

let log_file_path = format!("/miq/log/{}.log", self.result.deref());
let mut log_file = std::fs::File::create(&log_file_path)
.wrap_err(format!("Creating logfile at {}", &log_file_path))?;
let log_file = tokio::fs::File::create(&log_file_path).await?;
let log_writer = tokio::io::BufWriter::new(log_file);

let mut pb_writer = pb.wrap_async_write(log_writer);

let mut procstream = ProcessLineStream::try_from(child)?;
while let Some(item) = procstream.next().await {
use owo_colors::OwoColorize;
match item {
tokio_process_stream::Item::Stdout(line) => {
let msg = format!("{}>>{}", self.name.blue(), line.bright_black());
println!("{}", msg);
log_file.write_all(line.as_bytes())?;
log_file.write_all(b"\n")?;
}
tokio_process_stream::Item::Stderr(line) => {
let msg = format!("{}>>{}", self.name.blue(), line.bright_black());
println!("{}", msg);
log_file.write_all(line.as_bytes())?;
log_file.write_all(b"\n")?;
}
tokio_process_stream::Item::Done(Ok(exit)) => {
pb.tick();
let msg = match item {
Item::Stdout(line) | Item::Stderr(line) => line,
Item::Done(Ok(exit)) => {
if exit.success() {
debug!("Build OK");
format!("miq: exit ok")
} else {
bail!(eyre!("Exit not successful").wrap_err(exit));
}
}
tokio_process_stream::Item::Done(Err(exit)) => bail!(exit),
}
Item::Done(Err(exit)) => bail!(exit),
};
let pretty = format!("{}>>{}", self.name.blue(), msg.bright_black());
pb.println(&pretty);
copy(&mut msg.as_bytes(), &mut pb_writer).await?;
copy(&mut "\n".as_bytes(), &mut pb_writer).await?;
}

match path.try_exists().wrap_err("Failed to produce an output") {
Expand All @@ -165,6 +166,7 @@ impl Build for Package {
}

conn.lock().unwrap().add(&path)?;
pb.finish_and_clear();
Ok(())
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/schema_eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ pub trait Build {
&self,
rebuild: bool,
conn: &Mutex<DbConnection>,
pb: Option<ProgressBar>,
pb: ProgressBar,
) -> Result<()>;
}

Expand Down

0 comments on commit b6eb342

Please sign in to comment.