feat: with loggers

This commit is contained in:
2023-03-14 23:09:52 +01:00
parent 756a080533
commit 01c1232b50
36 changed files with 30308 additions and 17 deletions

View File

@@ -14,6 +14,8 @@ eyre = { workspace = true }
serde = { workspace = true }
serde_json = { workspace = true }
tokio = { workspace = true }
tracing = { workspace = true }
tracing-subscriber = { workspace = true }
dirs = "4.0.0"
flate2 = { version = "1.0.25", features = ["zlib"] }

View File

@@ -35,7 +35,7 @@ impl InnerCliSession {
cli_path: &PathBuf,
) -> eyre::Result<(ConnectParams, tokio::process::Child)> {
let proc = self.start(config, cli_path)?;
let params = self.get_conn(proc).await?;
let params = self.get_conn(proc, config).await?;
Ok(params)
}
@@ -69,6 +69,7 @@ impl InnerCliSession {
async fn get_conn(
&self,
mut proc: tokio::process::Child,
config: &Config,
) -> eyre::Result<(ConnectParams, tokio::process::Child)> {
let stdout = proc
.stdout
@@ -82,6 +83,7 @@ impl InnerCliSession {
let (sender, mut receiver) = tokio::sync::mpsc::channel(1);
let logger = config.logger.as_ref().map(|p| p.clone());
tokio::spawn(async move {
let mut stdout_bufr = tokio::io::BufReader::new(stdout).lines();
while let Ok(Some(line)) = stdout_bufr.next_line().await {
@@ -89,14 +91,19 @@ impl InnerCliSession {
sender.send(conn).await.unwrap();
}
println!("dagger: {}", line);
if let Some(logger) = &logger {
logger.stdout(&line).unwrap();
}
}
});
let logger = config.logger.as_ref().map(|p| p.clone());
tokio::spawn(async move {
let mut stdout_bufr = tokio::io::BufReader::new(stderr).lines();
while let Ok(Some(line)) = stdout_bufr.next_line().await {
println!("dagger: {}", line);
let mut stderr_bufr = tokio::io::BufReader::new(stderr).lines();
while let Ok(Some(line)) = stderr_bufr.next_line().await {
if let Some(logger) = &logger {
logger.stdout(&line).unwrap();
}
}
});

View File

@@ -1,15 +1,18 @@
use std::path::PathBuf;
use crate::logger::DynLogger;
pub struct Config {
pub workdir_path: Option<PathBuf>,
pub config_path: Option<PathBuf>,
pub timeout_ms: u64,
pub execute_timeout_ms: Option<u64>,
pub logger: Option<DynLogger>,
}
impl Default for Config {
fn default() -> Self {
Self::new(None, None, None, None)
Self::new(None, None, None, None, None)
}
}
@@ -19,12 +22,14 @@ impl Config {
config_path: Option<PathBuf>,
timeout_ms: Option<u64>,
execute_timeout_ms: Option<u64>,
logger: Option<DynLogger>,
) -> Self {
Self {
workdir_path,
config_path,
timeout_ms: timeout_ms.unwrap_or(10 * 1000),
execute_timeout_ms,
logger,
}
}
}

View File

@@ -139,9 +139,10 @@ impl Downloader {
if let Ok(entry) = file {
let path = entry.path();
if path != cli_bin_path {
println!(
"deleting client: path: {:?} vs cli_bin_path: {:?}",
path, cli_bin_path
tracing::debug!(
path = path.display().to_string(),
cli_bin_path = cli_bin_path.display().to_string(),
"deleting existing dagger-engine"
);
std::fs::remove_file(path)?;
}

View File

@@ -24,6 +24,8 @@ impl Engine {
&self,
cfg: &Config,
) -> eyre::Result<(ConnectParams, tokio::process::Child)> {
tracing::info!("starting dagger-engine");
// TODO: Add from existing session as well
self.from_cli(cfg).await
}

View File

@@ -8,6 +8,7 @@ pub mod connect_params;
pub mod downloader;
pub mod engine;
pub mod introspection;
pub mod logger;
pub mod schema;
pub mod session;

View File

@@ -0,0 +1,8 @@
use std::sync::Arc;
pub trait Logger {
fn stdout(&self, output: &str) -> eyre::Result<()>;
fn stderr(&self, output: &str) -> eyre::Result<()>;
}
pub type DynLogger = Arc<dyn Logger + Send + Sync>;

View File

@@ -2,7 +2,7 @@ use crate::introspection::IntrospectionResponse;
use crate::{config::Config, engine::Engine, session::Session};
pub async fn get_schema() -> eyre::Result<IntrospectionResponse> {
let cfg = Config::new(None, None, None, None);
let cfg = Config::default();
//TODO: Implement context for proc
let (conn, _proc) = Engine::new().start(&cfg).await?;