databuild/databuild/job.rs

28 lines
1 KiB
Rust

use crate::job_run::{spawn_job_run, JobRun, JobRunConfig};
use crate::{PartitionRef, WantDetail};
use regex::Regex;
use std::error::Error;
#[derive(Debug, Clone)]
pub struct JobConfiguration {
pub label: String,
pub pattern: String,
pub entry_point: String,
}
impl JobConfiguration {
/** Launch job to build the partitions specified by the provided wants. */
pub fn spawn(&self, wants: Vec<WantDetail>) -> Result<Box<dyn JobRun>, Box<dyn 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();
let config = JobRunConfig::SubProcess { entry_point: self.entry_point.clone(), args };
spawn_job_run(config)
}
pub fn matches(&self, refs: &PartitionRef) -> bool {
let regex =
Regex::new(&self.pattern).expect(&format!("Invalid regex pattern: {}", self.pattern));
regex.is_match(&refs.r#ref)
}
}