Skip to content

Commit

Permalink
vicky: don't accept new conflicting locks
Browse files Browse the repository at this point in the history
  • Loading branch information
Kek5chen committed Apr 24, 2024
1 parent 8589f4d commit 80fbe61
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 0 deletions.
69 changes: 69 additions & 0 deletions vicky/src/bin/vicky/tasks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,16 @@ pub async fn tasks_finish(
Ok(Json(task))
}

// TODO: Move into Task Builder
fn check_lock_conflict(task: &Task) -> bool {
task.locks.iter().enumerate().any(|(i, lock)| {
task.locks
.iter()
.enumerate()
.any(|(j, lock2)| i < j && lock.is_conflicting(lock2))
})
}

#[post("/", data = "<task>")]
pub async fn tasks_add(
task: Json<RoTaskNew>,
Expand All @@ -248,6 +258,10 @@ pub async fn tasks_add(
features: task.features.clone(),
};

if check_lock_conflict(&task_manifest) {
return Err(AppError::HttpError(Status::Conflict));
}

etcd.put_task(&task_manifest).await?;
global_events.send(GlobalEvent::TaskAdd)?;

Expand All @@ -258,3 +272,58 @@ pub async fn tasks_add(

Ok(Json(ro_task))
}

mod tests {
use crate::tasks::check_lock_conflict;
use uuid::Uuid;
use vickylib::documents::{FlakeRef, Lock, Task, TaskStatus};

#[test]
fn add_new_conflicting_task() {
let task = Task {
id: Uuid::new_v4(),
display_name: String::from("Test 1"),
status: TaskStatus::NEW,
locks: vec![
Lock::READ {
name: String::from("mauz"),
},
Lock::WRITE {
name: String::from("mauz"),
},
],
flake_ref: FlakeRef {
flake: String::from(""),
args: vec![],
},
features: vec![],
};
assert!(check_lock_conflict(&task))
}

#[test]
fn add_new_not_conflicting_task() {
let task = Task {
id: Uuid::new_v4(),
display_name: String::from("Test 1"),
status: TaskStatus::NEW,
locks: vec![
Lock::READ {
name: String::from("mauz"),
},
Lock::READ {
name: String::from("mauz"),
},
Lock::WRITE {
name: String::from("delete_everything"),
},
],
flake_ref: FlakeRef {
flake: String::from(""),
args: vec![],
},
features: vec![],
};
assert!(!check_lock_conflict(&task))
}
}
11 changes: 11 additions & 0 deletions vicky/src/lib/documents/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,17 @@ pub enum Lock {
READ { name: String },
}

impl Lock {
pub fn is_conflicting(&self, other: &Lock) -> bool {
match (self, other) {
(Lock::WRITE { name: name1 }, Lock::WRITE { name: name2 })
| (Lock::READ { name: name1 }, Lock::WRITE { name: name2 })
| (Lock::WRITE { name: name1 }, Lock::READ { name: name2 }) => name1 == name2,
_ => false,
}
}
}

type FlakeURI = String;

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
Expand Down

0 comments on commit 80fbe61

Please sign in to comment.