fix: parse a schedule's target once, at save and when it fires (#12183)

This commit is contained in:
AlexRV12
2026-11-03 17:09:22 +01:00
committed by GitHub
parent 795ded9d19
commit 67dcf145f4
8 changed files with 139 additions and 25 deletions
@@ -0,0 +1 @@
-- Nothing to do here: the stripped `/` named nothing.
@@ -0,0 +1,12 @@
-- A schedule's target is compared with the path of the jobs it queues, and with a renamed
-- runnable's old path, neither of which starts with `/`. A target with more than one leading `/`
-- names nothing and is left as is: firing the schedule refuses it.
UPDATE schedule SET script_path = substr(script_path, 2)
WHERE script_path LIKE '/%' AND script_path NOT LIKE '//%';
-- A restore puts the trashed row back as saved, so the copies awaiting one get the same fix.
UPDATE trashbin
SET item_data = jsonb_set(item_data, '{row,script_path}',
to_jsonb(substr(item_data #>> '{row,script_path}', 2)))
WHERE item_kind = 'schedule'
AND item_data #>> '{row,script_path}' LIKE '/%'
AND item_data #>> '{row,script_path}' NOT LIKE '//%';
+43
View File
@@ -0,0 +1,43 @@
use serde_json::json;
use sqlx::{Pool, Postgres};
use windmill_test_utils::*;
#[sqlx::test(fixtures("base"))]
async fn create_stores_the_parsed_target(db: Pool<Postgres>) -> anyhow::Result<()> {
initialize_tracing().await;
let server = ApiServer::start(db.clone()).await?;
let base = format!(
"http://localhost:{}/api/w/test-workspace/schedules",
server.addr.port()
);
let client = reqwest::Client::new();
let create = |path: &str, script_path: &str| {
client
.post(format!("{base}/create"))
.bearer_auth("SECRET_TOKEN")
.json(&json!({
"path": path,
"schedule": "0 0 0 * * *",
"timezone": "UTC",
"script_path": script_path,
"is_flow": false,
"args": {},
"enabled": false,
}))
.send()
};
let resp = create("u/test-user/one_slash", "/u/test-user/x").await?;
assert_eq!(resp.status(), 200, "{}", resp.text().await?);
let stored: String = sqlx::query_scalar(
"SELECT script_path FROM schedule WHERE workspace_id = 'test-workspace' AND path = 'u/test-user/one_slash'",
)
.fetch_one(&db)
.await?;
assert_eq!(stored, "u/test-user/x");
let resp = create("u/test-user/two_slashes", "//u/test-user/x").await?;
assert_eq!(resp.status(), 400, "{}", resp.text().await?);
Ok(())
}
+9 -1
View File
@@ -21,6 +21,7 @@ use windmill_api_auth::{
};
use windmill_audit::audit_oss::audit_log;
use windmill_audit::ActionKind;
use windmill_common::runnable_ref::RunnableRef;
use windmill_common::DB;
use windmill_common::{
can_preserve_on_behalf_of,
@@ -268,10 +269,17 @@ async fn create_schedule(
Extension(db): Extension<DB>,
Extension(user_db): Extension<UserDB>,
Path(w_id): Path<String>,
Json(ns): Json<NewSchedule>,
Json(mut ns): Json<NewSchedule>,
) -> Result<String> {
check_scopes(&authed, || format!("schedules:write:{}", ns.path))?;
reject_reserved_schedule_path(&ns.path)?;
// Stored as parsed: the next-run check, the monitor's re-arm, the run history and renames
// all match the stored target exactly against job paths, which never start with `/`.
if !ns.script_path.is_empty() {
ns.script_path = RunnableRef::from_path(&ns.script_path, ns.is_flow)?
.path()
.to_string();
}
let authed = maybe_refresh_folders(&ns.path, &w_id, authed, &db).await;
+22 -20
View File
@@ -23,6 +23,7 @@ use windmill_common::get_latest_flow_version_id_for_path;
use windmill_common::jobs::JobPayload;
use windmill_common::jobs::JobTriggerKind;
use windmill_common::jobs::OnBehalfOf;
use windmill_common::runnable_ref::RunnableRef;
use windmill_common::runnable_settings::ConcurrencySettings;
use windmill_common::runnable_settings::DebouncingSettings;
use windmill_common::schedule::schedule_to_user;
@@ -43,6 +44,7 @@ use windmill_common::{
async fn get_schedule_metadata<'c>(
tx: &mut sqlx::Transaction<'c, sqlx::Postgres>,
schedule: &Schedule,
target: &RunnableRef,
) -> Result<(
Option<String>, // tag
Option<i32>, // timeout
@@ -61,7 +63,7 @@ async fn get_schedule_metadata<'c>(
None,
&mut **tx,
&schedule.workspace_id,
&schedule.script_path,
target.path(),
false,
)
.await?;
@@ -70,7 +72,7 @@ async fn get_schedule_metadata<'c>(
&mut **tx,
version,
&schedule.workspace_id,
&schedule.script_path,
target.path(),
)
.await?;
@@ -87,7 +89,7 @@ async fn get_schedule_metadata<'c>(
Some(version),
parsed_retry,
))
} else if schedule.script_path.starts_with("hub/") {
} else if target.path().starts_with("hub/") {
Ok((None, None, None, None, None, parsed_retry))
} else {
let (
@@ -111,7 +113,7 @@ async fn get_schedule_metadata<'c>(
) = windmill_common::get_latest_hash_for_path(
&mut **tx,
&schedule.workspace_id,
&schedule.script_path,
target.path(),
false,
)
.await?;
@@ -138,6 +140,7 @@ pub async fn push_scheduled_job<'c>(
));
}
let target = RunnableRef::from_path(&schedule.script_path, schedule.is_flow)?;
let sched =
ScheduleType::from_str(&schedule.schedule, schedule.cron_version.as_deref(), false)?;
@@ -200,7 +203,7 @@ pub async fn push_scheduled_job<'c>(
&schedule.workspace_id,
&schedule.path,
next,
&schedule.script_path
target.path()
)
.fetch_one(&mut *tx)
.warn_after_seconds_with_sql(1, "already_exists_job".to_string())
@@ -326,11 +329,11 @@ pub async fn push_scheduled_job<'c>(
// Get metadata from the scheduled script/flow for tag, timeout, etc.
let (tag, timeout, on_behalf_of, hash, flow_version, retry) =
get_schedule_metadata(&mut tx, schedule).await?;
get_schedule_metadata(&mut tx, schedule, &target).await?;
(
JobPayload::SingleStepFlow {
path: schedule.script_path.clone(),
path: target.path().to_string(),
hash,
flow_version,
language: None,
@@ -366,7 +369,7 @@ pub async fn push_scheduled_job<'c>(
None,
&mut *tx,
&schedule.workspace_id,
&schedule.script_path,
target.path(),
false,
)
.warn_after_seconds_with_sql(1, "get_latest_flow_version_id_for_path".to_string())
@@ -376,7 +379,7 @@ pub async fn push_scheduled_job<'c>(
&mut *tx,
version,
&schedule.workspace_id,
&schedule.script_path,
target.path(),
)
.warn_after_seconds_with_sql(1, "get_flow_version_info_from_version".to_string())
.await?;
@@ -391,7 +394,7 @@ pub async fn push_scheduled_job<'c>(
(
JobPayload::Flow {
path: schedule.script_path.clone(),
path: target.path().to_string(),
dedicated_worker,
apply_preprocessor: false,
version,
@@ -402,18 +405,18 @@ pub async fn push_scheduled_job<'c>(
None,
on_behalf_of,
)
} else if schedule.script_path.starts_with("hub/") {
} else if target.path().starts_with("hub/") {
let tag = schedule.tag.clone().filter(|t| !t.is_empty());
let payload = match &schedule.retry {
// The language is what lets `push` materialize this as a native retry
// instead of a flow wrapper, which would queue the next tick at start.
Some(retry) => JobPayload::SingleStepFlow {
path: schedule.script_path.clone(),
path: target.path().to_string(),
hash: None,
flow_version: None,
language: Some(
get_full_hub_script_by_path(
StripPath(schedule.script_path.clone()),
StripPath(target.path().to_string()),
&HTTP_CLIENT,
Some(db),
)
@@ -438,10 +441,9 @@ pub async fn push_scheduled_job<'c>(
concurrency_settings: ConcurrencySettings::default(),
debouncing_settings: DebouncingSettings::default(),
},
None => JobPayload::ScriptHub {
path: schedule.script_path.clone(),
apply_preprocessor: false,
},
None => {
JobPayload::ScriptHub { path: target.path().to_string(), apply_preprocessor: false }
}
};
(payload, tag, None, None)
} else {
@@ -466,7 +468,7 @@ pub async fn push_scheduled_job<'c>(
) = windmill_common::get_latest_hash_for_path(
&mut *tx,
&schedule.workspace_id,
&schedule.script_path,
target.path(),
false,
)
.warn_after_seconds_with_sql(1, "get_latest_hash_for_path".to_string())
@@ -500,7 +502,7 @@ pub async fn push_scheduled_job<'c>(
// cannot be evaluated and fails closed (no retry).
(
JobPayload::SingleStepFlow {
path: schedule.script_path.clone(),
path: target.path().to_string(),
hash: Some(hash),
flow_version: None,
language: Some(language),
@@ -534,7 +536,7 @@ pub async fn push_scheduled_job<'c>(
(
JobPayload::ScriptHash {
hash,
path: schedule.script_path.clone(),
path: target.path().to_string(),
cache_ttl,
cache_ignore_s3_path,
dedicated_worker,
@@ -145,6 +145,26 @@ mod schedule_push {
Ok(())
}
#[sqlx::test(migrations = "../migrations", fixtures("base", "schedule_push"))]
async fn test_push_parses_target(db: Pool<Postgres>) -> anyhow::Result<()> {
let authed = make_authed();
let schedule = make_schedule(|s| s.script_path = "/f/system/test_script".to_string());
let tx = db.begin().await?;
push_scheduled_job(&db, tx, &schedule, Some(&authed), None)
.await?
.commit()
.await?;
let (_, path, _, _) = get_queued_job(&db).await.unwrap();
assert_eq!(path.as_deref(), Some("f/system/test_script"));
let schedule = make_schedule(|s| s.script_path = "//f/system/test_script".to_string());
let tx = db.begin().await?;
assert!(push_scheduled_job(&db, tx, &schedule, Some(&authed), None)
.await
.is_err());
Ok(())
}
// -----------------------------------------------------------------------
// push_scheduled_job: flow schedule
// -----------------------------------------------------------------------
+11 -1
View File
@@ -107,6 +107,13 @@ async function get(opts: GlobalOptions & { json?: boolean }, path: string) {
}
}
// A server stores a target without its one tolerated leading `/`, while one predating
// that stores it as typed. Compared as written, the two spellings never match and every
// push re-saves the schedule, which resets its run-as to a non-admin pusher.
function canonicalTarget(path: string): string {
return path?.startsWith("/") && !path.startsWith("//") ? path.slice(1) : path;
}
export async function pushSchedule(
workspace: string,
path: string,
@@ -130,6 +137,8 @@ export async function pushSchedule(
// Strip CLI-only boolean marker before sending to API
delete (localSchedule as any).has_permissioned_as;
localSchedule.script_path = canonicalTarget(localSchedule.script_path);
// In a fork, the file's `enabled` is the parent's for a path the parent
// also has (see sync push's `parentOwnedScheduleEnabled`): the fork's own
// flag stays as it is.
@@ -158,7 +167,8 @@ export async function pushSchedule(
}
if (schedule) {
if (isSuperset(localSchedule, schedule)) {
const remote = { ...schedule, script_path: canonicalTarget(schedule.script_path) };
if (isSuperset(localSchedule, remote)) {
log.debug(`Schedule ${path} is up to date`);
return;
}
@@ -13,12 +13,13 @@ import { mockServices } from "./mock_services.ts";
let updateScheduleCalls: any[] = [];
let remotePermissionedAs: string | undefined = "u/svc";
let remoteScriptPath = "u/admin/script";
const REMOTE_SCHEDULE = () => ({
path: "u/admin/sched",
schedule: "0 0 */6 * * *",
timezone: "Etc/UTC",
script_path: "u/admin/script",
script_path: remoteScriptPath,
is_flow: false,
args: {},
enabled: false,
@@ -74,8 +75,8 @@ await mockModule("../src/core/auth.ts", (real) => ({
requireLogin: async () => ({}),
}));
const scheduleCommand = (await import("../src/commands/schedule/schedule.ts"))
.default;
const scheduleModule = await import("../src/commands/schedule/schedule.ts");
const scheduleCommand = scheduleModule.default;
async function pushIn(wmillYamlTail: string): Promise<void> {
const dir = await mkdtemp(join(tmpdir(), "windmill_sched_push_"));
@@ -107,6 +108,7 @@ describe("wmill schedule push ownership", () => {
beforeEach(() => {
updateScheduleCalls = [];
remotePermissionedAs = "u/svc";
remoteScriptPath = "u/admin/script";
});
test("keeps the remote's permissioned_as under syncBehavior v1", async () => {
@@ -141,4 +143,20 @@ describe("wmill schedule push ownership", () => {
expect(body.permissioned_as).toBeUndefined();
expect(body.preserve_permissioned_as).toBeUndefined();
});
for (const [server, stored] of [
["a current server", "u/admin/script"],
["an older server", "/u/admin/script"],
]) {
test(`a target written with one leading slash matches what ${server} stores (${stored})`, async () => {
remoteScriptPath = stored;
const { permissioned_as: _, path: __, ...remote } = REMOTE_SCHEDULE();
await scheduleModule.pushSchedule("w", "u/admin/sched.schedule.yaml", undefined, {
...remote,
script_path: "/u/admin/script",
} as any);
expect(updateScheduleCalls).toHaveLength(0);
});
}
});