2021-11-13 05:29:29 +00:00
|
|
|
use actix::Message;
|
2022-09-25 18:06:34 +00:00
|
|
|
use rand::Rng;
|
2021-11-13 05:29:29 +00:00
|
|
|
use std::{
|
|
|
|
collections::HashMap,
|
|
|
|
ops::{Deref, DerefMut},
|
|
|
|
};
|
|
|
|
|
2022-09-25 18:06:34 +00:00
|
|
|
use crate::definition::Command;
|
|
|
|
|
|
|
|
#[derive(Clone, Message, Debug)]
|
2021-11-13 05:29:29 +00:00
|
|
|
#[rtype(result = "()")]
|
|
|
|
pub struct Jobs(Vec<Job>);
|
|
|
|
|
|
|
|
impl Jobs {
|
|
|
|
pub fn new(tasks: Vec<Job>) -> Self {
|
|
|
|
Self(tasks)
|
|
|
|
}
|
|
|
|
|
2022-09-25 18:06:34 +00:00
|
|
|
pub fn pop_front(&mut self) -> Option<Job> {
|
|
|
|
if self.0.is_empty() {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
Some(self.0.remove(0))
|
|
|
|
}
|
2021-11-13 05:29:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-25 18:06:34 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-13 06:53:44 +00:00
|
|
|
impl From<Vec<Job>> for Jobs {
|
|
|
|
fn from(fr: Vec<Job>) -> Self {
|
|
|
|
Jobs::new(fr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-13 05:29:29 +00:00
|
|
|
#[derive(Clone, Debug, Message)]
|
|
|
|
#[rtype(result = "()")]
|
|
|
|
pub struct Job {
|
|
|
|
pub name: String,
|
2022-09-25 18:06:34 +00:00
|
|
|
pub command: Command,
|
2021-11-13 05:29:29 +00:00
|
|
|
pub path: String,
|
|
|
|
pub env: HashMap<String, String>,
|
|
|
|
pub retry: bool,
|
|
|
|
pub keep_alive: bool,
|
|
|
|
pub retry_delay: u64,
|
2022-09-25 18:06:34 +00:00
|
|
|
pub color: (u8, u8, u8),
|
2021-11-13 05:29:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for Job {
|
|
|
|
fn default() -> Self {
|
2022-09-25 18:06:34 +00:00
|
|
|
let mut rng = rand::thread_rng();
|
|
|
|
|
2021-11-13 05:29:29 +00:00
|
|
|
Job {
|
|
|
|
name: "Unnamed".to_string(),
|
2022-09-25 18:06:34 +00:00
|
|
|
command: "".into(),
|
2021-11-13 05:29:29 +00:00
|
|
|
path: ".".to_string(),
|
|
|
|
env: HashMap::new(),
|
|
|
|
retry: true,
|
|
|
|
keep_alive: true,
|
|
|
|
retry_delay: 2,
|
2022-09-25 18:06:34 +00:00
|
|
|
color: (
|
|
|
|
rng.gen_range(100..255),
|
|
|
|
rng.gen_range(100..255),
|
|
|
|
rng.gen_range(100..255),
|
|
|
|
),
|
2021-11-13 05:29:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|