Skip to content

Commit

Permalink
remove select! from wws shim
Browse files Browse the repository at this point in the history
Signed-off-by: jiaxiao zhou <[email protected]>
  • Loading branch information
Mossaka committed Sep 1, 2023
1 parent 2a3df26 commit 3e78d6d
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 58 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ test/clean:

.PHONY: fmt
fmt:
$(foreach shim,$(SHIMS),cargo fmt --all --manifest-path=containerd-shim-$(shim)-v1/Cargo.toml;)
$(foreach shim,$(SHIMS),cargo fmt --all --manifest-path=containerd-shim-$(shim)-v1/Cargo.toml -- --check;)
$(foreach shim,$(SHIMS),cargo clippy --all-targets --all-features --workspace --manifest-path=containerd-shim-$(shim)-v1/Cargo.toml -- -D warnings;)
cargo fmt --all -- --check
cargo clippy --all-targets --all-features --workspace -- --deny=warnings
Expand Down
8 changes: 4 additions & 4 deletions containerd-shim-slight-v1/src/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ impl SlightExecutor {
Self { stdio }
}

fn slight_run(&self) -> anyhow::Result<()> {
fn wasm_exec(&self) -> anyhow::Result<()> {
self.stdio
.take()
.redirect()
Expand All @@ -29,11 +29,11 @@ impl SlightExecutor {
let rt = Runtime::new().context("failed to create runtime")?;
let args = RunArgs {
module: wasm_path,
slightfile: PathBuf::from(&mod_path),
slightfile: mod_path,
io_redirects: None,
link_all_capabilities: true,
};
rt.block_on(async { handle_run(args).await })
rt.block_on(handle_run(args))
}
}

Expand All @@ -43,7 +43,7 @@ impl Executor for SlightExecutor {
log::info!("executing linux container");
LinuxContainerExecutor::new(self.stdio.clone()).exec(spec)
} else {
if let Err(err) = self.slight_run() {
if let Err(err) = self.wasm_exec() {
log::error!(" >>> error: {:?}", err);
std::process::exit(137);
}
Expand Down
14 changes: 6 additions & 8 deletions containerd-shim-spin-v1/src/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use spin_manifest::Application;
use spin_redis_engine::RedisTrigger;
use spin_trigger::{loader, RuntimeConfig, TriggerExecutor, TriggerExecutorBuilder};
use spin_trigger_http::HttpTrigger;
use std::{future::Future, path::PathBuf, pin::Pin};
use std::path::PathBuf;

use tokio::runtime::Runtime;
use url::Url;
Expand Down Expand Up @@ -81,20 +81,18 @@ impl SpinExecutor {
let trigger = app.info.trigger.clone();
info!(" >>> building spin trigger {:?}", trigger);

let f: Pin<Box<dyn Future<Output = Result<(), anyhow::Error>> + Send>>;

match trigger {
let f = match trigger {
spin_manifest::ApplicationTrigger::Http(_config) => {
let http_trigger: HttpTrigger =
SpinExecutor::build_spin_trigger(PathBuf::from("/"), app)
.await
.context("failed to build spin trigger")?;
info!(" >>> running spin trigger");
f = http_trigger.run(spin_trigger_http::CliArgs {
http_trigger.run(spin_trigger_http::CliArgs {
address: parse_addr(SPIN_ADDR).unwrap(),
tls_cert: None,
tls_key: None,
});
})
}
spin_manifest::ApplicationTrigger::Redis(_config) => {
let redis_trigger: RedisTrigger =
Expand All @@ -103,10 +101,10 @@ impl SpinExecutor {
.context("failed to build spin trigger")?;

info!(" >>> running spin trigger");
f = redis_trigger.run(spin_trigger::cli::NoArgs);
redis_trigger.run(spin_trigger::cli::NoArgs)
}
_ => todo!("Only Http and Redis triggers are currently supported."),
}
};

info!(" >>> notifying main thread we are about to start");
f.await
Expand Down
83 changes: 38 additions & 45 deletions containerd-shim-wws-v1/src/executor.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
use anyhow::Result;
use anyhow::{Context, Result};
use log::{error, info};
use std::path::PathBuf;
use std::path::Path;
use tokio::runtime::Runtime;

use containerd_shim_wasm::{
libcontainer_instance::LinuxContainerExecutor,
sandbox::Stdio,
};
use containerd_shim_wasm::{libcontainer_instance::LinuxContainerExecutor, sandbox::Stdio};
use libcontainer::workload::{Executor, ExecutorError, ExecutorValidationError};
use oci_spec::runtime::Spec;
use utils::is_linux_executable;
Expand All @@ -27,6 +24,36 @@ impl WwsExecutor {
pub fn new(stdio: Stdio) -> Self {
Self { stdio }
}

fn wasm_exec(&self, _spec: &Spec) -> anyhow::Result<()> {
let stderr = self.stdio.take().stderr;
stderr.redirect().context("redirecting stdio")?;

let path = Path::new("/");

let config = Config::load(path).unwrap_or_else(|err| {
error!("[wws] Error reading .wws.toml file. It will be ignored");
error!("[wws] Error: {err}");
Config::default()
});

// Check if there're missing runtimes
if config.is_missing_any_runtime(path) {
error!("[wws] Required language runtimes are not installed. Some files may not be considered workers");
error!("[wws] You can install the missing runtimes with: wws runtimes install");
}

let routes = Routes::new(path, "", Vec::new(), &config);

let rt = Runtime::new().context("failed to create runtime")?;
rt.block_on(self.wasm_exec_async(path, routes))
}

async fn wasm_exec_async(&self, root: &Path, routes: Routes) -> Result<()> {
let server = serve(root, routes, WWS_ADDR, WWS_PORT, false, None).await?;
info!(" >>> notifying main thread we are about to start");
Ok(server.await?)
}
}

impl Executor for WwsExecutor {
Expand All @@ -35,46 +62,12 @@ impl Executor for WwsExecutor {
log::info!("executing linux container");
LinuxContainerExecutor::new(self.stdio.clone()).exec(spec)
} else {
self.stdio.take().stderr.redirect().map_err(|err| {
error!(" >>> error: {:?}", err);
ExecutorError::Other(format!("failed to redirect stderr: {:?}", err))
})?;
let path = PathBuf::from("/");

let config = match Config::load(&path) {
Ok(c) => c,
Err(err) => {
error!(
"[wws] There was an error reading the .wws.toml file. It will be ignored"
);
error!("[wws] Error: {err}");

Config::default()
}
};

// Check if there're missing runtimes
if config.is_missing_any_runtime(&path) {
error!("[wws] Required language runtimes are not installed. Some files may not be considered workers");
error!("[wws] You can install the missing runtimes with: wws runtimes install");
if let Err(err) = self.wasm_exec(spec) {
log::info!(" >>> server shut down due to error: {err}");
std::process::exit(137);
}

let routes = Routes::new(&path, "", Vec::new(), &config);

let rt = Runtime::new().unwrap();
rt.block_on(async {
let f = serve(&path, routes, WWS_ADDR, WWS_PORT, false, None)
.await
.unwrap();
info!(" >>> notifying main thread we are about to start");
tokio::select! {
_ = f => {
log::info!(" >>> server shut down: exiting");
std::process::exit(0);
},
};
});
std::process::exit(137);
log::info!(" >>> server shut down: exiting");
std::process::exit(0);
}
}

Expand Down

0 comments on commit 3e78d6d

Please sign in to comment.