From c9513a1836ee057a289033567551aff457081156 Mon Sep 17 00:00:00 2001 From: AlexRV12 <71396855+AlexRV12@users.noreply.github.com> Date: Thu, 3 Dec 2026 18:53:15 +0100 Subject: [PATCH] refactor: keep caching Hub scripts by version alone --- backend/tests/hub_named_path.rs | 12 +--------- backend/tests/trigger_hub_target.rs | 2 +- backend/tests/workspace_handlers.rs | 4 ++-- backend/windmill-common/src/scripts.rs | 24 +++++-------------- backend/windmill-queue/tests/schedule_push.rs | 2 +- 5 files changed, 11 insertions(+), 33 deletions(-) diff --git a/backend/tests/hub_named_path.rs b/backend/tests/hub_named_path.rs index df758b1a91..cf8864a263 100644 --- a/backend/tests/hub_named_path.rs +++ b/backend/tests/hub_named_path.rs @@ -12,7 +12,7 @@ use windmill_test_utils::*; const DEV: &str = "SECRET_TOKEN_2"; const OP: &str = "OPERATOR_TOKEN"; -const HUB_ID: i32 = 9990792; +const HUB_ID: i32 = 990000792; const REFUSAL: &str = "Your role may not use this Hub script"; const FLOW_REFUSAL: &str = "Hub flows require a role that can write code"; @@ -311,16 +311,6 @@ async fn a_hub_grant_admits_approved_scripts_of_its_integration( assert!(err.contains(REFUSAL), "{err}"); assert_eq!(calls.load(std::sync::atomic::Ordering::SeqCst), before); - // Another Hub's script of the same id, cached above, is not what this Hub's approval admits. - let (other_hub, _) = start_stub_hub(vec![]).await; - let script = windmill_common::scripts::get_full_hub_script_by_id( - SLACK_READ, - &windmill_common::utils::HTTP_CLIENT, - None, - ) - .await?; - assert_eq!(script.content, format!("echo {other_hub}")); - for (url, id, reason) in [ ( "http://127.0.0.1:1".to_string(), diff --git a/backend/tests/trigger_hub_target.rs b/backend/tests/trigger_hub_target.rs index 7dc63e4f91..f42ff339b4 100644 --- a/backend/tests/trigger_hub_target.rs +++ b/backend/tests/trigger_hub_target.rs @@ -16,7 +16,7 @@ async fn hub_target_with_retry_runs_its_preprocessor(db: Pool) -> anyh initialize_tracing().await; let server = ApiServer::start(db.clone()).await?; - let version = "9990004"; + let version = "990000004"; let hub_dir = &*windmill_common::worker::HUB_CACHE_DIR; tokio::fs::create_dir_all(hub_dir).await?; tokio::fs::write( diff --git a/backend/tests/workspace_handlers.rs b/backend/tests/workspace_handlers.rs index fc36bbba3e..0ad1e044bd 100644 --- a/backend/tests/workspace_handlers.rs +++ b/backend/tests/workspace_handlers.rs @@ -288,7 +288,7 @@ async fn test_error_handler_triggered_on_hub_script_failure( let server = ApiServer::start(db.clone()).await?; - let version = "9990002"; + let version = "990000002"; let hub_dir = &*windmill_common::worker::HUB_CACHE_DIR; tokio::fs::create_dir_all(hub_dir).await?; tokio::fs::write( @@ -386,7 +386,7 @@ async fn test_success_handler_triggered_on_hub_script_success( let server = ApiServer::start(db.clone()).await?; - let version = "9990003"; + let version = "990000003"; let hub_dir = &*windmill_common::worker::HUB_CACHE_DIR; tokio::fs::create_dir_all(hub_dir).await?; tokio::fs::write( diff --git a/backend/windmill-common/src/scripts.rs b/backend/windmill-common/src/scripts.rs index 95438e4fdb..79a2065354 100644 --- a/backend/windmill-common/src/scripts.rs +++ b/backend/windmill-common/src/scripts.rs @@ -22,7 +22,7 @@ use backon::ConstantBuilder; use backon::{BackoffBuilder, Retryable}; use regex::Regex; -use crate::utils::{calculate_hash, parse_hub_id, StripPath}; +use crate::utils::{parse_hub_id, StripPath}; /// The tag a script's dependency job runs on, given the script's own tag; `None` leaves it to /// `push`'s default (the language tag). Every relock of a script must use this, or a relock @@ -247,17 +247,6 @@ pub async fn get_full_hub_script_by_path( get_full_hub_script_by_id(parse_hub_id(path.to_path(), "hub/")?, http_client, db).await } -/// The name a version is cached under. Private Hubs number their scripts alike, so theirs carry -/// the Hub: what [`crate::jobs::check_can_name_runnables`] read from the configured Hub must not -/// admit a script cached from another. Public ones keep the bare id, which images ship. -fn hub_cache_name(hub_base_url: &str, version: i32) -> String { - if version < PRIVATE_HUB_MIN_VERSION { - version.to_string() - } else { - format!("{version}-{}", &calculate_hash(hub_base_url)[..16]) - } -} - /// Performs no authorization and uses the instance's Hub credentials and shared cache: the caller /// must already have decided this user may run or read this script. pub async fn get_full_hub_script_by_id( @@ -268,9 +257,7 @@ pub async fn get_full_hub_script_by_id( // A cache entry that cannot be read or parsed counts as a miss rather than an error: // a truncated write leaves a file that exists but deserializes to nothing, and refetching // it is always preferable to failing the job push it was read for. - let hub_base_url = (**HUB_BASE_URL.load()).clone(); - let cache_name = hub_cache_name(&hub_base_url, version); - let cache_path = format!("{}/{cache_name}", *HUB_CACHE_DIR); + let cache_path = format!("{}/{version}", *HUB_CACHE_DIR); let cached = match tokio::fs::read_to_string(&cache_path).await { Ok(content) => serde_json::from_str::(&content) .inspect_err(|e| { @@ -289,10 +276,10 @@ pub async fn get_full_hub_script_by_id( return Ok(script); } - let script = fetch_hub_script(&hub_base_url, version, http_client, db).await?; + let script = fetch_hub_script(version, http_client, db).await?; if let Err(e) = crate::worker::write_file( &HUB_CACHE_DIR, - &cache_name, + &version.to_string(), &serde_json::to_string(&script).map_err(to_anyhow)?, ) { tracing::error!("failed to write hub script {version} to cache: {e}"); @@ -303,11 +290,12 @@ pub async fn get_full_hub_script_by_id( } async fn fetch_hub_script( - hub_base_url: &str, id: i32, http_client: &reqwest::Client, db: Option<&DB>, ) -> crate::error::Result { + let hub_base_url = (**HUB_BASE_URL.load()).clone(); + let response = (|| async { let response = http_get_from_hub( http_client, diff --git a/backend/windmill-queue/tests/schedule_push.rs b/backend/windmill-queue/tests/schedule_push.rs index aadb733e1c..7de6be09ff 100644 --- a/backend/windmill-queue/tests/schedule_push.rs +++ b/backend/windmill-queue/tests/schedule_push.rs @@ -1880,7 +1880,7 @@ mod schedule_push { #[sqlx::test(migrations = "../migrations", fixtures("base", "schedule_push"))] async fn test_push_hub_script_schedule(db: Pool) -> anyhow::Result<()> { // Seed the hub cache so the push resolves the script without the network. - let version = "9990001"; + let version = "990000001"; let hub_dir = &*windmill_common::worker::HUB_CACHE_DIR; tokio::fs::create_dir_all(hub_dir).await?; tokio::fs::write(