Skip to content

Commit

Permalink
feat: adds alphanumeric name generator
Browse files Browse the repository at this point in the history
  • Loading branch information
claymcleod committed Sep 20, 2024
1 parent 106a12b commit 258cb33
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 3 deletions.
1 change: 1 addition & 0 deletions crankshaft-engine/src/service.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! Services for various functionality within the execution engine.

pub mod name;
pub mod runner;

pub use runner::Runner;
36 changes: 36 additions & 0 deletions crankshaft-engine/src/service/name.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
//! Name generation services.

use rand::rngs::ThreadRng;
use rand::Rng as _;

/// A name generator.
pub trait Generator {
/// Generates a new name.
fn generate(&self) -> String;
}

/// An alphanumeric name generator.
pub struct Alphanumeric {
/// The length of the randomized portion of the name.
length: usize,
}

impl Default for Alphanumeric {
fn default() -> Self {
Self { length: 12 }
}
}

impl Generator for Alphanumeric {
fn generate(&self) -> String {
let mut rng = ThreadRng::default();

let random: String = (&mut rng)
.sample_iter(&rand::distributions::Alphanumeric)
.take(self.length) // Generate 12 alphanumeric characters
.map(char::from)
.collect();

format!("job-{}", random)
}
}
1 change: 0 additions & 1 deletion crankshaft-engine/src/service/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ impl Runner {
};

self.tasks.push(Box::pin(fun));

TaskHandle { callback: rx }
}

Expand Down
7 changes: 5 additions & 2 deletions crankshaft-engine/src/service/runner/backend/docker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ use futures::stream::FuturesUnordered;
use futures::FutureExt;
use futures::StreamExt;
use nonempty::NonEmpty;
use rand::Rng;
use tempfile::TempDir;

use crate::service::name;
use crate::service::name::Generator;
use crate::service::runner::backend::TaskResult;
use crate::Result;
use crate::Task;
Expand Down Expand Up @@ -120,10 +121,12 @@ fn run(backend: &Backend, task: Task) -> BoxFuture<'static, TaskResult> {
let mut outputs = Vec::new();

for execution in task.executions() {
// NOTE: a name is required by Docker, so a name is generated
// automatically if a name isn't provided.
let name = task
.name()
.map(|v| v.to_owned())
.unwrap_or_else(|| format!("job-{}", rand::thread_rng().gen_range(0..1000000)));
.unwrap_or_else(|| name::Alphanumeric::default().generate());

// (1) Create the container.
let mut builder = client
Expand Down
2 changes: 2 additions & 0 deletions crankshaft-engine/src/service/runner/backend/tes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ impl crate::Backend for Backend {

/// Translates a [`Task`] to a [TES Task](tes::v1::types::Task) for submission.
fn to_tes_task(task: Task) -> tes::v1::types::Task {
// NOTE: a name is not required by the TES specification, so it is kept as
// empty if no name is provided.
let name = task.name().map(|v| v.to_owned());
let description = task.description().map(|v| v.to_owned());

Expand Down

0 comments on commit 258cb33

Please sign in to comment.