Skip to content

Commit

Permalink
Ensure only successful tasks persist the sha.
Browse files Browse the repository at this point in the history
Also, failing tasks dont abort execution.
  • Loading branch information
bittrance committed Oct 6, 2023
1 parent 88f6392 commit a6b0b83
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 8 deletions.
9 changes: 7 additions & 2 deletions src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ pub enum GitOpsError {
FetchError(Box<dyn std::error::Error + Send + Sync>),
#[error("Failed to open repository: {0}")]
OpenRepo(gix::open::Error),
#[error("Action failed: {1} in {0}")]
ActionFailed(String, String),
#[error("Failed to send event: {0}")]
NotifyError(String),
#[error("Failed to launch action: {0}")]
Expand All @@ -57,7 +59,10 @@ pub enum GitOpsError {
impl GitOpsError {
#[allow(clippy::unused_self)]
pub fn is_fatal(&self) -> bool {
// TODO Some errors should be recovered
true
#[allow(clippy::match_like_matches_macro)]
match self {
Self::ActionFailed(..) => false,
_ => true,
}
}
}
13 changes: 13 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ mod lib {
use gix::{hash::Kind, ObjectId};

use crate::{
errors::GitOpsError,
task::{scheduled::ScheduledTask, State},
testutils::TestWorkload,
};
Expand Down Expand Up @@ -106,4 +107,16 @@ mod lib {
let progress = super::progress_one_task(&mut tasks[..], &mut persist).unwrap();
assert!(progress == super::Progress::Idle);
}

#[test]
fn dont_pesist_failing_task() {
let mut tasks = vec![ScheduledTask::new(TestWorkload::fail_with(|| {
GitOpsError::ActionFailed("ze-task".to_owned(), "ze-action".to_owned())
}))];
let mut persist = |_t: &ScheduledTask<TestWorkload>| Ok(());
super::progress_one_task(&mut tasks[..], &mut persist).unwrap();
tasks[0].await_finished();
super::progress_one_task(&mut tasks[..], &mut persist).unwrap();
assert_eq!(tasks[0].state().current_sha, ObjectId::null(Kind::Sha1));
}
}
7 changes: 6 additions & 1 deletion src/task/gixworkload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ impl Workload for GitWorkload {
new_sha,
))
.map_err(|err| GitOpsError::NotifyError(format!("{}", err)))?;
// TODO The returns dodge cleanup
match self.run_actions(&workdir, deadline, &sink) {
Ok(None) => {
sink.lock().unwrap()(WorkloadEvent::Success(self.config.name.clone(), new_sha))
Expand All @@ -104,10 +105,14 @@ impl Workload for GitWorkload {
Ok(Some(action_name)) => {
sink.lock().unwrap()(WorkloadEvent::Failure(
self.config.name.clone(),
action_name,
action_name.clone(),
new_sha,
))
.map_err(|err| GitOpsError::NotifyError(format!("{}", err)))?;
return Err(GitOpsError::ActionFailed(
self.config.name.clone(),
action_name,
));
}
Err(err) => {
sink.lock().unwrap()(WorkloadEvent::Error(
Expand Down
18 changes: 14 additions & 4 deletions src/testutils.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::{
path::PathBuf,
sync::{atomic::AtomicBool, Arc},
sync::Arc,
thread::sleep,
time::Duration,
};
Expand Down Expand Up @@ -28,7 +28,16 @@ impl<W: Workload + Clone + Send + 'static> ScheduledTask<W> {

#[derive(Clone, Default)]
pub struct TestWorkload {
pub status: Arc<AtomicBool>,
errfunc: Option<Arc<Box<dyn Fn() -> GitOpsError + Send + Sync>>>,
}

impl TestWorkload {
pub fn fail_with(errfunc: impl Fn() -> GitOpsError + Send + Sync + 'static) -> Self {
Self {
errfunc: Some(Arc::new(Box::new(errfunc))),
..Default::default()
}
}
}

impl Workload for TestWorkload {
Expand All @@ -41,9 +50,10 @@ impl Workload for TestWorkload {
}

fn perform(self, _workdir: PathBuf, _current_sha: ObjectId) -> Result<ObjectId, GitOpsError> {
self.status
.store(true, std::sync::atomic::Ordering::Relaxed);
sleep(Duration::from_millis(10));
if self.errfunc.is_some() {
return Err(self.errfunc.unwrap()());
}
Ok(ObjectId::empty_blob(gix::hash::Kind::Sha1))
}
}
3 changes: 2 additions & 1 deletion tests/workload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,8 @@ fn watch_failing_workload() {
Ok(())
});
let prev_sha = ObjectId::empty_tree(Kind::Sha1);
workload.perform(workdir.into_path(), prev_sha).unwrap();
let res = workload.perform(workdir.into_path(), prev_sha);
assert!(matches!(res, Err(GitOpsError::ActionFailed(..))));
let events = non_action_events(events);
assert_eq!(events.len(), 2);
assert!(matches!(events[0], WorkloadEvent::Changes(..)));
Expand Down

0 comments on commit a6b0b83

Please sign in to comment.