conductor/src/job.rs

90 lines
1.7 KiB
Rust

use actix::Message;
use rand::Rng;
use std::{
collections::HashMap,
ops::{Deref, DerefMut},
};
use crate::definition::Command;
#[derive(Clone, Message, Debug)]
#[rtype(result = "()")]
pub struct Jobs(Vec<Job>);
impl Jobs {
pub fn new(tasks: Vec<Job>) -> Self {
Self(tasks)
}
pub fn pop_front(&mut self) -> Option<Job> {
if self.0.is_empty() {
None
} else {
Some(self.0.remove(0))
}
}
}
impl Deref for Jobs {
type Target = Vec<Job>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl DerefMut for Jobs {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
impl std::ops::Add for Jobs {
type Output = Jobs;
fn add(mut self, mut rhs: Self) -> Self::Output {
self.0.append(&mut rhs.0);
self
}
}
impl From<Vec<Job>> for Jobs {
fn from(fr: Vec<Job>) -> Self {
Jobs::new(fr)
}
}
#[derive(Clone, Debug, Message)]
#[rtype(result = "()")]
pub struct Job {
pub name: String,
pub command: Command,
pub path: String,
pub env: HashMap<String, String>,
pub retry: bool,
pub keep_alive: bool,
pub retry_delay: u64,
pub color: (u8, u8, u8),
}
impl Default for Job {
fn default() -> Self {
let mut rng = rand::thread_rng();
Job {
name: "Unnamed".to_string(),
command: "".into(),
path: ".".to_string(),
env: HashMap::new(),
retry: true,
keep_alive: true,
retry_delay: 2,
color: (
rng.gen_range(100..255),
rng.gen_range(100..255),
rng.gen_range(100..255),
),
}
}
}