Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: remove stderr path #203

Merged
merged 1 commit into from
Aug 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 5 additions & 20 deletions crates/server/src/handlers/worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use actix_web::{
web::{Bytes, Data},
HttpRequest, HttpResponse,
};
use std::{io::Write, sync::RwLock};
use std::sync::RwLock;
use wws_router::WORKERS;
use wws_worker::io::WasmOutput;

Expand Down Expand Up @@ -79,25 +79,10 @@ pub async fn handle_worker(req: HttpRequest, body: Bytes) -> HttpResponse {
None => None,
};

let stderr_file = app_data
.stderr
.as_ref()
.map(|file| file.try_clone().expect("error setting up stderr"));

let (handler_result, handler_success) =
match worker.run(&req, &body_str, store, vars, &stderr_file) {
Ok(output) => (output, true),
Err(error) => {
if let Some(mut stderr_file) = stderr_file {
stderr_file
.write_all(error.to_string().as_bytes())
.expect("Failed to write error to stderr_file");
} else {
eprintln!("{}", error);
}
(WasmOutput::failed(), false)
}
};
let (handler_result, handler_success) = match worker.run(&req, &body_str, store, vars) {
Ok(output) => (output, true),
Err(_) => (WasmOutput::failed(), false),
};

let mut builder = HttpResponse::build(
StatusCode::from_u16(handler_result.status).unwrap_or(StatusCode::OK),
Expand Down
32 changes: 6 additions & 26 deletions crates/server/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,7 @@ use actix_web::{
use handlers::assets::handle_assets;
use handlers::not_found::handle_not_found;
use handlers::worker::handle_worker;
use std::{
fs::{File, OpenOptions},
path::PathBuf,
sync::RwLock,
};
use std::{path::PathBuf, sync::RwLock};
use wws_api_manage::config_manage_api_handlers;
use wws_data_kv::KV;
use wws_panel::config_panel_handlers;
Expand Down Expand Up @@ -54,39 +50,23 @@ pub struct ServeOptions {
pub hostname: String,
pub port: u16,
pub panel: Panel,
pub stderr: Option<PathBuf>,
pub cors_origins: Option<Vec<String>>,
}

#[derive(Default)]
pub struct AppData {
routes: Routes,
root_path: PathBuf,
stderr: Option<File>,
cors_origins: Option<Vec<String>>,
}

impl TryFrom<ServeOptions> for AppData {
type Error = ServeError;

fn try_from(serve_options: ServeOptions) -> Result<Self> {
let stderr = if let Some(stderr) = serve_options.stderr {
Some(
OpenOptions::new()
.append(true)
.open(stderr)
.map_err(|_| ServeError::InitializeServerError)?,
)
} else {
None
};

Ok(AppData {
impl From<ServeOptions> for AppData {
fn from(serve_options: ServeOptions) -> Self {
AppData {
routes: serve_options.base_routes,
root_path: serve_options.root_path.clone(),
stderr,
cors_origins: serve_options.cors_origins.clone(),
})
}
}
}

Expand Down Expand Up @@ -174,7 +154,7 @@ pub async fn serve(serve_options: ServeOptions) -> Result<Server> {
app
})
.bind(format!("{}:{}", hostname, port))
.map_err(|_| errors::ServeError::InitializeServerError)?;
.map_err(|_| ServeError::InitializeServerError)?;

Ok(server.run())
}
19 changes: 2 additions & 17 deletions crates/worker/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use errors::Result;
use features::wasi_nn::WASI_NN_BACKEND_OPENVINO;
use io::{WasmInput, WasmOutput};
use sha256::digest as sha256_digest;
use std::fs::{self, File};
use std::fs;
use std::path::PathBuf;
use std::sync::Arc;
use std::{collections::HashMap, path::Path};
Expand Down Expand Up @@ -96,25 +96,9 @@ impl Worker {
body: &str,
kv: Option<HashMap<String, String>>,
vars: &HashMap<String, String>,
stderr: &Option<File>,
) -> Result<WasmOutput> {
let input = serde_json::to_string(&WasmInput::new(request, body, kv)).unwrap();

// Prepare the stderr file if present
let stderr_file;

if let Some(file) = stderr {
stderr_file = Some(
file.try_clone()
.map_err(|_| errors::WorkerError::ConfigureRuntimeError)?,
);
} else {
stderr_file = None;
}

// Initialize stdio and configure it
let stdio = Stdio::new(&input, stderr_file);

let mut linker = Linker::new(&self.engine);

http_add_to_linker(&mut linker, |s: &mut WorkerState| &mut s.http)
Expand All @@ -132,6 +116,7 @@ impl Worker {
.map_err(|_| errors::WorkerError::ConfigureRuntimeError)?;

// Configure the stdio
let stdio = Stdio::new(&input);
wasi_builder = stdio.configure_wasi_ctx(wasi_builder);

// Mount folders from the configuration
Expand Down
35 changes: 6 additions & 29 deletions crates/worker/src/stdio.rs
Original file line number Diff line number Diff line change
@@ -1,56 +1,33 @@
use std::{fs::File, io::Cursor};
use std::io::Cursor;
use wasi_common::pipe::{ReadPipe, WritePipe};
use wasmtime_wasi::WasiCtxBuilder;

/// A library to configure the stdio of the WASI context.
/// Note that currently, wws relies on stdin and stdout
/// to send and read data from the worker.
///
/// The stderr is configurable just to cover use cases in which
/// wws is used as a library and we want to expose the logs
///
/// @see https://github.com/vmware-labs/wasm-workers-server/issues/125
///
/// The stdin/stdout approach will change in the future with
/// a more performant and appropiate way.
/// a more performant and appropiate approach.
pub struct Stdio {
/// Defines the stdin ReadPipe to send the data to the module
pub stdin: ReadPipe<Cursor<String>>,
/// Defines the stdout to extract the data from the module
pub stdout: WritePipe<Cursor<Vec<u8>>>,
/// Defines the stderr to expose logs from inside the module
pub stderr: Option<WritePipe<File>>,
}

impl Stdio {
/// Initialize the stdio. The stdin will contain the input data.
pub fn new(input: &str, stderr_file: Option<File>) -> Self {
let stderr;

if let Some(file) = stderr_file {
stderr = Some(WritePipe::new(file));
} else {
stderr = None
}

pub fn new(input: &str) -> Self {
Self {
stdin: ReadPipe::from(input),
stdout: WritePipe::new_in_memory(),
stderr,
}
}

pub fn configure_wasi_ctx(&self, builder: WasiCtxBuilder) -> WasiCtxBuilder {
let builder = builder
builder
.stdin(Box::new(self.stdin.clone()))
.stdout(Box::new(self.stdout.clone()));

// Set stderr if it was previously configured. If not, inherit
// it from the environment
if let Some(pipe) = self.stderr.clone() {
builder.stderr(Box::new(pipe))
} else {
builder.inherit_stderr()
}
.stdout(Box::new(self.stdout.clone()))
.inherit_stderr()
}
}
1 change: 0 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,6 @@ async fn main() -> std::io::Result<()> {
hostname: args.hostname.clone(),
port: args.port,
panel: args.enable_panel.into(),
stderr: None,
cors_origins: args.cors,
})
.await
Expand Down