use std::collections::BTreeSet;
use std::path::Path;
use aion_store::{DesiredState, NewWorkerDeployment, WorkerArtifactRef};
pub const AUTO_PREFIX: &str = "auto/";
#[must_use]
pub fn auto_name(task_queue: &str) -> String {
format!("{AUTO_PREFIX}{task_queue}")
}
#[must_use]
pub fn is_auto_name(name: &str) -> bool {
name.starts_with(AUTO_PREFIX)
}
#[must_use]
pub fn verb(task_queue: &str, document: &Path, dial: &str, namespace: &str) -> Vec<String> {
vec![
"worker".to_owned(),
"agent".to_owned(),
document.display().to_string(),
"--task-queue".to_owned(),
task_queue.to_owned(),
"--liminal-address".to_owned(),
dial.to_owned(),
"--identity".to_owned(),
auto_name(task_queue),
"--namespace".to_owned(),
namespace.to_owned(),
]
}
#[must_use]
pub fn document_path(artifact: &WorkerArtifactRef) -> Option<&Path> {
let WorkerArtifactRef::Builtin { verb } = artifact;
match (verb.first(), verb.get(1), verb.get(2)) {
(Some(worker), Some(agent), Some(document)) if worker == "worker" && agent == "agent" => {
Some(Path::new(document))
}
_ => None,
}
}
#[must_use]
pub fn staged_workflow_type(artifact: &WorkerArtifactRef) -> Option<String> {
let directory = document_path(artifact)?.parent()?.file_name()?.to_str()?;
let (workflow_type, _digest) = directory.rsplit_once('@')?;
Some(workflow_type.to_owned())
}
#[must_use]
pub fn new_deployment(
task_queue: &str,
document: &Path,
dial: &str,
namespace: &str,
binary: aion_store::DeployedBinaryIdentity,
) -> NewWorkerDeployment {
NewWorkerDeployment {
name: auto_name(task_queue),
artifact: WorkerArtifactRef::Builtin {
verb: verb(task_queue, document, dial, namespace),
},
binary,
namespaces: BTreeSet::from([namespace.to_owned()]),
task_queue: task_queue.to_owned(),
node: None,
desired: DesiredState::Running,
}
}
#[cfg(test)]
#[path = "record_tests.rs"]
mod tests;