Skip to content

Commit

Permalink
Flesh out GenericRunner backend impl, still need to build up substitu…
Browse files Browse the repository at this point in the history
…tions before processing
  • Loading branch information
BradenEverson committed Sep 5, 2024
1 parent ce4f4cc commit 5884098
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 14 deletions.
3 changes: 2 additions & 1 deletion src/engine/service/runner/backend/backend_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const LEFT_PLACEHOLDER: &str = "~{";
const RIGHT_PLACEHOLDER: &str = "}";

/// Substitutes placeholders in a string with values from a hashmap
fn substitute_placeholders(s: &str, substitutions: &HashMap<String, String>) -> String {
pub(crate) fn substitute_placeholders(s: &str, substitutions: &HashMap<String, String>) -> String {
let mut result = s.to_string();
for (key, value) in substitutions {
let placeholder_key = format!("{}{}{}", LEFT_PLACEHOLDER, key, RIGHT_PLACEHOLDER);
Expand Down Expand Up @@ -43,6 +43,7 @@ pub struct BackendConfig {
impl BackendConfig {
/// Submits a backend based on its config. Likely this method will be removed and the branch for Generic will be moved to GenericBackend's submit method.
/// Instead of this method we should have a to_backend() method or something similar that creates a Box<dyn Backend> based on config.
#[cfg(test)]
pub fn submit(&self, substitutions: &mut HashMap<String, String>) -> Option<Output> {
// Replace default flags only if it isn't already set

Expand Down
56 changes: 43 additions & 13 deletions src/engine/service/runner/backend/generic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,18 @@
//! Note: Generic backend isn't actually constructed currently. Once we have a `Backend` trait, I'd like to add a `to_backend` method to `BackendConfig`
//! that returns something like a `Box<dyn Backend>.` For now since we don't have this yet, I just have the submit method directly in `BackendConfig`
use std::{collections::HashMap, sync::Arc};
use std::{collections::HashMap, process::Command, sync::Arc};

use async_trait::async_trait;
use futures::FutureExt;
use nonempty::NonEmpty;
use tokio::sync::oneshot::Sender;

use crate::engine::Task;
use crate::engine::{service::runner::backend::backend_config::substitute_placeholders, Task};

use super::{
backend_config::{BackendConfig, BackendType},
Backend, ExecutionResult,
Backend, ExecutionResult, Reply,
};

/// A generic backend
Expand Down Expand Up @@ -46,14 +47,23 @@ impl GenericBackend {
}

/// Generates a process result from an incoming task
pub fn process_task(&self, substitutions: &HashMap<String, String>) -> ExecutionResult {
let command = &self.submit;
todo!();
/*ExecutionResult {
status: (),
stdout: (),
stderr: (),
}*/
pub fn process_command(
&self,
substitutions: &HashMap<String, String>,
) -> Option<ExecutionResult> {
let command = substitute_placeholders(&self.submit, substitutions);

let output = Command::new("sh")
.arg("-c")
.arg(command)
.output()
.expect("Failed to run command");

Some(ExecutionResult {
status: output.status.code()? as i64,
stdout: String::from_utf8(output.stdout).ok()?,
stderr: String::from_utf8(output.stderr).ok()?,
})
}

/// Wraps the GenericBackend in an Arc and returns the GenericRunner from it
Expand All @@ -78,8 +88,28 @@ pub struct GenericRunner {
#[async_trait]
impl Backend for GenericRunner {
fn run(&self, task: Task, cb: Sender<super::Reply>) -> futures::future::BoxFuture<'static, ()> {
let mut client = self.client.clone();
let client = self.client.clone();

async move {
let mut results: Option<NonEmpty<ExecutionResult>> = None;
for exec in task.executions() {
let mut substitutions = HashMap::new();
//TODO: Build up substitutions with respect to task/exec values and runtime_attrs
let execution_result = client.process_command(&substitutions).unwrap();

async move { todo!() }.boxed()
results = match results {
Some(mut results) => {
results.push(execution_result);
Some(results)
}
None => Some(NonEmpty::new(execution_result)),
}
}

let _ = cb.send(Reply {
executions: results.expect("at least one execution to be run"),
});
}
.boxed()
}
}

0 comments on commit 5884098

Please sign in to comment.