databuild/databuild/job.rs

51 lines
1.6 KiB
Rust

use crate::job_run::{JobRunHandle, SubProcessBackend};
use crate::util::DatabuildError;
use crate::{JobConfig, PartitionRef, WantDetail};
use regex::Regex;
use std::collections::HashMap;
#[derive(Debug, Clone)]
pub struct JobConfiguration {
pub label: String,
pub patterns: Vec<String>,
pub entry_point: String,
pub environment: HashMap<String, String>,
}
impl JobConfiguration {
/** Launch job to build the partitions specified by the provided wants. */
pub fn spawn(
&self,
wants: Vec<WantDetail>,
) -> Result<JobRunHandle<SubProcessBackend>, std::io::Error> {
let wanted_refs: Vec<PartitionRef> = wants
.iter()
.flat_map(|want| want.partitions.clone())
.collect();
let args: Vec<String> = wanted_refs.iter().map(|pref| pref.r#ref.clone()).collect();
Ok(JobRunHandle::spawn(self.entry_point.clone(), args))
}
pub fn matches(&self, refs: &PartitionRef) -> bool {
self.patterns.iter().any(|pattern| {
let regex = Regex::new(&pattern).expect(&format!("Invalid regex pattern: {}", pattern));
regex.is_match(&refs.r#ref)
})
}
}
impl From<JobConfig> for JobConfiguration {
fn from(config: JobConfig) -> Self {
Self {
label: config.label,
patterns: config.partition_patterns,
entry_point: config.entrypoint,
environment: config.environment,
}
}
}
pub fn parse_job_configuration(s: &str) -> Result<JobConfiguration, DatabuildError> {
let cfg: JobConfig = serde_json::from_str(s)?;
Ok(cfg.into())
}