|
|
@ -1,11 +1,8 @@ |
|
|
|
use crate::{ |
|
|
|
use crate::{ |
|
|
|
combined_result::CombinedResult, |
|
|
|
combined_result::CombinedResult, |
|
|
|
executor::{ExecResult, Waiter}, |
|
|
|
executor::{ExecResult, Waiter}, |
|
|
|
models::{ |
|
|
|
models::{Agent, AssignedJob, AssignedJobById, Job, JobType, RawJob}, |
|
|
|
Agent, AssignedJob, AssignedJobById, BriefJob, FullJob, FullPayload, JobType, RawJob, |
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
proc_output::ProcOutput, |
|
|
|
proc_output::ProcOutput, |
|
|
|
ufs, |
|
|
|
|
|
|
|
}; |
|
|
|
}; |
|
|
|
use std::collections::HashMap; |
|
|
|
use std::collections::HashMap; |
|
|
|
use std::process::exit; |
|
|
|
use std::process::exit; |
|
|
@ -17,7 +14,7 @@ pub struct AnonymousJobBatch { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
impl AnonymousJobBatch { |
|
|
|
impl AnonymousJobBatch { |
|
|
|
pub fn from_meta_with_id(jobs: impl IntoIterator<Item = (BriefJob, AssignedJobById)>) -> Self { |
|
|
|
pub fn from_meta_with_id(jobs: impl IntoIterator<Item = (Job, AssignedJobById)>) -> Self { |
|
|
|
let mut waiter = Waiter::new(); |
|
|
|
let mut waiter = Waiter::new(); |
|
|
|
for (job, ids) in jobs { |
|
|
|
for (job, ids) in jobs { |
|
|
|
waiter.push(run_assigned_job(job, ids)); |
|
|
|
waiter.push(run_assigned_job(job, ids)); |
|
|
@ -28,7 +25,7 @@ impl AnonymousJobBatch { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
pub fn from_meta(jobs: impl IntoIterator<Item = BriefJob>) -> Self { |
|
|
|
pub fn from_meta(jobs: impl IntoIterator<Item = Job>) -> Self { |
|
|
|
let jobs_ids: Vec<_> = jobs |
|
|
|
let jobs_ids: Vec<_> = jobs |
|
|
|
.into_iter() |
|
|
|
.into_iter() |
|
|
|
.map(|job| { |
|
|
|
.map(|job| { |
|
|
@ -47,6 +44,7 @@ impl AnonymousJobBatch { |
|
|
|
|
|
|
|
|
|
|
|
/// Spawn jobs
|
|
|
|
/// Spawn jobs
|
|
|
|
pub async fn spawn(mut self) -> Self { |
|
|
|
pub async fn spawn(mut self) -> Self { |
|
|
|
|
|
|
|
debug!("spawning jobs"); |
|
|
|
self.waiter = self.waiter.spawn().await; |
|
|
|
self.waiter = self.waiter.spawn().await; |
|
|
|
self.is_running = true; |
|
|
|
self.is_running = true; |
|
|
|
self |
|
|
|
self |
|
|
@ -81,11 +79,7 @@ impl NamedJobBatch { |
|
|
|
let jobs: Vec<_> = named_jobs |
|
|
|
let jobs: Vec<_> = named_jobs |
|
|
|
.into_iter() |
|
|
|
.into_iter() |
|
|
|
.filter_map(|(alias, cmd)| { |
|
|
|
.filter_map(|(alias, cmd)| { |
|
|
|
match RawJob::brief_job_builder() |
|
|
|
match RawJob::builder().with_shell(cmd).with_alias(alias).build() { |
|
|
|
.with_shell(cmd) |
|
|
|
|
|
|
|
.with_alias(alias) |
|
|
|
|
|
|
|
.build() |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
Ok(jpm) => Some(jpm), |
|
|
|
Ok(jpm) => Some(jpm), |
|
|
|
Err(e) => { |
|
|
|
Err(e) => { |
|
|
|
result.push_err(e); |
|
|
|
result.push_err(e); |
|
|
@ -98,7 +92,7 @@ impl NamedJobBatch { |
|
|
|
result |
|
|
|
result |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
pub fn from_meta(named_jobs: Vec<BriefJob>) -> Self { |
|
|
|
pub fn from_meta(named_jobs: Vec<Job>) -> Self { |
|
|
|
let (job_names, jobs): (Vec<_>, Vec<_>) = named_jobs |
|
|
|
let (job_names, jobs): (Vec<_>, Vec<_>) = named_jobs |
|
|
|
.into_iter() |
|
|
|
.into_iter() |
|
|
|
.map(|job| (job.job.alias.clone().unwrap(), job)) |
|
|
|
.map(|job| (job.job.alias.clone().unwrap(), job)) |
|
|
@ -134,14 +128,14 @@ impl NamedJobBatch<true> { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
pub async fn run_assigned_job(job: BriefJob, ids: AssignedJobById) -> ExecResult { |
|
|
|
pub async fn run_assigned_job(job: Job, ids: AssignedJobById) -> ExecResult { |
|
|
|
let BriefJob { job, payload_meta } = job; |
|
|
|
let Job { job, payload } = job; |
|
|
|
let mut result = AssignedJob::from((&job, ids)); |
|
|
|
let mut result = AssignedJob::from((&job, ids)); |
|
|
|
match job.exec_type { |
|
|
|
match job.exec_type { |
|
|
|
JobType::Shell => { |
|
|
|
JobType::Shell => { |
|
|
|
let (argv, _prepared_payload) = { |
|
|
|
let (argv, _prepared_payload) = { |
|
|
|
if let Some(meta) = payload_meta { |
|
|
|
if let Some(payload) = payload { |
|
|
|
let (prep_exec, prep_exec_path) = ufs::prepare_executable(meta.name)?; |
|
|
|
let (prep_exec, prep_exec_path) = payload.prepare_executable()?; |
|
|
|
let argv_with_exec = job.argv.replace("{}", &prep_exec_path); |
|
|
|
let argv_with_exec = job.argv.replace("{}", &prep_exec_path); |
|
|
|
(argv_with_exec, Some(prep_exec)) |
|
|
|
(argv_with_exec, Some(prep_exec)) |
|
|
|
} else { |
|
|
|
} else { |
|
|
@ -181,38 +175,6 @@ pub async fn run_assigned_job(job: BriefJob, ids: AssignedJobById) -> ExecResult |
|
|
|
Ok(result) |
|
|
|
Ok(result) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
pub fn split_payload(job: FullJob) -> Result<BriefJob, ufs::Error> { |
|
|
|
|
|
|
|
let FullJob { job, payload } = job; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if let Some(payload) = &payload { |
|
|
|
|
|
|
|
if ufs::exists_in_index(&payload.meta.name) { |
|
|
|
|
|
|
|
ufs::edit(&payload.meta.name, &payload.data)?; |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
ufs::put(&payload.meta.name, &payload.data)?; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Ok(BriefJob { |
|
|
|
|
|
|
|
job, |
|
|
|
|
|
|
|
payload_meta: payload.map(|p| p.meta), |
|
|
|
|
|
|
|
}) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub fn join_payload(job: BriefJob) -> Result<FullJob, ufs::Error> { |
|
|
|
|
|
|
|
let BriefJob { job, payload_meta } = job; |
|
|
|
|
|
|
|
let payload = match payload_meta { |
|
|
|
|
|
|
|
Some(meta) => { |
|
|
|
|
|
|
|
let payload_data = ufs::read(&meta.name)?; |
|
|
|
|
|
|
|
Some(FullPayload { |
|
|
|
|
|
|
|
meta, |
|
|
|
|
|
|
|
data: payload_data, |
|
|
|
|
|
|
|
}) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
None => None, |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
Ok(FullJob { job, payload }) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#[cfg(test)] |
|
|
|
#[cfg(test)] |
|
|
|
mod tests { |
|
|
|
mod tests { |
|
|
|
use crate::{ |
|
|
|
use crate::{ |
|
|
@ -265,7 +227,7 @@ mod tests { |
|
|
|
#[case] payload: Option<&[u8]>, |
|
|
|
#[case] payload: Option<&[u8]>, |
|
|
|
#[case] expected_result: &str, |
|
|
|
#[case] expected_result: &str, |
|
|
|
) -> TestResult { |
|
|
|
) -> TestResult { |
|
|
|
let mut job = RawJob::brief_job_builder().with_shell(cmd); |
|
|
|
let mut job = RawJob::builder().with_shell(cmd); |
|
|
|
if let Some(p) = payload { |
|
|
|
if let Some(p) = payload { |
|
|
|
job = job.with_raw_payload(p); |
|
|
|
job = job.with_raw_payload(p); |
|
|
|
} |
|
|
|
} |
|
|
@ -348,7 +310,7 @@ mod tests { |
|
|
|
#[case] payload: Option<&[u8]>, |
|
|
|
#[case] payload: Option<&[u8]>, |
|
|
|
#[case] err_str: &str, |
|
|
|
#[case] err_str: &str, |
|
|
|
) -> TestResult { |
|
|
|
) -> TestResult { |
|
|
|
let mut job = RawJob::brief_job_builder().with_shell(cmd); |
|
|
|
let mut job = RawJob::builder().with_shell(cmd); |
|
|
|
if let Some(p) = payload { |
|
|
|
if let Some(p) = payload { |
|
|
|
job = job.with_raw_payload(p); |
|
|
|
job = job.with_raw_payload(p); |
|
|
|
} |
|
|
|
} |
|
|
@ -361,12 +323,12 @@ mod tests { |
|
|
|
#[tokio::test] |
|
|
|
#[tokio::test] |
|
|
|
async fn test_different_job_types() -> TestResult { |
|
|
|
async fn test_different_job_types() -> TestResult { |
|
|
|
let mut jobs = NamedJobBatch::from_meta(vec![ |
|
|
|
let mut jobs = NamedJobBatch::from_meta(vec![ |
|
|
|
RawJob::brief_job_builder() |
|
|
|
RawJob::builder() |
|
|
|
.with_shell("sleep 3") |
|
|
|
.with_shell("sleep 3") |
|
|
|
.with_alias("sleeper") |
|
|
|
.with_alias("sleeper") |
|
|
|
.build() |
|
|
|
.build() |
|
|
|
.unwrap(), |
|
|
|
.unwrap(), |
|
|
|
RawJob::brief_job_builder() |
|
|
|
RawJob::builder() |
|
|
|
.with_type(JobType::Init) |
|
|
|
.with_type(JobType::Init) |
|
|
|
.with_alias("gatherer") |
|
|
|
.with_alias("gatherer") |
|
|
|
.build() |
|
|
|
.build() |
|
|
|