allow only one job of a type to be in progress at once
parent
bb13f92207
commit
fa5626af4f
|
@ -7,7 +7,7 @@ use std::{fs, iter};
|
||||||
|
|
||||||
use crate::addressing::Address;
|
use crate::addressing::Address;
|
||||||
use crate::database::constants::{
|
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::entry::{Entry, EntryValue, InvariantEntry};
|
||||||
use crate::database::hierarchies::{
|
use crate::database::hierarchies::{
|
||||||
|
@ -55,27 +55,28 @@ pub async fn rescan_vault(
|
||||||
job_container: Arc<RwLock<JobContainer>>,
|
job_container: Arc<RwLock<JobContainer>>,
|
||||||
initial: bool,
|
initial: bool,
|
||||||
) {
|
) {
|
||||||
let job_id = job_container
|
let add_result = job_container
|
||||||
.write()
|
.write()
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.add_job(Job::new("REIMPORT", "Reimporting vault..."))
|
.add_job(Job::new("REIMPORT", "Reimporting vault..."));
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
let job_container_rescan = job_container.clone();
|
if let Ok(job_id) = add_result {
|
||||||
let result = actix_web::web::block(move || {
|
let job_container_rescan = job_container.clone();
|
||||||
rescan_vault_inner(db, job_container_rescan, job_id, initial)
|
let result = actix_web::web::block(move || {
|
||||||
})
|
rescan_vault_inner(db, job_container_rescan, job_id, initial)
|
||||||
.await;
|
})
|
||||||
|
.await;
|
||||||
|
|
||||||
if result.is_err() {
|
if result.is_err() {
|
||||||
let err = result.err().unwrap();
|
let err = result.err().unwrap();
|
||||||
error!("Update did not succeed! {:?}", err);
|
error!("Update did not succeed! {:?}", err);
|
||||||
|
|
||||||
job_container
|
job_container
|
||||||
.write()
|
.write()
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.update_state(&job_id, State::Failed)
|
.update_state(&job_id, State::Failed)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
struct PragmaSynchronousGuard<'a>(&'a UpEndConnection);
|
struct PragmaSynchronousGuard<'a>(&'a UpEndConnection);
|
||||||
|
@ -436,9 +437,7 @@ fn insert_file_with_metadata(
|
||||||
let name_entry = Entry {
|
let name_entry = Entry {
|
||||||
entity: dir_has_entry_addr,
|
entity: dir_has_entry_addr,
|
||||||
attribute: ALIAS_KEY.to_string(),
|
attribute: ALIAS_KEY.to_string(),
|
||||||
value: EntryValue::String(
|
value: EntryValue::String(filename.as_os_str().to_string_lossy().to_string()),
|
||||||
filename.as_os_str().to_string_lossy().to_string(),
|
|
||||||
),
|
|
||||||
};
|
};
|
||||||
connection.insert_entry(name_entry)?;
|
connection.insert_entry(name_entry)?;
|
||||||
|
|
||||||
|
|
|
@ -23,7 +23,7 @@ impl Job {
|
||||||
|
|
||||||
pub type JobType = String;
|
pub type JobType = String;
|
||||||
|
|
||||||
#[derive(Serialize, Clone)]
|
#[derive(Serialize, Clone, PartialEq)]
|
||||||
pub enum State {
|
pub enum State {
|
||||||
InProgress,
|
InProgress,
|
||||||
Done,
|
Done,
|
||||||
|
@ -61,11 +61,25 @@ impl Serialize for JobId {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
|
pub struct JobInProgessError(String);
|
||||||
|
|
||||||
impl JobContainer {
|
impl JobContainer {
|
||||||
pub fn add_job(&mut self, job: Job) -> Result<JobId> {
|
pub fn add_job(&mut self, job: Job) -> Result<JobId, JobInProgessError> {
|
||||||
let uuid = Uuid::new_v4();
|
if self
|
||||||
self.jobs.insert(JobId::from(uuid), job);
|
.jobs
|
||||||
Ok(JobId::from(uuid))
|
.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> {
|
pub fn get_jobs(&self) -> HashMap<JobId, Job> {
|
||||||
|
|
Loading…
Reference in New Issue