mirror of
https://github.com/windmill-labs/windmill.git
synced 2026-11-29 08:06:46 +00:00
fix: clear a flow's own result only after its sync caller has read it
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
1cf93903d2
commit
ad54561b2b
+22
@@ -0,0 +1,22 @@
|
||||
{
|
||||
"db_name": "PostgreSQL",
|
||||
"query": "SELECT j.runnable_id FROM v2_job j JOIN v2_job_completed c ON c.id = j.id\n WHERE j.id = $1 AND j.kind = 'flow'",
|
||||
"describe": {
|
||||
"columns": [
|
||||
{
|
||||
"ordinal": 0,
|
||||
"name": "runnable_id",
|
||||
"type_info": "Int8"
|
||||
}
|
||||
],
|
||||
"parameters": {
|
||||
"Left": [
|
||||
"Uuid"
|
||||
]
|
||||
},
|
||||
"nullable": [
|
||||
true
|
||||
]
|
||||
},
|
||||
"hash": "78aa4eb3cff10feda7c7dd823ae9a537f635d33426bd1e3955b5521ee7445f04"
|
||||
}
|
||||
@@ -91,18 +91,20 @@ async fn test_schedule_job_deletion_is_idempotent(db: Pool<Postgres>) -> anyhow:
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// A loop iteration's root_job is stored NULL, so this pins that the flow-level setting reaches
|
||||
// jobs nested below the flow, and that the flow's own result stays cleared.
|
||||
#[cfg(feature = "enterprise")]
|
||||
#[sqlx::test(fixtures("base"))]
|
||||
async fn test_flow_level_delete_clears_every_job_of_the_run(
|
||||
db: Pool<Postgres>,
|
||||
) -> anyhow::Result<()> {
|
||||
use windmill_common::jobs::JobPayload;
|
||||
// ---------------------------------------------------------------------------
|
||||
// Integration: a flow's own delete_after_secs = 0
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
initialize_tracing().await;
|
||||
let server = ApiServer::start(db.clone()).await?;
|
||||
let flow = serde_json::from_value(serde_json::json!({
|
||||
#[cfg(feature = "enterprise")]
|
||||
const FLOW_PATH: &str = "u/test-user/delete_after_use_flow";
|
||||
#[cfg(feature = "enterprise")]
|
||||
const FLOW_VERSION: i64 = 8_812_004;
|
||||
|
||||
/// A sequential loop: its iterations are stored with a NULL root_job, so only a walk down
|
||||
/// parent_job reaches them.
|
||||
#[cfg(feature = "enterprise")]
|
||||
async fn deploy_loop_flow(db: &Pool<Postgres>) -> anyhow::Result<()> {
|
||||
let value = serde_json::json!({
|
||||
"delete_after_secs": 0,
|
||||
"modules": [{
|
||||
"id": "loop",
|
||||
@@ -114,32 +116,127 @@ async fn test_flow_level_delete_clears_every_job_of_the_run(
|
||||
"modules": [{ "id": "inner", "value": { "type": "identity" } }]
|
||||
}
|
||||
}]
|
||||
}))?;
|
||||
let flow_job =
|
||||
RunJob::from(JobPayload::RawFlow { value: flow, path: None, restarted_from: None })
|
||||
.arg("x", serde_json::json!("secret"))
|
||||
.run_until_complete(&db, false, server.addr.port())
|
||||
.await;
|
||||
});
|
||||
sqlx::query(
|
||||
"INSERT INTO flow (workspace_id, path, summary, description, value, edited_by, edited_at,
|
||||
schema, extra_perms, versions)
|
||||
VALUES ('test-workspace', $1, '', '', $2, 'test-user', now(), '{}', '{}', ARRAY[$3])",
|
||||
)
|
||||
.bind(FLOW_PATH)
|
||||
.bind(&value)
|
||||
.bind(FLOW_VERSION)
|
||||
.execute(db)
|
||||
.await?;
|
||||
sqlx::query(
|
||||
"INSERT INTO flow_version (id, workspace_id, path, value, schema, created_by)
|
||||
VALUES ($1, 'test-workspace', $2, $3, '{}', 'test-user')",
|
||||
)
|
||||
.bind(FLOW_VERSION)
|
||||
.bind(FLOW_PATH)
|
||||
.bind(&value)
|
||||
.execute(db)
|
||||
.await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
let kept = sqlx::query_scalar::<_, String>(
|
||||
/// `(flow_step_id, args, result)` of every job of the run, the flow job included as "flow".
|
||||
#[cfg(feature = "enterprise")]
|
||||
async fn run_data(
|
||||
db: &Pool<Postgres>,
|
||||
flow_id: uuid::Uuid,
|
||||
) -> anyhow::Result<Vec<(String, serde_json::Value, serde_json::Value)>> {
|
||||
Ok(sqlx::query_as(
|
||||
"WITH RECURSIVE run AS (
|
||||
SELECT id FROM v2_job WHERE id = $1
|
||||
UNION ALL
|
||||
SELECT j.id FROM v2_job j JOIN run r ON j.parent_job = r.id
|
||||
)
|
||||
SELECT COALESCE(j.flow_step_id, 'flow') || ' ' || j.args::text || ' ' || c.result::text
|
||||
FROM run JOIN v2_job j USING (id) JOIN v2_job_completed c USING (id)
|
||||
WHERE j.args <> '{}'::jsonb OR c.result <> '{}'::jsonb",
|
||||
SELECT COALESCE(j.flow_step_id, 'flow'), j.args, c.result
|
||||
FROM run JOIN v2_job j USING (id) JOIN v2_job_completed c USING (id)",
|
||||
)
|
||||
.bind(flow_job.id)
|
||||
.fetch_all(&db)
|
||||
.await?;
|
||||
let nb_jobs: i64 = sqlx::query_scalar("SELECT COUNT(*) FROM v2_job WHERE parent_job = $1")
|
||||
.bind(flow_job.id)
|
||||
.fetch_one(&db)
|
||||
.await?;
|
||||
.bind(flow_id)
|
||||
.fetch_all(db)
|
||||
.await?)
|
||||
}
|
||||
|
||||
assert!(nb_jobs > 0, "the flow should have run steps");
|
||||
#[cfg(feature = "enterprise")]
|
||||
#[sqlx::test(fixtures("base"))]
|
||||
async fn test_flow_level_delete_clears_steps_and_keeps_flow_result(
|
||||
db: Pool<Postgres>,
|
||||
) -> anyhow::Result<()> {
|
||||
use windmill_common::jobs::JobPayload;
|
||||
|
||||
initialize_tracing().await;
|
||||
deploy_loop_flow(&db).await?;
|
||||
let server = ApiServer::start(db.clone()).await?;
|
||||
let flow_job = RunJob::from(JobPayload::Flow {
|
||||
path: FLOW_PATH.to_string(),
|
||||
dedicated_worker: None,
|
||||
apply_preprocessor: false,
|
||||
version: FLOW_VERSION,
|
||||
labels: None,
|
||||
job_token_scopes: None,
|
||||
})
|
||||
.arg("x", serde_json::json!("secret"))
|
||||
.run_until_complete(&db, false, server.addr.port())
|
||||
.await;
|
||||
|
||||
let jobs = run_data(&db, flow_job.id).await?;
|
||||
assert!(jobs.len() > 1, "the flow should have run steps: {jobs:?}");
|
||||
for (step, args, result) in &jobs {
|
||||
assert_eq!(args, &serde_json::json!({}), "{step} kept its args");
|
||||
if step == "flow" {
|
||||
// An async run has no caller to read the result, so the flow keeps it.
|
||||
assert_ne!(
|
||||
result,
|
||||
&serde_json::json!({}),
|
||||
"the flow's result was cleared"
|
||||
);
|
||||
} else {
|
||||
assert_eq!(result, &serde_json::json!({}), "{step} kept its result");
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(feature = "enterprise")]
|
||||
#[sqlx::test(fixtures("base"))]
|
||||
async fn test_flow_level_delete_clears_flow_result_after_sync_read(
|
||||
db: Pool<Postgres>,
|
||||
) -> anyhow::Result<()> {
|
||||
initialize_tracing().await;
|
||||
deploy_loop_flow(&db).await?;
|
||||
let server = ApiServer::start(db.clone()).await?;
|
||||
let port = server.addr.port();
|
||||
let response = in_test_worker(
|
||||
windmill_common::worker::Connection::Sql(db.clone()),
|
||||
reqwest::Client::new()
|
||||
.post(format!(
|
||||
"http://localhost:{port}/api/w/test-workspace/jobs/run_wait_result/f/{FLOW_PATH}"
|
||||
))
|
||||
.bearer_auth("SECRET_TOKEN")
|
||||
.json(&serde_json::json!({ "x": "secret" }))
|
||||
.send(),
|
||||
port,
|
||||
)
|
||||
.await?
|
||||
.error_for_status()?
|
||||
.json::<serde_json::Value>()
|
||||
.await?;
|
||||
|
||||
assert_eq!(response.as_array().map(Vec::len), Some(2), "{response}");
|
||||
let flow_id: uuid::Uuid =
|
||||
sqlx::query_scalar("SELECT id FROM v2_job WHERE runnable_path = $1 AND parent_job IS NULL")
|
||||
.bind(FLOW_PATH)
|
||||
.fetch_one(&db)
|
||||
.await?;
|
||||
let kept: Vec<_> = run_data(&db, flow_id)
|
||||
.await?
|
||||
.into_iter()
|
||||
.filter(|(_, args, result)| {
|
||||
args != &serde_json::json!({}) || result != &serde_json::json!({})
|
||||
})
|
||||
.collect();
|
||||
assert!(kept.is_empty(), "jobs that kept args or result: {kept:?}");
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -842,6 +842,40 @@ pub async fn handle_delete_after_completion(
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Clears a flow job's own result once its synchronous caller has read it, when the flow's
|
||||
/// delete_after_secs is 0. The worker clears the rest of the run, but leaves this result so
|
||||
/// the caller can read it; for a delay above 0 the worker schedules the whole flow job.
|
||||
pub async fn delete_flow_result_after_read(db: &DB, job_uuid: Uuid) -> Result<(), Error> {
|
||||
#[cfg(feature = "enterprise")]
|
||||
{
|
||||
// Only a completed flow: after an early return or a timed-out wait the flow is still
|
||||
// running, and its args are still read by its remaining steps.
|
||||
let version = sqlx::query_scalar!(
|
||||
"SELECT j.runnable_id FROM v2_job j JOIN v2_job_completed c ON c.id = j.id
|
||||
WHERE j.id = $1 AND j.kind = 'flow'",
|
||||
job_uuid
|
||||
)
|
||||
.fetch_optional(db)
|
||||
.await?
|
||||
.flatten();
|
||||
if let Some(version) = version {
|
||||
let flow = match windmill_common::cache::flow::fetch_version_lite(db, version).await {
|
||||
Ok(flow) => flow,
|
||||
Err(_) => windmill_common::cache::flow::fetch_version(db, version).await?,
|
||||
};
|
||||
let flow_value = flow.value();
|
||||
if resolve_delete_after_secs(flow_value.delete_after_use, flow_value.delete_after_secs)
|
||||
== Some(0)
|
||||
{
|
||||
delete_job_metadata_after_use(db, job_uuid).await?;
|
||||
}
|
||||
}
|
||||
}
|
||||
#[cfg(not(feature = "enterprise"))]
|
||||
let _ = (db, job_uuid);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Queue limit check
|
||||
// ---------------------------------------------------------------------------
|
||||
@@ -1188,7 +1222,7 @@ pub async fn run_flow_and_wait_result(
|
||||
)
|
||||
.await?;
|
||||
|
||||
run_wait_result(
|
||||
let wait_result = run_wait_result(
|
||||
&db,
|
||||
uuid,
|
||||
w_id,
|
||||
@@ -1196,7 +1230,9 @@ pub async fn run_flow_and_wait_result(
|
||||
has_failure_module,
|
||||
&authed.username,
|
||||
)
|
||||
.await
|
||||
.await;
|
||||
delete_flow_result_after_read(db, uuid).await?;
|
||||
wait_result
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
@@ -29,9 +29,10 @@ use windmill_api_auth::{check_scopes, ApiAuthed};
|
||||
use windmill_api_jobs::execution::check_license_key_valid;
|
||||
use windmill_api_jobs::{
|
||||
execution::{
|
||||
check_tag_available_for_workspace, delete_job_metadata_after_use,
|
||||
push_flow_job_by_path_into_queue, push_script_job_by_path_into_queue, result_to_response,
|
||||
run_wait_result_internal, schedule_job_deletion,
|
||||
check_tag_available_for_workspace, delete_flow_result_after_read,
|
||||
delete_job_metadata_after_use, push_flow_job_by_path_into_queue,
|
||||
push_script_job_by_path_into_queue, result_to_response, run_wait_result_internal,
|
||||
schedule_job_deletion,
|
||||
},
|
||||
types::RunJobQuery,
|
||||
};
|
||||
@@ -683,6 +684,9 @@ pub async fn trigger_runnable_and_wait_for_result(
|
||||
Some(secs) => schedule_job_deletion(&db, uuid, &workspace_id, secs).await?,
|
||||
None => {}
|
||||
}
|
||||
if is_flow {
|
||||
delete_flow_result_after_read(db, uuid).await?;
|
||||
}
|
||||
|
||||
result_to_response(result, success)
|
||||
}
|
||||
@@ -744,6 +748,9 @@ pub async fn trigger_runnable_and_wait_for_raw_result(
|
||||
Some(secs) => schedule_job_deletion(&db, uuid, &workspace_id, secs).await?,
|
||||
None => {}
|
||||
}
|
||||
if is_flow {
|
||||
delete_flow_result_after_read(db, uuid).await?;
|
||||
}
|
||||
|
||||
Ok((result, success))
|
||||
}
|
||||
|
||||
@@ -1985,6 +1985,14 @@ pub async fn update_flow_status_after_job_completion_internal(
|
||||
})?;
|
||||
}
|
||||
}
|
||||
|
||||
let flow_value = flow_data.value();
|
||||
if let Some(secs) = windmill_common::jobs::resolve_delete_after_secs(
|
||||
flow_value.delete_after_use,
|
||||
flow_value.delete_after_secs,
|
||||
) {
|
||||
delete_flow_run_data(db, &flow_job, secs).await?;
|
||||
}
|
||||
}
|
||||
|
||||
if flow_job.is_canceled() {
|
||||
@@ -2085,19 +2093,6 @@ pub async fn update_flow_status_after_job_completion_internal(
|
||||
.started_at
|
||||
.map(|x| FlowJobDuration { started_at: x, duration_ms: duration });
|
||||
}
|
||||
|
||||
// Runs after the flow's completed row is written: clearing it earlier lets that
|
||||
// write put the flow's result back.
|
||||
#[cfg(feature = "enterprise")]
|
||||
if flow_job.parent_job.is_none() {
|
||||
let flow_value = flow_data.value();
|
||||
if let Some(secs) = windmill_common::jobs::resolve_delete_after_secs(
|
||||
flow_value.delete_after_use,
|
||||
flow_value.delete_after_secs,
|
||||
) {
|
||||
delete_flow_run_data(db, &flow_job, secs).await?;
|
||||
}
|
||||
}
|
||||
true
|
||||
} else {
|
||||
tracing::debug!(id = %flow_job.id, "start handle flow");
|
||||
@@ -2216,6 +2211,8 @@ async fn delete_flow_run_data(db: &DB, flow_job: &MiniPulledJob, secs: i32) -> e
|
||||
.map_err(|e| {
|
||||
Error::internal_err(format!("error while cleaning up completed job args: {e:#}"))
|
||||
})?;
|
||||
// The flow's own completed row is written after this, so its result is kept: a
|
||||
// synchronous caller clears it once it has read it (`delete_flow_result_after_read`).
|
||||
sqlx::query!(
|
||||
"UPDATE v2_job_completed SET result = '{}'::jsonb WHERE id = ANY($1)",
|
||||
&all_ids,
|
||||
|
||||
Reference in New Issue
Block a user