use crate::db::{self, params};
use anyhow::Context;
use std::collections::HashSet;
use tracing::warn;
pub const RETENTION_HOURS: i64 = 24;
const STOPPED_MID_FLIGHT: &str = "the product stopped before this handover's answer was delivered";
const PURGE_BATCH: i64 = 500;
const PURGE_PAUSE: std::time::Duration = std::time::Duration::from_millis(50);
const PURGE_BUDGET: std::time::Duration = std::time::Duration::from_secs(30);
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub(crate) enum State {
Running,
Done,
DoneNoAnswer,
Failed,
}
impl State {
#[must_use]
pub(crate) const fn as_str(self) -> &'static str {
match self {
Self::Running => "running",
Self::Done => "done",
Self::DoneNoAnswer => "done_no_answer",
Self::Failed => "failed",
}
}
fn parse(raw: &str) -> Option<Self> {
match raw {
"running" => Some(Self::Running),
"done" => Some(Self::Done),
"done_no_answer" => Some(Self::DoneNoAnswer),
"failed" => Some(Self::Failed),
_ => None,
}
}
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub(crate) enum Shape {
None,
Json,
Forwarded,
}
impl Shape {
#[must_use]
pub(crate) const fn as_str(self) -> &'static str {
match self {
Self::None => "none",
Self::Json => "json",
Self::Forwarded => "forwarded",
}
}
#[must_use]
pub(crate) const fn of(schema: bool, produced: bool) -> Self {
match (schema, produced) {
(false, _) => Self::None,
(true, true) => Self::Json,
(true, false) => Self::Forwarded,
}
}
fn parse(raw: &str) -> Option<Self> {
match raw {
"none" => Some(Self::None),
"json" => Some(Self::Json),
"forwarded" => Some(Self::Forwarded),
_ => None,
}
}
}
#[must_use]
pub(crate) fn acknowledged(answer: String, id: Option<&str>) -> String {
match id {
Some(id) => format!("{answer} (job {id})"),
None => answer,
}
}
pub(crate) struct Record {
pub state: State,
pub answer: Option<String>,
pub reason: Option<String>,
pub shape: Option<Shape>,
pub chain_steps: Option<usize>,
}
pub(crate) async fn begin(id: &str, chain_steps: Option<usize>) {
let chain_steps = chain_steps.and_then(|steps| i64::try_from(steps).ok());
let now = db::now();
let result = crate::session::store()
.conn
.upsert_row(
"UPDATE send_outcomes SET state = 'running', answer = NULL, reason = NULL, \
shape = NULL, finished_at = NULL, chain_steps = ?2, created_at = ?3 WHERE id = ?1",
|| params![id.to_string(), chain_steps, now.clone()],
"INSERT INTO send_outcomes (id, state, chain_steps, created_at) \
VALUES (?1, 'running', ?2, ?3) ON CONFLICT (id) DO NOTHING",
params![id.to_string(), chain_steps, now.clone()],
)
.await;
if let Err(e) = result {
warn!(id, error = %e, "Failed to open a command-line send's outcome record");
}
}
pub(crate) async fn finish(id: &str, answer: &str, shape: Shape) {
let (answer, state, shape) = if answer.trim().is_empty() {
(None, State::DoneNoAnswer, None)
} else {
(Some(answer.to_string()), State::Done, Some(shape.as_str()))
};
let result = crate::session::store()
.conn
.execute(
"UPDATE send_outcomes SET state = ?2, answer = ?3, shape = ?4, finished_at = ?5 \
WHERE id = ?1 AND state = 'running'",
params![id.to_string(), state.as_str(), answer, shape, db::now()],
)
.await;
if let Err(e) = result {
warn!(id, error = %e, "Failed to complete a command-line send's outcome record");
}
}
pub(crate) async fn fail(id: &str, reason: &str) {
let result = crate::session::store()
.conn
.execute(
"UPDATE send_outcomes SET state = 'failed', reason = ?2, finished_at = ?3 \
WHERE id = ?1 AND state = 'running'",
params![id.to_string(), reason.to_string(), db::now()],
)
.await;
if let Err(e) = result {
warn!(id, error = %e, "Failed to end a command-line send's outcome record");
}
}
pub(crate) async fn fail_dropped(pending_job_id: Option<&str>, reason: &str) {
if let Some(id) = pending_job_id {
fail(id, reason).await;
}
}
pub(crate) async fn running(ids: &[&str]) -> Option<HashSet<String>> {
if ids.is_empty() {
return Some(HashSet::new());
}
let bound: Vec<db::Value> = ids.iter().map(|id| db::Value::from(*id)).collect();
let rows = crate::session::store()
.conn
.query(
&format!(
"SELECT id FROM send_outcomes WHERE state = 'running' AND id IN ({})",
db::sql_in_placeholders(ids.len())
),
bound,
)
.await;
match rows {
Ok(rows) => Some(
rows.into_iter()
.filter_map(|row| row.get::<String>(0).ok())
.collect(),
),
Err(e) => {
warn!(error = %e, "Failed to read the running command-line send outcomes of a turn");
None
}
}
}
pub(crate) async fn lookup(id: &str) -> anyhow::Result<Option<Record>> {
let row = crate::session::store()
.conn
.query_optional(
"SELECT state, answer, reason, shape, chain_steps FROM send_outcomes WHERE id = ?1",
params![id.to_string()],
|row| {
Ok::<_, anyhow::Error>((
row.get::<String>(0)?,
row.get::<Option<String>>(1)?,
row.get::<Option<String>>(2)?,
row.get::<Option<String>>(3)?,
row.get::<Option<i64>>(4)?,
))
},
)
.await
.context("read a command-line send's outcome record")?;
let Some((state, answer, reason, shape, chain_steps)) = row else {
return Ok(None);
};
let Some(state) = State::parse(&state) else {
warn!(id, state = %state, "send_outcomes.state is not one this build knows");
return Ok(None);
};
Ok(Some(Record {
state,
answer,
reason,
shape: shape.as_deref().and_then(Shape::parse),
chain_steps: chain_steps.and_then(|steps| usize::try_from(steps).ok()),
}))
}
async fn work_stands(id: &str) -> Option<bool> {
match crate::session::store()
.conn
.query_optional(
"SELECT 1 FROM pending_jobs WHERE id = ?1 \
UNION ALL SELECT 1 FROM jobs WHERE id = ?1 LIMIT 1",
params![id.to_string()],
|_| Ok::<(), anyhow::Error>(()),
)
.await
{
Ok(found) => Some(found.is_some()),
Err(e) => {
warn!(id, error = %e, "Failed to read a handover's work while settling interrupted records");
None
}
}
}
pub async fn fail_interrupted() {
let rows = match crate::session::store()
.conn
.query(
"SELECT id, chain_steps FROM send_outcomes WHERE state = 'running'",
params![],
)
.await
{
Ok(rows) => rows,
Err(e) => {
warn!(error = %e, "Failed to read interrupted command-line send outcome records");
return;
}
};
for row in rows {
let Ok(id) = row.get::<String>(0) else {
continue;
};
let chain_steps = row
.get::<Option<i64>>(1)
.ok()
.flatten()
.and_then(|steps| usize::try_from(steps).ok());
let in_flight = match chain_steps {
Some(steps) => crate::jobs::chain_has_records(&id, 1..=steps).await != Some(false),
None => work_stands(&id).await != Some(false),
};
if !in_flight {
fail(&id, STOPPED_MID_FLIGHT).await;
}
}
}
pub async fn purge_expired(cutoff: String) -> anyhow::Result<u64> {
let conn = &crate::session::store().conn;
let deadline = std::time::Instant::now() + PURGE_BUDGET;
let mut total = 0u64;
loop {
let deleted = conn
.execute(
"DELETE FROM send_outcomes WHERE id IN (SELECT id FROM send_outcomes WHERE \
(finished_at IS NOT NULL AND finished_at < ?1) OR \
(finished_at IS NULL AND created_at < ?1) LIMIT ?2)",
params![cutoff.as_str(), PURGE_BATCH],
)
.await
.context("delete expired command-line send outcome records")?;
total += deleted;
if deleted == 0 || std::time::Instant::now() >= deadline {
break;
}
if !crate::shutdown::sleep_or_shutdown_or_drain(PURGE_PAUSE).await {
break;
}
}
Ok(total)
}
#[cfg(test)]
mod tests {
use super::*;
#[tokio::test]
async fn fail_dropped_settles_only_a_record_that_exists() {
let _guard = crate::util::test::STORE_LOCK.lock().await;
crate::util::test::init_test_stores().await;
begin("dropped_handover", None).await;
fail_dropped(Some("dropped_handover"), "the custom role no longer exists").await;
fail_dropped(None, "the custom role no longer exists").await;
let record = lookup("dropped_handover")
.await
.expect("read the record")
.expect("the record stands");
assert_eq!(record.state, State::Failed);
assert_eq!(
record.reason.as_deref(),
Some("the custom role no longer exists")
);
}
#[test]
fn acknowledged_names_the_id_it_printed() {
assert_eq!(
acknowledged("Message handed over".to_string(), Some("job_1")),
"Message handed over (job job_1)"
);
assert_eq!(
acknowledged("Message handed over".to_string(), None),
"Message handed over",
"a handover that opened no record prints no id"
);
}
#[tokio::test]
async fn begin_opens_a_running_record() {
let _guard = crate::util::test::STORE_LOCK.lock().await;
crate::util::test::init_test_stores().await;
begin("record_running", Some(3)).await;
let record = lookup("record_running")
.await
.expect("read the record")
.expect("the record stands");
assert_eq!(record.state, State::Running);
assert_eq!(record.answer, None);
assert_eq!(record.reason, None);
assert_eq!(record.shape, None);
assert_eq!(record.chain_steps, Some(3));
}
#[tokio::test]
async fn finish_completes_a_record_with_its_answer() {
let _guard = crate::util::test::STORE_LOCK.lock().await;
crate::util::test::init_test_stores().await;
begin("record_done", None).await;
finish("record_done", "the answer", Shape::Json).await;
let record = lookup("record_done")
.await
.expect("read the record")
.expect("the record stands");
assert_eq!(record.state, State::Done);
assert_eq!(record.answer.as_deref(), Some("the answer"));
assert_eq!(record.shape, Some(Shape::Json));
assert_eq!(record.chain_steps, None);
let finished = crate::session::store()
.conn
.query_optional(
"SELECT finished_at FROM send_outcomes WHERE id = ?1",
params!["record_done".to_string()],
|row| row.get::<Option<String>>(0),
)
.await
.expect("read finished_at")
.flatten();
assert!(finished.is_some(), "a completed record is stamped");
}
#[tokio::test]
async fn finish_with_an_empty_answer_is_done_no_answer() {
let _guard = crate::util::test::STORE_LOCK.lock().await;
crate::util::test::init_test_stores().await;
begin("record_empty", None).await;
finish("record_empty", " \n", Shape::Json).await;
let record = lookup("record_empty")
.await
.expect("read the record")
.expect("the record stands");
assert_eq!(record.state, State::DoneNoAnswer);
assert_eq!(record.answer, None);
assert_eq!(
record.shape, None,
"nothing came back, so nothing was shaped — not even the schema that was asked for"
);
}
#[tokio::test]
async fn fail_ends_a_record_with_its_reason() {
let _guard = crate::util::test::STORE_LOCK.lock().await;
crate::util::test::init_test_stores().await;
begin("record_failed", None).await;
fail("record_failed", "stopped from the dashboard").await;
let record = lookup("record_failed")
.await
.expect("read the record")
.expect("the record stands");
assert_eq!(record.state, State::Failed);
assert_eq!(record.reason.as_deref(), Some("stopped from the dashboard"));
assert_eq!(record.answer, None);
}
#[tokio::test]
async fn a_later_finish_cannot_overwrite_a_finished_record() {
let _guard = crate::util::test::STORE_LOCK.lock().await;
crate::util::test::init_test_stores().await;
begin("record_guarded", None).await;
fail("record_guarded", "the addressed turn failed").await;
finish("record_guarded", "a later answer", Shape::Json).await;
let record = lookup("record_guarded")
.await
.expect("read the record")
.expect("the record stands");
assert_eq!(record.state, State::Failed);
assert_eq!(record.reason.as_deref(), Some("the addressed turn failed"));
assert_eq!(record.answer, None, "the failed report is the final one");
}
#[tokio::test]
async fn begin_replaces_a_finished_record() {
let _guard = crate::util::test::STORE_LOCK.lock().await;
crate::util::test::init_test_stores().await;
begin("record_replaced", Some(2)).await;
finish("record_replaced", "the first answer", Shape::Json).await;
begin("record_replaced", Some(2)).await;
let record = lookup("record_replaced")
.await
.expect("read the record")
.expect("the record stands");
assert_eq!(record.state, State::Running);
assert_eq!(record.answer, None, "the old answer is gone");
assert_eq!(record.reason, None);
assert_eq!(record.shape, None);
assert_eq!(record.chain_steps, Some(2));
}
#[tokio::test]
#[serial_test::serial(send_outcome)]
async fn purge_expired_removes_only_what_is_past_its_retention() {
let _guard = crate::util::test::STORE_LOCK.lock().await;
crate::util::test::init_test_stores().await;
crate::session::store()
.conn
.execute("DELETE FROM send_outcomes", params![])
.await
.expect("clear the outcome records");
begin("purge_done", None).await;
finish("purge_done", "an answer", Shape::Json).await;
begin("purge_running", None).await;
let old = "2020-01-01T00:00:00+00:00";
crate::session::store()
.conn
.execute(
"UPDATE send_outcomes SET finished_at = ?1 WHERE id = 'purge_done'",
params![old],
)
.await
.expect("backdate the finished record");
let between = "2021-01-01T00:00:00+00:00".to_string();
assert_eq!(
purge_expired(between).await.expect("purge once"),
1,
"only the finished record is past its retention"
);
assert!(
lookup("purge_done").await.expect("read").is_none(),
"the finished record is gone"
);
assert!(
lookup("purge_running").await.expect("read").is_some(),
"a running record is kept until its own creation is past the cutoff"
);
let future = "2999-01-01T00:00:00+00:00".to_string();
assert_eq!(
purge_expired(future).await.expect("purge again"),
1,
"the running record is past the cutoff too"
);
assert!(
lookup("purge_running").await.expect("read").is_none(),
"the running record is gone once its creation is old enough"
);
}
#[tokio::test]
#[serial_test::serial(send_outcome)]
async fn fail_interrupted_fails_a_record_whose_work_is_gone() {
let _guard = crate::util::test::STORE_LOCK.lock().await;
crate::util::test::init_test_stores().await;
begin("interrupted_gone", None).await;
fail_interrupted().await;
let record = lookup("interrupted_gone")
.await
.expect("read the record")
.expect("the record stands");
assert_eq!(record.state, State::Failed);
assert_eq!(record.reason.as_deref(), Some(STOPPED_MID_FLIGHT));
assert_eq!(record.answer, None);
}
#[tokio::test]
#[serial_test::serial(send_outcome)]
async fn fail_interrupted_leaves_a_record_whose_work_still_stands() {
let _guard = crate::util::test::STORE_LOCK.lock().await;
crate::util::test::init_test_stores().await;
begin("interrupted_live", None).await;
crate::util::test::JobRowBuilder::new(
&crate::session::store().conn,
"interrupted_live",
"worker",
"worker",
"interrupted_ws",
)
.timestamps(db::now())
.insert()
.await
.expect("the handover's own round stands");
fail_interrupted().await;
let record = lookup("interrupted_live")
.await
.expect("read the record")
.expect("the record stands");
assert_eq!(record.state, State::Running);
}
#[tokio::test]
#[serial_test::serial(send_outcome)]
async fn fail_interrupted_leaves_a_chain_whose_step_still_stands() {
let _guard = crate::util::test::STORE_LOCK.lock().await;
crate::util::test::init_test_stores().await;
begin("interrupted_chain", Some(2)).await;
crate::util::test::JobRowBuilder::new(
&crate::session::store().conn,
crate::launch::record_id("interrupted_chain", 2),
"worker",
"worker",
"interrupted_chain_ws",
)
.timestamps(db::now())
.insert()
.await
.expect("the chain's second step stands");
fail_interrupted().await;
let record = lookup("interrupted_chain")
.await
.expect("read the record")
.expect("the record stands");
assert_eq!(record.state, State::Running);
}
}