mirror of
https://github.com/windmill-labs/windmill.git
synced 2026-12-04 00:06:39 +00:00
refactor: keep caching Hub scripts by version alone
This commit is contained in:
@@ -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(),
|
||||
|
||||
@@ -16,7 +16,7 @@ async fn hub_target_with_retry_runs_its_preprocessor(db: Pool<Postgres>) -> 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(
|
||||
|
||||
@@ -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(
|
||||
|
||||
@@ -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::<HubScript>(&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<HubScript> {
|
||||
let hub_base_url = (**HUB_BASE_URL.load()).clone();
|
||||
|
||||
let response = (|| async {
|
||||
let response = http_get_from_hub(
|
||||
http_client,
|
||||
|
||||
@@ -1880,7 +1880,7 @@ mod schedule_push {
|
||||
#[sqlx::test(migrations = "../migrations", fixtures("base", "schedule_push"))]
|
||||
async fn test_push_hub_script_schedule(db: Pool<Postgres>) -> 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(
|
||||
|
||||
Reference in New Issue
Block a user