2021-02-20 17:36:19 +01:00
|
|
|
use anyhow::{anyhow, Result};
|
|
|
|
use serde::{Serialize, Serializer};
|
|
|
|
use std::collections::HashMap;
|
|
|
|
use uuid::Uuid;
|
|
|
|
|
|
|
|
#[derive(Default, Serialize, Clone)]
|
|
|
|
pub struct Job {
|
2022-02-10 15:20:15 +01:00
|
|
|
pub job_type: Option<JobType>,
|
|
|
|
pub title: String,
|
2022-02-19 16:31:51 +01:00
|
|
|
pub progress: Option<f32>,
|
2022-02-10 15:20:15 +01:00
|
|
|
pub state: State,
|
2021-02-20 17:36:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Job {
|
2022-02-10 10:47:10 +01:00
|
|
|
pub fn new<S, IS>(job_type: IS, title: S) -> Self
|
|
|
|
where
|
|
|
|
S: AsRef<str>,
|
|
|
|
IS: Into<Option<S>>,
|
|
|
|
{
|
2022-02-28 17:00:27 +01:00
|
|
|
Job {
|
2022-02-10 10:47:10 +01:00
|
|
|
job_type: job_type.into().map(|jt| String::from(jt.as_ref())),
|
2021-02-20 17:36:19 +01:00
|
|
|
title: String::from(title.as_ref()),
|
|
|
|
..Default::default()
|
2022-02-28 17:00:27 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Drop for Job {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
self.state = State::Failed;
|
2021-02-20 17:36:19 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub type JobType = String;
|
|
|
|
|
2022-02-28 17:00:27 +01:00
|
|
|
#[derive(Serialize, Clone, Copy, PartialEq)]
|
2021-02-20 17:36:19 +01:00
|
|
|
pub enum State {
|
|
|
|
InProgress,
|
|
|
|
Done,
|
2021-06-20 16:46:45 +02:00
|
|
|
Failed,
|
2021-02-20 17:36:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for State {
|
|
|
|
fn default() -> Self {
|
|
|
|
State::InProgress
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Default)]
|
|
|
|
pub struct JobContainer {
|
|
|
|
jobs: HashMap<JobId, Job>,
|
|
|
|
}
|
|
|
|
|
2021-06-20 16:46:45 +02:00
|
|
|
#[derive(Clone, Hash, PartialEq, Eq, Copy)]
|
2021-02-20 17:36:19 +01:00
|
|
|
pub struct JobId {
|
|
|
|
uuid: Uuid,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<Uuid> for JobId {
|
|
|
|
fn from(uuid: Uuid) -> Self {
|
|
|
|
JobId { uuid }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Serialize for JobId {
|
|
|
|
fn serialize<S>(&self, serializer: S) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>
|
|
|
|
where
|
|
|
|
S: Serializer,
|
|
|
|
{
|
|
|
|
serializer.serialize_str(format!("{}", self.uuid).as_str())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-30 16:00:29 +01:00
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub struct JobInProgessError(String);
|
|
|
|
|
2022-02-28 19:49:42 +01:00
|
|
|
impl std::fmt::Display for JobInProgessError {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
write!(f, "job of type {} is already in progress", self.0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl std::error::Error for JobInProgessError {}
|
|
|
|
|
2021-02-20 17:36:19 +01:00
|
|
|
impl JobContainer {
|
2022-01-30 16:00:29 +01:00
|
|
|
pub fn add_job(&mut self, job: Job) -> Result<JobId, JobInProgessError> {
|
2022-02-10 10:47:10 +01:00
|
|
|
if let Some(job_type) = &job.job_type {
|
|
|
|
if self
|
|
|
|
.jobs
|
|
|
|
.iter()
|
|
|
|
.any(|(_, j)| j.state == State::InProgress && j.job_type == job.job_type)
|
|
|
|
{
|
|
|
|
return Err(JobInProgessError(format!(
|
|
|
|
"Job of type \"{}\" currently in progress.",
|
|
|
|
job_type
|
2022-02-10 15:20:15 +01:00
|
|
|
)));
|
2022-02-10 10:47:10 +01:00
|
|
|
}
|
2022-01-30 16:00:29 +01:00
|
|
|
}
|
2022-02-10 10:47:10 +01:00
|
|
|
|
|
|
|
let uuid = Uuid::new_v4();
|
|
|
|
self.jobs.insert(JobId::from(uuid), job);
|
|
|
|
Ok(JobId::from(uuid))
|
2021-02-20 17:36:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_jobs(&self) -> HashMap<JobId, Job> {
|
|
|
|
self.jobs.clone()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn update_progress(&mut self, id: &JobId, progress: f32) -> Result<()> {
|
|
|
|
if let Some(job) = self.jobs.get_mut(id) {
|
2022-02-19 16:31:51 +01:00
|
|
|
job.progress = Some(progress);
|
2021-02-20 17:36:19 +01:00
|
|
|
if progress >= 100.0 {
|
|
|
|
job.state = State::Done;
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
} else {
|
|
|
|
Err(anyhow!("No such job."))
|
|
|
|
}
|
|
|
|
}
|
2021-06-20 16:46:45 +02:00
|
|
|
|
|
|
|
pub fn update_state(&mut self, id: &JobId, state: State) -> Result<()> {
|
|
|
|
if let Some(job) = self.jobs.get_mut(id) {
|
|
|
|
job.state = state;
|
|
|
|
Ok(())
|
|
|
|
} else {
|
|
|
|
Err(anyhow!("No such job."))
|
|
|
|
}
|
|
|
|
}
|
2021-02-20 17:36:19 +01:00
|
|
|
}
|