-
Notifications
You must be signed in to change notification settings - Fork 355
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
55 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,53 +1,76 @@ | ||
use anyhow::{Context, Result}; | ||
use oci_spec::runtime::Spec; | ||
|
||
use self::default::DefaultExecHandler; | ||
use self::default::DefaultExecutor; | ||
#[cfg(feature = "wasm-wasmer")] | ||
use self::wasmer::WasmerExecHandler; | ||
use self::wasmer::WasmerExecutor; | ||
|
||
pub mod default; | ||
#[cfg(feature = "wasm-wasmer")] | ||
pub mod wasmer; | ||
|
||
static EMPTY: Vec<String> = Vec::new(); | ||
|
||
pub trait ExecHandler { | ||
/// The name of the handler | ||
fn name(&self) -> &str; | ||
pub trait Executor { | ||
/// Executes the workload | ||
fn exec(&self, spec: &Spec) -> Result<()>; | ||
/// Checks if the handler is able to handle the workload | ||
fn can_handle(&self, spec: &Spec) -> Result<bool>; | ||
/// The name of the handler | ||
fn name(&self) -> &str; | ||
} | ||
pub struct Executor { | ||
handlers: Vec<Box<dyn ExecHandler>>, | ||
pub struct CompositeExecutor { | ||
executors: Vec<Box<dyn Executor>>, | ||
} | ||
|
||
impl Executor { | ||
pub fn exec(&self, spec: &Spec) -> Result<()> { | ||
for handler in &self.handlers { | ||
if handler | ||
impl Executor for CompositeExecutor { | ||
fn exec(&self, spec: &Spec) -> Result<()> { | ||
for executor in &self.executors { | ||
if executor | ||
.can_handle(spec) | ||
.with_context(|| format!("handler {} failed on selection", handler.name()))? | ||
.with_context(|| format!("executor {} failed on selection", executor.name()))? | ||
{ | ||
handler | ||
.exec(spec) | ||
.with_context(|| format!("handler {} failed on exec", handler.name()))?; | ||
let result = executor.exec(spec); | ||
if result.is_err() { | ||
let error_msg = if executor.name() == "default" { | ||
"executor default failed on exec. This might have been caused \ | ||
by another handler not being able to match on your request".to_string() | ||
} else { | ||
format!("executor {} failed on exec", executor.name()) | ||
}; | ||
|
||
return result.context(error_msg); | ||
} else { | ||
return Ok(()); | ||
} | ||
} | ||
} | ||
|
||
unreachable!("no suitable execution handler has been registered"); | ||
} | ||
|
||
fn can_handle(&self, spec: &Spec) -> Result<bool> { | ||
Ok(self | ||
.executors | ||
.iter() | ||
.any(|h| h.can_handle(spec).unwrap_or_default())) | ||
} | ||
|
||
fn name(&self) -> &str { | ||
"composite" | ||
} | ||
} | ||
|
||
impl Default for Executor { | ||
impl Default for CompositeExecutor { | ||
fn default() -> Self { | ||
let handlers: Vec<Box<dyn ExecHandler>> = vec![ | ||
let handlers: Vec<Box<dyn Executor>> = vec![ | ||
#[cfg(feature = "wasm-wasmer")] | ||
Box::new(WasmerExecHandler {}), | ||
Box::new(DefaultExecHandler {}), | ||
Box::new(WasmerExecutor {}), | ||
Box::new(DefaultExecutor {}), | ||
]; | ||
|
||
Self { handlers } | ||
Self { | ||
executors: handlers, | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters