Skip to content

Commit

Permalink
testing progress bars
Browse files Browse the repository at this point in the history
  • Loading branch information
viperML committed Jul 9, 2023
1 parent 2994ad3 commit c959a49
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 25 deletions.
43 changes: 43 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ clap = { version = "4.1.8", features = [
] }
reqwest = { version = "0.11.14", default-features = false, features = [
"rustls-tls",
"stream"
] }
schemars = "0.8.12"
serde = { version = "1.0.154", features = [
Expand Down Expand Up @@ -74,3 +75,5 @@ caps = "0.5.5"
uninit = "0.5.1"
once_cell = "1.18.0"
file-lock = "2.1.9"
futures-util = "0.3.28"
indicatif = { version = "0.17.5", features = ["tokio"] }
17 changes: 15 additions & 2 deletions src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use color_eyre::Help;
use daggy::Walker;
use futures::stream::futures_unordered;
use futures::{StreamExt, TryStreamExt};
use indicatif::{MultiProgress, ProgressBar};
use owo_colors::OwoColorize;
use tracing::{debug, instrument, span, trace, Level};

Expand Down Expand Up @@ -69,6 +70,8 @@ impl Args {

build_tasks.insert(&root_node, BuildTask::Waiting);

let bars = MultiProgress::new();

while !build_tasks.iter().all(|(_, task)| match task {
BuildTask::Finished => true,
_ => false,
Expand Down Expand Up @@ -130,10 +133,20 @@ impl Args {
) => true,
_ => false,
};
let bars = bars.clone();
let fut = tokio::spawn(async move {
trace!("Starting build task");
let res = unit.build(rebuild, &_db_conn).await;
(unit, res)
let build_result = match unit {
Unit::PackageUnit(_) => unit.build(rebuild, &_db_conn, None),
Unit::FetchUnit(_) => {
let pb = ProgressBar::hidden();
bars.add(pb);
unit.build(rebuild, &_db_conn, None)
}
}
.await;
// let res = unit.build(rebuild, &_db_conn).await;
(unit, build_result)
});
futs.push(fut);
BuildTask::Building
Expand Down
46 changes: 28 additions & 18 deletions src/build_fetch.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use std::ffi::OsStr;
use std::fs::Permissions;
use std::os::unix::prelude::PermissionsExt;
use std::sync::Mutex;

use async_trait::async_trait;
use bytes::Buf;
use daggy::Walker;
use futures::StreamExt;
use tracing::{debug, trace};
use futures_util::{Stream, StreamExt};
use indicatif::ProgressBar;
use tracing::{debug, trace, warn};

use crate::db::DbConnection;
use crate::schema_eval::{Build, Fetch};
Expand All @@ -14,7 +14,7 @@ use crate::*;
#[async_trait]
impl Build for Fetch {
#[tracing::instrument(skip(conn), ret, err, level = "debug")]
async fn build(&self, rebuild: bool, conn: &Mutex<DbConnection>) -> Result<()> {
async fn build(&self, rebuild: bool, conn: &Mutex<DbConnection>, pb: Option<ProgressBar>) -> Result<()> {
let path = self.result.store_path();
let path = path.as_path();

Expand All @@ -26,25 +26,35 @@ impl Build for Fetch {
}
}

let tempfile = &mut tempfile::NamedTempFile::new()?;
debug!(?tempfile);
crate::build::clean_path(path)?;

let client = reqwest::Client::new();
trace!("Fetching file, please wait");
let response = client.get(&self.url).send().await?;
let content = &mut response.bytes().await?.reader();
std::io::copy(content, tempfile)?;

std::fs::copy(tempfile.path(), &path)?;
let status = response.status();
if !status.is_success() {
bail!(status);
}

let total_length = response.content_length();

if self.executable {
// FIXME
debug!("Setting exec bit");
std::process::Command::new("chmod")
.args([OsStr::new("+x"), path.as_ref()])
.output()?;
let mut out = tokio::fs::File::create(path).await?;
let mut stream = response.bytes_stream();
while let Some(item) = stream.next().await {
let hint = stream.size_hint();
warn!(?hint);
tokio::io::copy(&mut item?.as_ref(), &mut out).await?;
}

let perms = if self.executable {
debug!("Setting as executable exec bit");
Permissions::from_mode(0o555)
} else {
Permissions::from_mode(0o444)
};

out.set_permissions(perms).await?;

conn.lock().unwrap().add(&path)?;
Ok(())
}
Expand Down
8 changes: 4 additions & 4 deletions src/build_package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use std::sync::Mutex;
use async_trait::async_trait;
use color_eyre::eyre::{bail, Context};
use futures::StreamExt;
use indicatif::ProgressBar;
use nix::libc::uid_t;
use nix::mount::{mount, MsFlags};
use nix::sched::CloneFlags;
Expand All @@ -20,13 +21,12 @@ use crate::mem_app::MemApp;
use crate::schema_eval::{Build, Package};
use crate::*;

const STACK_SIZE: usize = 1024 * 1024;
const BUILD_SCRIPT_LOC: &str = "/build-script";

#[async_trait]
impl Build for Package {
#[tracing::instrument(skip(conn), ret, err, level = "debug")]
async fn build(&self, rebuild: bool, conn: &Mutex<DbConnection>) -> Result<()> {
async fn build(&self, rebuild: bool, conn: &Mutex<DbConnection>, pb: Option<ProgressBar>) -> Result<()> {
let path = self.result.store_path();
let path = path.as_path();
let _path_str = path.to_str().unwrap();
Expand Down Expand Up @@ -124,8 +124,8 @@ impl Build for Package {
let child = cmd.spawn()?;

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

let mut procstream = ProcessLineStream::try_from(child)?;
while let Some(item) = procstream.next().await {
Expand Down
3 changes: 2 additions & 1 deletion src/schema_eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use std::sync::Mutex;
use ambassador::{delegatable_trait, Delegate};
use async_trait::async_trait;
use color_eyre::Result;
use indicatif::ProgressBar;
use schemars::{schema_for, JsonSchema};
use serde::{Deserialize, Serialize};
use tracing::info;
Expand Down Expand Up @@ -55,7 +56,7 @@ impl crate::Main for Args {
#[async_trait]
#[delegatable_trait]
pub trait Build {
async fn build(&self, rebuild: bool, conn: &Mutex<DbConnection>) -> Result<()>;
async fn build(&self, rebuild: bool, conn: &Mutex<DbConnection>, pb: Option<ProgressBar>) -> Result<()>;
}

#[derive(Educe, PartialEq, Clone, Serialize, Deserialize, JsonSchema, Hash, Delegate, Eq)]
Expand Down

0 comments on commit c959a49

Please sign in to comment.