fix: close the job pause propagation window and surface read errors

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Ruben Fiszel
2026-10-23 16:32:32 +02:00
co-authored by Claude Opus 5.5
parent b8eda8c9a8
commit 6e8c9e3b18
8 changed files with 83 additions and 33 deletions
@@ -0,0 +1,20 @@
{
"db_name": "PostgreSQL",
"query": "SELECT workspace_id FROM workspace_job_pause WHERE paused_until > now()",
"describe": {
"columns": [
{
"ordinal": 0,
"name": "workspace_id",
"type_info": "Varchar"
}
],
"parameters": {
"Left": []
},
"nullable": [
false
]
},
"hash": "c1e17da7e9e2df6366f417e5f5685c73537bde89e52ebf9374e3512e4275d886"
}
+4 -4
View File
@@ -4467,11 +4467,11 @@ pub async fn monitor_db(
}
};
let resume_expired_job_pauses_f = async {
let maintain_job_pauses_f = async {
if server_mode && !initial_load {
if let Some(db) = conn.as_sql() {
if let Err(e) = windmill_common::job_pause::resume_expired(&db).await {
tracing::error!("Error resuming expired workspace job pauses: {e:#}");
if let Err(e) = windmill_common::job_pause::maintain_pauses(&db).await {
tracing::error!("Error maintaining workspace job pauses: {e:#}");
}
}
}
@@ -4639,7 +4639,7 @@ pub async fn monitor_db(
pipeline_freshness_watchdog_f,
reconcile_unarmed_schedules_f,
stranded_jobs_f,
resume_expired_job_pauses_f,
maintain_job_pauses_f,
);
}
+40 -25
View File
@@ -35,37 +35,31 @@ lazy_static::lazy_static! {
pub const JOB_PAUSE_CHANGE_CHANNEL: &str = "notify_workspace_job_pause_change";
/// The deadline of the workspace's pause, `None` when it is not paused. A pause past its deadline
/// reads as over even before the monitor deletes it. Fails open: a job is never held because the
/// pause could not be read. Takes the caller's connection: `push` reads it inside its own
/// transaction, where asking the pool for a second connection can starve it.
/// reads as over even before the monitor deletes it. Takes the caller's connection: `push` reads it
/// inside its own transaction, where asking the pool for a second connection can starve it, and
/// where a failed read has aborted the transaction anyway, so the error is returned.
pub async fn jobs_paused_until<'c, E: sqlx::PgExecutor<'c>>(
db: E,
w_id: &str,
) -> Option<DateTime<Utc>> {
) -> Result<Option<DateTime<Utc>>> {
let now = Utc::now();
let cached = JOB_PAUSE_CACHE
.get(w_id)
.filter(|(_, cached_at)| now.timestamp() - cached_at < CACHE_TTL_SECS);
let until = match cached {
Some((until, _)) => until,
None => match sqlx::query_scalar!(
"SELECT paused_until FROM workspace_job_pause WHERE workspace_id = $1",
w_id
)
.fetch_optional(db)
.await
{
Ok(until) => {
JOB_PAUSE_CACHE.insert(w_id.to_string(), (until, now.timestamp()));
until
}
Err(e) => {
tracing::error!("could not read the job pause of workspace {w_id}: {e:#}");
None
}
},
None => {
let until = sqlx::query_scalar!(
"SELECT paused_until FROM workspace_job_pause WHERE workspace_id = $1",
w_id
)
.fetch_optional(db)
.await?;
JOB_PAUSE_CACHE.insert(w_id.to_string(), (until, now.timestamp()));
until
}
};
until.filter(|until| *until > now)
Ok(until.filter(|until| *until > now))
}
pub fn evict(w_id: &str) {
@@ -154,7 +148,7 @@ pub async fn check_push<'c, E: sqlx::PgExecutor<'c>>(
if policy == Some(PausePolicy::Exempt) {
return Ok(None);
}
let Some(until) = jobs_paused_until(db, w_id).await else {
let Some(until) = jobs_paused_until(db, w_id).await? else {
return Ok(None);
};
match pause_decision(kind, trigger_kind, trigger_path, is_admin) {
@@ -180,6 +174,12 @@ pub async fn hold_queued_jobs(tx: &mut Transaction<'_, Postgres>, w_id: &str) ->
)
.execute(&mut **tx)
.await?;
sweep_queued_jobs(tx, w_id).await
}
/// The sweep half of [`hold_queued_jobs`], also run by the monitor while a pause lasts: a server
/// or worker whose cached read still says "not paused" queues jobs for up to the cache TTL.
async fn sweep_queued_jobs(tx: &mut Transaction<'_, Postgres>, w_id: &str) -> Result<i64> {
let held = sqlx::query_scalar!(
r#"WITH candidates AS (
SELECT q.id, q.scheduled_for
@@ -286,9 +286,24 @@ pub async fn resume(tx: &mut Transaction<'_, Postgres>, w_id: &str) -> Result<i6
release_held_jobs(tx, w_id).await
}
/// Ends every pause past its deadline and releases the jobs of workspaces that are no longer
/// paused. Safe to run on every server at once: each release deletes the rows it acts on.
pub async fn resume_expired(db: &DB) -> Result<()> {
/// Holds what slipped through since the last pass in workspaces still paused, ends every pause past
/// its deadline, and releases the jobs of workspaces that are no longer paused. Safe to run on
/// every server at once: the sweep skips locked rows and each release deletes the rows it acts on.
pub async fn maintain_pauses(db: &DB) -> Result<()> {
let paused = sqlx::query_scalar!(
"SELECT workspace_id FROM workspace_job_pause WHERE paused_until > now()"
)
.fetch_all(db)
.await?;
for w_id in paused {
let mut tx = db.begin().await?;
let held = sweep_queued_jobs(&mut tx, &w_id).await?;
tx.commit().await?;
if held > 0 {
tracing::info!("job pause of workspace {w_id} held {held} more queued jobs");
}
}
let workspaces = sqlx::query_scalar!(
r#"SELECT workspace_id AS "workspace_id!" FROM workspace_job_pause WHERE paused_until <= now()
UNION
@@ -178,7 +178,7 @@ pub async fn resume_suspended_trigger_jobs(
// Released in place mid-pause, a job would start during the deploy the pause protects; a
// repushed one goes through `push`, which applies the pause itself.
let workspace_paused = windmill_common::job_pause::jobs_paused_until(&mut *tx, &w_id)
.await
.await?
.is_some();
for job in jobs {
+2
View File
@@ -70,6 +70,7 @@ import {
pauseWorkspaceJobs,
resumeWorkspaceJobs,
waitForDependencyJobs,
waitForPauseToPropagate,
} from "../workspace/jobsPause.ts";
import {
deepEqual,
@@ -5848,6 +5849,7 @@ export async function push(
"wmill sync push",
)
: undefined;
if (jobsPause) await waitForPauseToPropagate();
log.info(colors.gray(`Applying changes to files ...`));
let stateful = opts.stateful;
+9
View File
@@ -39,6 +39,15 @@ export async function pauseWorkspaceJobs(
return pause;
}
// Servers and workers cache whether a workspace is paused for up to 10s; until then they can still
// start a new job. Deploying only after that keeps such a job on the pre-deploy state.
const PAUSE_PROPAGATION_MS = 11_000;
export async function waitForPauseToPropagate() {
log.info(colors.gray("Waiting for the pause to reach every server ..."));
await new Promise((r) => setTimeout(r, PAUSE_PROPAGATION_MS));
}
export async function resumeWorkspaceJobs(workspaceId: string) {
const released = await wmill.resumeJobs({ workspace: workspaceId });
log.info(
+6
View File
@@ -37,6 +37,12 @@ synchronous entry point needs the same wrapper. The policy is a task-local: it d
`scheduled_for = GREATEST(held due time, now())`, so that run keeps its slot.
- The pause is runtime state: it is not in the settings document (a settings push must not clear
it, a fork must not start paused) and is read through a 10s cache evicted by `notify_event`.
For up to that long after a pause starts, another server or worker can still queue or start a
job; the monitor re-sweeps paused workspaces every pass, and `sync push --pause-jobs` waits out
the TTL before deploying so a job that started in that window runs on the pre-deploy state.
- A job a waiting caller (`run_wait_result`, a sync route) pushed before the pause and that has not
started yet is held by the sweep like any queued job: that caller times out rather than getting
a 503, and the job runs on resume. Telling those jobs apart would need per-job state.
A pause past its deadline reads as over immediately; `monitor_db` deletes it and releases its
jobs, and also releases held rows left without a pause (a push that read a stale cache).
- `MAX_PAUSE_DAYS` (7) must stay well under the 30-day horizon after which a worker fails an
@@ -1872,9 +1872,7 @@
onclick={saveSlackSigningSecret}
disabled={!slackSigningSecret.trim() && !slackSigningSecretSet}
>
{slackSigningSecret.trim() || !slackSigningSecretSet
? 'Save'
: 'Remove'}
{slackSigningSecret.trim() || !slackSigningSecretSet ? 'Save' : 'Remove'}
</Button>
</div>
<div class="text-2xs text-hint"