Skip to content
This repository has been archived by the owner on Jun 29, 2023. It is now read-only.

Commit

Permalink
feat: with loggers
Browse files Browse the repository at this point in the history
  • Loading branch information
kjuulh committed Mar 14, 2023
1 parent 756a080 commit 79d931e
Show file tree
Hide file tree
Showing 36 changed files with 30,308 additions and 17 deletions.
108 changes: 108 additions & 0 deletions Cargo.lock

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

5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,8 @@ color-eyre = "0.6.2"
serde = { version = "1.0.152", features = ["derive"] }
serde_json = "1.0.93"
tokio = { version = "1.25.0", features = ["full"] }
tracing = { version = "0.1.37", features = ["log"] }
tracing-subscriber = { version = "0.3.16", features = [
"tracing-log",
"tracing",
] }
2 changes: 2 additions & 0 deletions crates/dagger-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
Expand Down
17 changes: 12 additions & 5 deletions crates/dagger-core/src/cli_session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down Expand Up @@ -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
Expand All @@ -82,21 +83,27 @@ 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 {
if let Ok(conn) = serde_json::from_str::<ConnectParams>(&line) {
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();
}
}
});

Expand Down
7 changes: 6 additions & 1 deletion crates/dagger-core/src/config.rs
Original file line number Diff line number Diff line change
@@ -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)
}
}

Expand All @@ -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,
}
}
}
7 changes: 4 additions & 3 deletions crates/dagger-core/src/downloader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)?;
}
Expand Down
2 changes: 2 additions & 0 deletions crates/dagger-core/src/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
1 change: 1 addition & 0 deletions crates/dagger-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
8 changes: 8 additions & 0 deletions crates/dagger-core/src/logger.rs
Original file line number Diff line number Diff line change
@@ -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>;
2 changes: 1 addition & 1 deletion crates/dagger-core/src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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?;
Expand Down
3 changes: 3 additions & 0 deletions crates/dagger-sdk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ eyre = { workspace = true }
tokio = { workspace = true }
serde = { workspace = true }
serde_json = { workspace = true }
tracing.workspace = true
tracing-subscriber.workspace = true

base64 = "0.21.0"
futures = "0.3.27"
Expand All @@ -27,3 +29,4 @@ derive_builder = "0.12.0"
pretty_assertions = "1.3.0"
rand = "0.8.5"
genco = "0.17.3"
tracing-test = "0.2.4"
25 changes: 25 additions & 0 deletions crates/dagger-sdk/examples/logging/app/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
/node_modules
/.pnp
.pnp.js

# testing
/coverage

# production
/build

# misc
.DS_Store
.env.local
.env.development.local
.env.test.local
.env.production.local

npm-debug.log*
yarn-debug.log*
yarn-error.log*

build-*
17 changes: 17 additions & 0 deletions crates/dagger-sdk/examples/logging/app/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# React Build

This is based on the [Getting Started guide for Nodejs](https://docs.dagger.io/sdk/nodejs/783645/get-started#step-5-test-against-multiple-nodejs-versions)

A simple react app is created with `create-react-app` which is built and tested by `build.js` or `build.ts`.

Run:

`npm install`

and then:

`node --loader ts-node/esm ./build.ts`

or

`node ./build.js`
Loading

0 comments on commit 79d931e

Please sign in to comment.