Skip to content

Commit

Permalink
Merge pull request #24 from wobcom/fix/conflicting-locks
Browse files Browse the repository at this point in the history
vicky: don't accept conflicting locks / stale tasks
  • Loading branch information
Kek5chen authored Apr 24, 2024
2 parents 0094ea7 + 40f9053 commit 8c5e046
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
42 changes: 42 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 @@ -245,6 +255,10 @@ pub async fn tasks_add(
.requires_features(task.features.clone())
.build();

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 @@ -255,3 +269,31 @@ pub async fn tasks_add(

Ok(Json(ro_task))
}

#[cfg(test)]
mod tests {
use crate::tasks::check_lock_conflict;
use uuid::Uuid;
use vickylib::documents::{FlakeRef, Lock, Task, TaskBuilder, TaskStatus};

#[test]
fn add_new_conflicting_task() {
let task = Task::builder()
.with_display_name("Test 1")
.with_read_lock("mauz")
.with_write_lock("mauz")
.build();
assert!(check_lock_conflict(&task))
}

#[test]
fn add_new_not_conflicting_task() {
let task = Task::builder()
.with_display_name("Test 1")
.with_read_lock("mauz")
.with_read_lock("mauz")
.with_write_lock("delete_everything")
.build();
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 8c5e046

Please sign in to comment.