Implement replay-safe authorized public HTTP API
This commit is contained in:
parent
791e00f4c0
commit
4c93c25692
1 changed files with 3940 additions and 345 deletions
|
|
@ -5,7 +5,8 @@ use remora_domain::{
|
|||
FailureClass, FailureDisposition, JobPhase, OpaqueId, fingerprint, ports::MutationScope,
|
||||
};
|
||||
use remora_durable::{
|
||||
DurableError, MutationDisposition, MutationStatus, enqueue, execute_mutation, with_transaction,
|
||||
DurableError, MutationDisposition, MutationStatus, PgTx, enqueue, execute_mutation,
|
||||
with_transaction,
|
||||
};
|
||||
use remora_target::{attempt_key_attempt_id, attempt_staging_prefix, validate_attempt_key};
|
||||
use serde::Serialize;
|
||||
|
|
@ -24,56 +25,61 @@ impl AttemptService {
|
|||
pub async fn request_cancel(&self, job_id: &OpaqueId) -> Result<CancelOutcome, AttemptError> {
|
||||
with_transaction(&self.pool, |tx| {
|
||||
let job_id = job_id.clone();
|
||||
Box::pin(async move {
|
||||
let Some(row) = sqlx::query_as::<_, CancelRow>(
|
||||
"SELECT phase, cancel_requested FROM jobs WHERE job_id = $1 FOR UPDATE",
|
||||
)
|
||||
.bind(job_id.as_str())
|
||||
.fetch_optional(tx.as_mut())
|
||||
.await?
|
||||
else {
|
||||
return Ok(CancelOutcome::NotFound);
|
||||
};
|
||||
let phase = parse_phase(&row.phase)?;
|
||||
if phase.is_terminal() {
|
||||
return Ok(CancelOutcome::AlreadyTerminal);
|
||||
}
|
||||
if row.cancel_requested {
|
||||
return Ok(CancelOutcome::AlreadyRequested);
|
||||
}
|
||||
let now = OffsetDateTime::now_utc();
|
||||
if phase == JobPhase::Running {
|
||||
sqlx::query(
|
||||
"UPDATE jobs SET cancel_requested = TRUE, phase = 'cancelling',
|
||||
dispatch_needed = FALSE, coordination_cleaned_at = NULL,
|
||||
revision = revision + 1, updated_at = $2
|
||||
WHERE job_id = $1",
|
||||
)
|
||||
.bind(job_id.as_str())
|
||||
.bind(now)
|
||||
.execute(tx.as_mut())
|
||||
.await?;
|
||||
} else {
|
||||
sqlx::query(
|
||||
"UPDATE jobs SET cancel_requested = TRUE, phase = 'cancelled',
|
||||
authority_attempt_id = NULL, dispatch_needed = FALSE,
|
||||
terminal_at = $2, coordination_cleaned_at = NULL,
|
||||
revision = revision + 1, updated_at = $2
|
||||
WHERE job_id = $1",
|
||||
)
|
||||
.bind(job_id.as_str())
|
||||
.bind(now)
|
||||
.execute(tx.as_mut())
|
||||
.await?;
|
||||
}
|
||||
enqueue(tx, "job.updated", json!({ "job_id": job_id })).await?;
|
||||
Ok(CancelOutcome::Requested)
|
||||
})
|
||||
Box::pin(async move { Self::request_cancel_in_tx(tx, &job_id).await })
|
||||
})
|
||||
.await
|
||||
.map_err(Into::into)
|
||||
}
|
||||
|
||||
pub async fn request_cancel_in_tx(
|
||||
tx: &mut PgTx<'_>,
|
||||
job_id: &OpaqueId,
|
||||
) -> Result<CancelOutcome, DurableError> {
|
||||
let Some(row) = sqlx::query_as::<_, CancelRow>(
|
||||
"SELECT phase, cancel_requested FROM jobs WHERE job_id = $1 FOR UPDATE",
|
||||
)
|
||||
.bind(job_id.as_str())
|
||||
.fetch_optional(tx.as_mut())
|
||||
.await?
|
||||
else {
|
||||
return Ok(CancelOutcome::NotFound);
|
||||
};
|
||||
let phase = parse_phase(&row.phase)?;
|
||||
if phase.is_terminal() {
|
||||
return Ok(CancelOutcome::AlreadyTerminal);
|
||||
}
|
||||
if row.cancel_requested {
|
||||
return Ok(CancelOutcome::AlreadyRequested);
|
||||
}
|
||||
let now = OffsetDateTime::now_utc();
|
||||
if phase == JobPhase::Running {
|
||||
sqlx::query(
|
||||
"UPDATE jobs SET cancel_requested = TRUE, phase = 'cancelling',
|
||||
dispatch_needed = FALSE, coordination_cleaned_at = NULL,
|
||||
revision = revision + 1, updated_at = $2
|
||||
WHERE job_id = $1",
|
||||
)
|
||||
.bind(job_id.as_str())
|
||||
.bind(now)
|
||||
.execute(tx.as_mut())
|
||||
.await?;
|
||||
} else {
|
||||
sqlx::query(
|
||||
"UPDATE jobs SET cancel_requested = TRUE, phase = 'cancelled',
|
||||
authority_attempt_id = NULL, dispatch_needed = FALSE,
|
||||
terminal_at = $2, coordination_cleaned_at = NULL,
|
||||
revision = revision + 1, updated_at = $2
|
||||
WHERE job_id = $1",
|
||||
)
|
||||
.bind(job_id.as_str())
|
||||
.bind(now)
|
||||
.execute(tx.as_mut())
|
||||
.await?;
|
||||
}
|
||||
enqueue(tx, "job.updated", json!({ "job_id": job_id })).await?;
|
||||
Ok(CancelOutcome::Requested)
|
||||
}
|
||||
|
||||
pub async fn cooperative_stop(
|
||||
&self,
|
||||
authority: &AttemptAuthority,
|
||||
|
|
|
|||
Loading…
Reference in a new issue