allow only one job of a type to be in progress at once

feat/vaults
Tomáš Mládek 2022-01-30 16:00:29 +01:00
parent bb13f92207
commit fa5626af4f
No known key found for this signature in database
GPG Key ID: ED21612889E75EC5
2 changed files with 38 additions and 25 deletions

View File

@ -7,7 +7,7 @@ use std::{fs, iter};
use crate::addressing::Address;
use crate::database::constants::{
HIER_HAS_ATTR, IS_OF_TYPE_ATTR, TYPE_ADDR, TYPE_BASE_ATTR, TYPE_HAS_ATTR, LABEL_ATTR,
HIER_HAS_ATTR, IS_OF_TYPE_ATTR, LABEL_ATTR, TYPE_ADDR, TYPE_BASE_ATTR, TYPE_HAS_ATTR,
};
use crate::database::entry::{Entry, EntryValue, InvariantEntry};
use crate::database::hierarchies::{
@ -55,27 +55,28 @@ pub async fn rescan_vault(
job_container: Arc<RwLock<JobContainer>>,
initial: bool,
) {
let job_id = job_container
let add_result = job_container
.write()
.unwrap()
.add_job(Job::new("REIMPORT", "Reimporting vault..."))
.unwrap();
.add_job(Job::new("REIMPORT", "Reimporting vault..."));
let job_container_rescan = job_container.clone();
let result = actix_web::web::block(move || {
rescan_vault_inner(db, job_container_rescan, job_id, initial)
})
.await;
if let Ok(job_id) = add_result {
let job_container_rescan = job_container.clone();
let result = actix_web::web::block(move || {
rescan_vault_inner(db, job_container_rescan, job_id, initial)
})
.await;
if result.is_err() {
let err = result.err().unwrap();
error!("Update did not succeed! {:?}", err);
if result.is_err() {
let err = result.err().unwrap();
error!("Update did not succeed! {:?}", err);
job_container
.write()
.unwrap()
.update_state(&job_id, State::Failed)
.unwrap();
job_container
.write()
.unwrap()
.update_state(&job_id, State::Failed)
.unwrap();
}
}
}
struct PragmaSynchronousGuard<'a>(&'a UpEndConnection);
@ -436,9 +437,7 @@ fn insert_file_with_metadata(
let name_entry = Entry {
entity: dir_has_entry_addr,
attribute: ALIAS_KEY.to_string(),
value: EntryValue::String(
filename.as_os_str().to_string_lossy().to_string(),
),
value: EntryValue::String(filename.as_os_str().to_string_lossy().to_string()),
};
connection.insert_entry(name_entry)?;

View File

@ -23,7 +23,7 @@ impl Job {
pub type JobType = String;
#[derive(Serialize, Clone)]
#[derive(Serialize, Clone, PartialEq)]
pub enum State {
InProgress,
Done,
@ -61,11 +61,25 @@ impl Serialize for JobId {
}
}
#[derive(Debug, Clone)]
pub struct JobInProgessError(String);
impl JobContainer {
pub fn add_job(&mut self, job: Job) -> Result<JobId> {
let uuid = Uuid::new_v4();
self.jobs.insert(JobId::from(uuid), job);
Ok(JobId::from(uuid))
pub fn add_job(&mut self, job: Job) -> Result<JobId, JobInProgessError> {
if self
.jobs
.iter()
.any(|(_, j)| j.state == State::InProgress && j.job_type == job.job_type)
{
Err(JobInProgessError(format!(
"Job of type \"{}\" currently in progress.",
job.job_type
)))
} else {
let uuid = Uuid::new_v4();
self.jobs.insert(JobId::from(uuid), job);
Ok(JobId::from(uuid))
}
}
pub fn get_jobs(&self) -> HashMap<JobId, Job> {