From d538f2adfa21ea7f99a2958c37744b80aeabddf5 Mon Sep 17 00:00:00 2001 From: hugocasa Date: Fri, 20 Nov 2026 16:55:30 +0100 Subject: [PATCH] fix: relax git auto-pull polling only once the webhook has delivered (#12532) * fix: relax git auto-pull polling only once the webhook has delivered Co-Authored-By: Claude Opus 5.5 * fix: keep a delivering webhook verified while its last pull failed Co-Authored-By: Claude Opus 5.5 * docs: state the caller contract of the webhook verification writer Co-Authored-By: Claude Opus 5.5 * fix: do not count a ref's first auto-pull as a missed webhook delivery Co-Authored-By: Claude Opus 5.5 * chore: update ee-repo-ref to 6c06d688fe435630e2827bc177f066d2646bb768 This commit updates the EE repository reference after PR #996 was merged in windmill-ee-private. Previous ee-repo-ref: 095ef5033bff64ed5b2d08c84bb0037b83449d88 New ee-repo-ref: 6c06d688fe435630e2827bc177f066d2646bb768 Automated by sync-ee-ref workflow. --------- Co-authored-by: Claude Opus 5.5 Co-authored-by: windmill-internal-app[bot] Co-authored-by: Ruben Fiszel --- ...1fadee66ac151e7cdbdd8601e6bab3c5bf12e.json | 16 +++++ backend/ee-repo-ref.txt | 2 +- backend/src/monitor.rs | 49 +++++++++++-- .../windmill-api-workspaces/src/workspaces.rs | 3 + backend/windmill-api/openapi.yaml | 4 ++ .../src/workspace_settings_document.rs | 1 + backend/windmill-common/src/workspaces.rs | 69 ++++++++++++++++++- .../git_sync/GitSyncRepositoryCard.svelte | 14 ++-- 8 files changed, 143 insertions(+), 15 deletions(-) create mode 100644 backend/.sqlx/query-2f97623f4c3d5fdf4fc741058ef1fadee66ac151e7cdbdd8601e6bab3c5bf12e.json diff --git a/backend/.sqlx/query-2f97623f4c3d5fdf4fc741058ef1fadee66ac151e7cdbdd8601e6bab3c5bf12e.json b/backend/.sqlx/query-2f97623f4c3d5fdf4fc741058ef1fadee66ac151e7cdbdd8601e6bab3c5bf12e.json new file mode 100644 index 0000000000..c5b854cffc --- /dev/null +++ b/backend/.sqlx/query-2f97623f4c3d5fdf4fc741058ef1fadee66ac151e7cdbdd8601e6bab3c5bf12e.json @@ -0,0 +1,16 @@ +{ + "db_name": "PostgreSQL", + "query": "\n UPDATE workspace_settings\n SET git_sync = jsonb_set(\n git_sync,\n '{repositories}',\n (SELECT jsonb_agg(\n CASE WHEN elem->>'git_repo_resource_path' = $2 AND elem ? 'auto_pull'\n THEN jsonb_set(elem, '{auto_pull,webhook_verified_id}',\n COALESCE(to_jsonb($3::bigint), 'null'::jsonb), true)\n ELSE elem END)\n FROM jsonb_array_elements(git_sync->'repositories') AS elem)\n )\n WHERE workspace_id = $1\n AND jsonb_typeof(git_sync->'repositories') = 'array'\n AND jsonb_array_length(git_sync->'repositories') > 0\n ", + "describe": { + "columns": [], + "parameters": { + "Left": [ + "Text", + "Text", + "Int8" + ] + }, + "nullable": [] + }, + "hash": "2f97623f4c3d5fdf4fc741058ef1fadee66ac151e7cdbdd8601e6bab3c5bf12e" +} diff --git a/backend/ee-repo-ref.txt b/backend/ee-repo-ref.txt index 024e0aa493..771a86c39d 100644 --- a/backend/ee-repo-ref.txt +++ b/backend/ee-repo-ref.txt @@ -1 +1 @@ -d01848c5a62d19896cf5c67dd0b69582e5365be9 +6c06d688fe435630e2827bc177f066d2646bb768 diff --git a/backend/src/monitor.rs b/backend/src/monitor.rs index 2cda5d8b0a..09cb7680cc 100644 --- a/backend/src/monitor.rs +++ b/backend/src/monitor.rs @@ -5374,7 +5374,7 @@ async fn poll_git_auto_pull_inner(db: &Pool) -> error::Result<()> { Ok(Some((git_ref, sha))) => { // Shared reconcile (also used by the webhook receiver): // checks should_pull, enqueues, and records status/failure. - if let Err(e) = windmill_git_sync::reconcile_and_enqueue_pull( + match windmill_git_sync::reconcile_and_enqueue_pull( db, &row.workspace_id, repo, @@ -5384,11 +5384,48 @@ async fn poll_git_auto_pull_inner(db: &Pool) -> error::Result<()> { ) .await { - tracing::warn!( - "git auto-pull: reconcile failed for {}/{}: {e:#}", - row.workspace_id, - repo.git_repo_resource_path - ); + // The webhook should have brought this head already: it stopped + // reaching us, so poll every minute again until it delivers. Not for + // a ref's first sync, which no delivery could have brought, nor after + // a failed pull: its rollback re-offers a head the webhook did deliver, + // and demoting would retry the failing pull every minute. + Ok(Some(_)) + if auto_pull.webhook_verified() + && auto_pull.last_synced_sha.contains_key(&git_ref) + && auto_pull + .last_pull_status + .as_ref() + .map_or(true, |s| s.success) => + { + tracing::warn!( + "git auto-pull: {}/{} moved without a webhook delivery, polling every minute until one arrives", + row.workspace_id, + repo.git_repo_resource_path + ); + if let Err(e) = + windmill_common::workspaces::set_auto_pull_webhook_verified_id( + db, + &row.workspace_id, + &repo.git_repo_resource_path, + None, + ) + .await + { + tracing::warn!( + "git auto-pull: could not record the missed delivery for {}/{}: {e:#}", + row.workspace_id, + repo.git_repo_resource_path + ); + } + } + Ok(_) => {} + Err(e) => { + tracing::warn!( + "git auto-pull: reconcile failed for {}/{}: {e:#}", + row.workspace_id, + repo.git_repo_resource_path + ); + } } // Parent-managed fork sync: list the fork branches' heads in diff --git a/backend/windmill-api-workspaces/src/workspaces.rs b/backend/windmill-api-workspaces/src/workspaces.rs index e5044cdb8a..3c4b533565 100644 --- a/backend/windmill-api-workspaces/src/workspaces.rs +++ b/backend/windmill-api-workspaces/src/workspaces.rs @@ -1022,6 +1022,7 @@ fn sanitize_client_auto_pull( auto_pull.webhook_secret = None; auto_pull.webhook_url = None; auto_pull.webhook_error = None; + auto_pull.webhook_verified_id = None; auto_pull.last_synced_sha = std::collections::HashMap::new(); auto_pull.last_pull_status = None; auto_pull.enabled_by = auto_pull.enabled.then(|| saver_email.to_string()); @@ -5456,6 +5457,7 @@ async fn edit_git_sync_config( new_ap.webhook_id = old_ap.webhook_id; new_ap.webhook_secret = old_ap.webhook_secret.clone(); new_ap.webhook_url = old_ap.webhook_url.clone(); + new_ap.webhook_verified_id = old_ap.webhook_verified_id; new_ap.last_synced_sha = old_ap.last_synced_sha.clone(); new_ap.last_pull_status = old_ap.last_pull_status.clone(); } @@ -5714,6 +5716,7 @@ async fn edit_git_sync_repository( new_ap.webhook_id = old_ap.webhook_id; new_ap.webhook_secret = old_ap.webhook_secret.clone(); new_ap.webhook_url = old_ap.webhook_url.clone(); + new_ap.webhook_verified_id = old_ap.webhook_verified_id; } // UI omitted auto_pull (e.g. older client): keep existing config. (None, Some(_)) => { diff --git a/backend/windmill-api/openapi.yaml b/backend/windmill-api/openapi.yaml index 6cbe0b4966..54a7f43e82 100644 --- a/backend/windmill-api/openapi.yaml +++ b/backend/windmill-api/openapi.yaml @@ -40128,6 +40128,10 @@ components: type: string webhook_error: type: string + webhook_verified_id: + type: integer + format: int64 + description: The webhook_id a delivery from the host last arrived for. Polling relaxes to about every 10 minutes only while it matches webhook_id. Set by the server. last_synced_sha: type: object additionalProperties: diff --git a/backend/windmill-api/src/workspace_settings_document.rs b/backend/windmill-api/src/workspace_settings_document.rs index a670340706..d793841e6c 100644 --- a/backend/windmill-api/src/workspace_settings_document.rs +++ b/backend/windmill-api/src/workspace_settings_document.rs @@ -106,6 +106,7 @@ pub(crate) fn redact_git_sync(git_sync: Option) -> Option { "webhook_secret", "webhook_id", "webhook_error", + "webhook_verified_id", "last_synced_sha", "last_pull_status", "enabled_by", diff --git a/backend/windmill-common/src/workspaces.rs b/backend/windmill-common/src/workspaces.rs index 2c43456896..e296b59b95 100644 --- a/backend/windmill-common/src/workspaces.rs +++ b/backend/windmill-common/src/workspaces.rs @@ -561,6 +561,12 @@ pub struct AutoPullSettings { /// webhook is live or the repo is polling-only. #[serde(default, skip_serializing_if = "Option::is_none")] pub webhook_error: Option, + /// The `webhook_id` a delivery from the host last arrived for. Creating a hook + /// only proves Windmill reaches the host, not the reverse, so polling relaxes + /// only once this matches `webhook_id`; a poll finding a head no delivery + /// brought clears it. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub webhook_verified_id: Option, /// Last synced commit sha per tracked git ref (e.g. `refs/heads/main`). #[serde(default, skip_serializing_if = "std::collections::HashMap::is_empty")] pub last_synced_sha: std::collections::HashMap, @@ -589,6 +595,7 @@ impl std::fmt::Debug for AutoPullSettings { ) .field("webhook_url", &self.webhook_url) .field("webhook_error", &self.webhook_error) + .field("webhook_verified_id", &self.webhook_verified_id) .field("last_synced_sha", &self.last_synced_sha) .field("last_pull_status", &self.last_pull_status) .finish() @@ -602,11 +609,16 @@ pub const DEFAULT_AUTO_PULL_POLL_INTERVAL_S: u32 = 60; pub const WEBHOOK_AUTO_PULL_POLL_INTERVAL_S: u32 = 600; impl AutoPullSettings { + /// Whether the registered hook has been seen delivering to this instance. + pub fn webhook_verified(&self) -> bool { + self.webhook_id.is_some() && self.webhook_verified_id == self.webhook_id + } + /// Effective polling interval in seconds, honouring the explicit override and - /// relaxing to `WEBHOOK_AUTO_PULL_POLL_INTERVAL_S` when a webhook is live. + /// relaxing to `WEBHOOK_AUTO_PULL_POLL_INTERVAL_S` once the webhook delivers. pub fn effective_poll_interval_s(&self) -> u32 { self.poll_interval_s.unwrap_or_else(|| { - if self.webhook_id.is_some() { + if self.webhook_verified() { WEBHOOK_AUTO_PULL_POLL_INTERVAL_S } else { DEFAULT_AUTO_PULL_POLL_INTERVAL_S @@ -627,6 +639,44 @@ impl AutoPullSettings { } } +/// Record the hook a delivery arrived for (`Some`), or that a poll found a head no +/// delivery brought (`None`), on one repository of `workspace_settings.git_sync` +/// without touching the rest of it. Authorizes nothing: callers pass a workspace +/// and repository they resolved themselves, the receiver only after verifying the +/// delivery against that repository's own secret, never ids taken from a request. +pub async fn set_auto_pull_webhook_verified_id( + db: &crate::DB, + w_id: &str, + repo_resource_path: &str, + hook_id: Option, +) -> Result<()> { + // COALESCE: `jsonb_set` with a NULL value returns NULL, which would wipe git_sync. + sqlx::query!( + r#" + UPDATE workspace_settings + SET git_sync = jsonb_set( + git_sync, + '{repositories}', + (SELECT jsonb_agg( + CASE WHEN elem->>'git_repo_resource_path' = $2 AND elem ? 'auto_pull' + THEN jsonb_set(elem, '{auto_pull,webhook_verified_id}', + COALESCE(to_jsonb($3::bigint), 'null'::jsonb), true) + ELSE elem END) + FROM jsonb_array_elements(git_sync->'repositories') AS elem) + ) + WHERE workspace_id = $1 + AND jsonb_typeof(git_sync->'repositories') = 'array' + AND jsonb_array_length(git_sync->'repositories') > 0 + "#, + w_id, + repo_resource_path, + hook_id, + ) + .execute(db) + .await?; + Ok(()) +} + #[derive(Serialize, Deserialize, Debug, Clone)] pub struct GitSyncSettings { pub include_path: Vec, @@ -4226,6 +4276,7 @@ mod tests { webhook_secret: None, webhook_url: None, webhook_error: None, + webhook_verified_id: None, enabled_by: None, last_synced_sha: synced .iter() @@ -4262,9 +4313,21 @@ mod tests { // explicit override wins s.poll_interval_s = Some(15); assert_eq!(s.effective_poll_interval_s(), 15); - // with a live webhook and no override, relax to the webhook interval + // a registered hook nothing has delivered from keeps polling every minute s.poll_interval_s = None; s.webhook_id = Some(42); + assert_eq!( + s.effective_poll_interval_s(), + DEFAULT_AUTO_PULL_POLL_INTERVAL_S + ); + // a delivery for an older hook does not count for its replacement + s.webhook_verified_id = Some(41); + assert_eq!( + s.effective_poll_interval_s(), + DEFAULT_AUTO_PULL_POLL_INTERVAL_S + ); + // once it delivers, relax to the webhook interval + s.webhook_verified_id = Some(42); assert_eq!( s.effective_poll_interval_s(), WEBHOOK_AUTO_PULL_POLL_INTERVAL_S diff --git a/frontend/src/lib/components/git_sync/GitSyncRepositoryCard.svelte b/frontend/src/lib/components/git_sync/GitSyncRepositoryCard.svelte index 27ae11e412..82b932b333 100644 --- a/frontend/src/lib/components/git_sync/GitSyncRepositoryCard.svelte +++ b/frontend/src/lib/components/git_sync/GitSyncRepositoryCard.svelte @@ -957,7 +957,13 @@ {/if} {#if repo.auto_pull?.enabled} - {@const viaWebhook = repo.auto_pull?.webhook_id != null} + {@const hookId = repo.auto_pull?.webhook_id} + {@const viaWebhook = + hookId != null && repo.auto_pull?.webhook_verified_id === hookId} + {@const pollNote = + hookId != null + ? ' Checking the tracked branch about every minute until the webhook delivers to this instance.' + : ' Checking the tracked branch about every minute.'} {#if hasManagedCredential}
@@ -976,9 +982,7 @@ date={pullStatusDate(repo.auto_pull.last_pull_status)} noSeconds />. - {viaWebhook - ? ' Syncing instantly via webhook.' - : ' Checking the tracked branch about every minute.'} + {viaWebhook ? ' Syncing instantly via webhook.' : pollNote} {:else} Last sync failed @@ -993,7 +997,7 @@ {:else} {viaWebhook ? 'Connected via webhook. New commits to the tracked branch deploy instantly.' - : 'Checking the tracked branch about every minute. New commits deploy automatically.'} + : `${pollNote.trim()} New commits deploy automatically.`} {/if}
{#if repo.auto_pull?.enabled_by}