Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 45 additions & 1 deletion oxa/oxa-work/src/publish/rerun.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
use super::{github_capture, LandingPublisher, Repository};
use crate::board::queue::{is_node_id, is_object_id};
use crate::board::{
check_recovery::{JobAttempt, JobLineage},
queue::{is_node_id, is_object_id},
};
use crate::{github_http_response, Result};
use serde::{Deserialize, Serialize};

Expand All @@ -23,7 +26,48 @@ pub struct RerunIntent {
pub run_attempt: u64,
}

impl From<JobAttempt<'_>> for RerunIntent {
fn from(attempt: JobAttempt<'_>) -> Self {
let lineage = attempt.lineage;
Self {
key: RerunKey {
repository_id: lineage.repository_id.to_owned(),
head: lineage.head.to_owned(),
workflow_run_id: lineage.workflow_run_id,
logical_job: lineage.required_name.to_owned(),
},
repository: lineage.repository.to_owned(),
workflow_path: lineage.workflow_path.to_owned(),
check_suite_id: lineage.check_suite_id,
check_run_id: attempt.check_run_id,
job_id: attempt.job_id,
run_attempt: attempt.run_attempt,
}
}
}

impl RerunIntent {
pub(crate) fn replaces(&self, before: &Self) -> bool {
self.valid() && self.as_attempt().replaces(&before.as_attempt())
}

fn as_attempt(&self) -> JobAttempt<'_> {
JobAttempt {
lineage: JobLineage {
repository: &self.repository,
repository_id: &self.key.repository_id,
head: &self.key.head,
workflow_path: &self.workflow_path,
required_name: &self.key.logical_job,
check_suite_id: self.check_suite_id,
workflow_run_id: self.key.workflow_run_id,
},
check_run_id: self.check_run_id,
job_id: self.job_id,
run_attempt: self.run_attempt,
}
}

pub(crate) fn valid(&self) -> bool {
is_node_id(&self.key.repository_id)
&& is_object_id(&self.key.head)
Expand Down
51 changes: 47 additions & 4 deletions oxa/oxa-work/src/shadow/rerun.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ use crate::{LandingPublisher, RerunAcknowledgment, RerunIntent, RerunKey, Result
use serde::{Deserialize, Serialize};
use std::{collections::BTreeMap, path::Path};

mod current;
pub use current::{run_current_once, CurrentSample};

#[derive(Default)]
pub struct Policy {
pub enabled: bool,
Expand All @@ -15,13 +18,21 @@ pub enum Status {
Skipped,
Reserved,
Acknowledged(RerunAcknowledgment),
Reconciled {
acknowledgment: RerunAcknowledgment,
replacement: RerunIntent,
},
}

#[derive(Clone, Deserialize, Serialize)]
#[serde(tag = "state", content = "binding", rename_all = "snake_case")]
pub(super) enum Record {
Reserved(RerunIntent),
Acknowledged(RerunAcknowledgment),
Reconciled {
acknowledgment: RerunAcknowledgment,
replacement: RerunIntent,
},
}

#[derive(Default)]
Expand All @@ -32,10 +43,18 @@ impl History {
let intent = match &record {
Record::Reserved(intent) => intent,
Record::Acknowledged(ack) => &ack.intent,
Record::Reconciled { acknowledgment, .. } => &acknowledgment.intent,
};
let valid_transition = match (&record, self.0.get(&intent.key)) {
(Record::Reserved(_), None) => true,
(Record::Acknowledged(_), Some(Record::Reserved(before))) => before == intent,
(
Record::Reconciled {
acknowledgment,
replacement,
},
Some(Record::Acknowledged(ack)),
) => acknowledgment == ack && replacement.replaces(intent),
_ => false,
};
if !intent.valid() || !valid_transition {
Expand All @@ -49,6 +68,13 @@ impl History {
self.0.get(key).map(|record| match record {
Record::Reserved(_) => Status::Reserved,
Record::Acknowledged(ack) => Status::Acknowledged(ack.clone()),
Record::Reconciled {
acknowledgment,
replacement,
} => Status::Reconciled {
acknowledgment: acknowledgment.clone(),
replacement: replacement.clone(),
},
})
}
}
Expand All @@ -67,6 +93,14 @@ pub fn run_once(
if !policy.enabled || run.offline {
return Ok(Status::Skipped);
}
let (mut log, history) = locked_history(journal_path)?;
if let Some(status) = history.reruns.status(&intent.key) {
return Ok(status);
}
publish(&mut log, policy, repo, run, gh_binary, intent, read)
}

fn locked_history(journal_path: &Path) -> Result<(File, Admissions)> {
let mut log = open_log_with_create(journal_path, false)?;
if log
.metadata()
Expand All @@ -77,15 +111,24 @@ pub fn run_once(
return Err("rerun history is missing from the owner journal".into());
}
let history = Admissions::load(&mut log)?;
if let Some(status) = history.reruns.status(&intent.key) {
return Ok(status);
}
Ok((log, history))
}

fn publish(
log: &mut File,
policy: &Policy,
repo: &Path,
run: &RunPolicy,
gh_binary: &Path,
intent: RerunIntent,
read: impl FnOnce() -> Result<(Board, Option<RerunIntent>)>,
) -> Result<Status> {
let inherited = log
.try_clone()
.map_err(|_| "cannot retain the owner journal lease")?;
let publisher = LandingPublisher::new(repo, run, gh_binary, &inherited);
let target = publisher.rerun_target(&intent, &policy.publisher_id)?;
dispatch(&mut log, intent, read, |intent| {
dispatch(log, intent, read, |intent| {
publisher.rerun_reserved(target, intent)
})
}
Expand Down
73 changes: 73 additions & 0 deletions oxa/oxa-work/src/shadow/rerun/current.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
use super::{append_publication, locked_history, publish, Board, Policy, Record, Result, RunPolicy, Status};
use crate::board::check_recovery::{decide, Current, Decision, History};
use crate::RerunIntent;
use serde_json::Value;
use std::path::Path;

/// A selected check and its snapshot from the protected Board reader.
pub struct CurrentSample<'a> {
pub board: &'a Board,
pub target: Current<'a>,
}

/// Use both read callbacks from the same bounded Board Reader. This entry point
/// has no production caller. The publisher and the journal stay unchanged when off.
pub fn run_current_once(
policy: &Policy,
journal_path: &Path,
repo: &Path,
run: &RunPolicy,
gh_binary: &Path,
sample: CurrentSample<'_>,
reader: (
impl FnMut(&str) -> Result<Value>,
impl FnMut(&str) -> Result<Value>,
),
) -> Result<Status> {
if !policy.enabled || run.offline {
return Ok(Status::Skipped);
}
let (mut log, history) = locked_history(journal_path)?;
let (mut repository_read, mut actions_json) = reader;
let mut observe = || {
sample
.target
.observe(&mut repository_read, &mut actions_json, |verified| {
verified.map(|verified| {
let intent = RerunIntent::from(verified.attempt);
let next = decide(
true,
&verified.observation,
sample.board.observed_at,
History::Unused,
);
let eligible = next == Decision::RerunJob(intent.job_id);
(intent, eligible)
})
})
};
let Some((intent, eligible)) = observe()? else {
return Ok(Status::Skipped);
};
match history.reruns.status(&intent.key) {
Some(Status::Acknowledged(acknowledgment)) if intent.replaces(&acknowledgment.intent) => {
let record = Record::Reconciled {
acknowledgment: acknowledgment.clone(),
replacement: intent.clone(),
};
append_publication(&mut log, sample.board, None, Some(&record))?;
Ok(Status::Reconciled {
acknowledgment,
replacement: intent,
})
}
Some(status) => Ok(status),
None if !eligible => Ok(Status::Skipped),
None => publish(&mut log, policy, repo, run, gh_binary, intent, || {
let fresh = observe()?
.filter(|(_, eligible)| *eligible)
.map(|(intent, _)| intent);
Ok((sample.board.clone(), fresh))
}),
}
}
Loading