use std::sync::Arc;
use adk_core::{AdkError, ErrorCategory, ErrorComponent, Result};
use adk_harness::website::SiteRuntime;
use adk_site::pipeline::{Change, EntryPoint, PipelineOutcome};
use async_trait::async_trait;
use awp_types::ContentId;
use super::{ExperimentRecord, ExperimentStopHook};
pub struct RepublishOnStop {
runtime: Arc<SiteRuntime>,
proposer: String,
}
impl std::fmt::Debug for RepublishOnStop {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("RepublishOnStop").field("proposer", &self.proposer).finish_non_exhaustive()
}
}
impl RepublishOnStop {
pub fn new(runtime: Arc<SiteRuntime>, proposer: impl Into<String>) -> Self {
Self { runtime, proposer: proposer.into() }
}
}
#[async_trait]
impl ExperimentStopHook for RepublishOnStop {
async fn stopped(&self, record: &ExperimentRecord) -> Result<()> {
let Some(id) = &record.plan.item else { return Ok(()) };
let failed = |message: String| {
AdkError::new(
ErrorComponent::Tool,
ErrorCategory::Unavailable,
"website.experiment.republish",
message,
)
};
let item = self
.runtime
.store()
.get(&ContentId(id.clone()))
.await
.map_err(|error| failed(error.to_string()))?
.ok_or_else(|| {
failed(format!("content item {id} of experiment {} is gone", record.plan.id))
})?;
let change = Change {
items: vec![item],
media: Vec::new(),
entry: EntryPoint::SiteAgent { agent: self.proposer.clone() },
proposer: self.proposer.clone(),
job: record.plan.job.clone(),
action_class: "publish".into(),
};
let outcome =
self.runtime.pipeline().run(change).await.map_err(|error| failed(error.to_string()))?;
match outcome {
PipelineOutcome::Published(published) => {
tracing::info!(experiment = %record.plan.id, site.release.seq = published.staged.manifest.seq, "control served alone after the experiment stopped");
Ok(())
}
PipelineOutcome::Held(gate) => Err(failed(format!(
"the launch gate held the release without experiment {}: {}",
record.plan.id,
gate.summary()
))),
PipelineOutcome::Pending { ticket, .. } => {
tracing::info!(experiment = %record.plan.id, ticket = %ticket, "the release without the stopped experiment waits for its approval chain");
Ok(())
}
PipelineOutcome::Rejected { by, reason } => Err(failed(format!(
"{by} rejected the release without experiment {}: {reason}",
record.plan.id
))),
PipelineOutcome::Scheduled { .. } => Ok(()),
}
}
}