Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 35 additions & 24 deletions backend/windmill-api/src/flows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,38 @@ pub fn global_service() -> Router {
.route("/hub/get/:id", get(get_hub_flow_by_id))
}

fn validate_flow_value(flow_value: &serde_json::Value) -> Result<()> {
#[cfg(not(feature = "enterprise"))]
if flow_value
.get("ws_error_handler_muted")
.map(|val| val.as_bool().unwrap_or(false))
.is_some_and(|val| val)
{
return Err(Error::BadRequest(
"Muting the error handler for certain flow is only available in enterprise version"
.to_string(),
));
}

if let Some(modules) = flow_value.get("modules").and_then(|m| m.as_array()) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it only go through first level, there is likely a dfs function somewhere however I think it might be the wrong approach, you likely want to try to deserialize each module and fail at first module that can't be deserializing with giving the id + error from the deserializer. Much more generic. You can then just apply a validate function on a FlowModuleValue directly for anything post validation.

for module in modules {
if let Some(retry) = module.get("retry") {
if let Some(exponential) = retry.get("exponential") {
let seconds = exponential.get("seconds").and_then(|s| s.as_u64()).ok_or(
Error::BadRequest("Exponential backoff base (seconds) must be a valid positive integer".to_string()),
)?;
if seconds == 0 {
return Err(Error::BadRequest(
"Exponential backoff base (seconds) must be greater than 0. A base of 0 would cause immediate retries.".to_string(),
));
}
}
}
}
}
Ok(())
}

#[derive(Serialize, FromRow)]
pub struct SearchFlow {
path: String,
Expand Down Expand Up @@ -411,18 +443,8 @@ async fn create_flow(
));
}
}
#[cfg(not(feature = "enterprise"))]
if nf
.value
.get("ws_error_handler_muted")
.map(|val| val.as_bool().unwrap_or(false))
.is_some_and(|val| val)
{
return Err(Error::BadRequest(
"Muting the error handler for certain flow is only available in enterprise version"
.to_string(),
));
}

validate_flow_value(&nf.value)?;

// cron::Schedule::from_str(&ns.schedule).map_err(|e| error::Error::BadRequest(e.to_string()))?;
let authed = maybe_refresh_folders(&nf.path, &w_id, authed, &db).await;
Expand Down Expand Up @@ -744,18 +766,7 @@ async fn update_flow(
let flow_path = flow_path.to_path();
check_scopes(&authed, || format!("flows:write:{}", flow_path))?;

#[cfg(not(feature = "enterprise"))]
if nf
.value
.get("ws_error_handler_muted")
.map(|val| val.as_bool().unwrap_or(false))
.is_some_and(|val| val)
{
return Err(Error::BadRequest(
"Muting the error handler for certain flow is only available in enterprise version"
.to_string(),
));
}
validate_flow_value(&nf.value)?;

let authed = maybe_refresh_folders(&flow_path, &w_id, authed, &db).await;
let mut tx = user_db.clone().begin(&authed).await?;
Expand Down
10 changes: 10 additions & 0 deletions frontend/src/lib/components/FlowBuilder.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@
import { StepsInputArgs } from './flows/stepsInputArgs.svelte'
import { aiChatManager } from './copilot/chat/AIChatManager.svelte'
import type { GraphModuleState } from './graph'
import { validateRetryConfig } from '$lib/utils'
import {
setStepHistoryLoaderContext,
StepHistoryLoader,
Expand Down Expand Up @@ -427,6 +428,15 @@
loadingSave = true
try {
const flow = cleanInputs(flowStore.val)
if (flow.value?.modules) {
for (const module of flow.value.modules) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that only validate the first level, use dfs function to go through all the modules

const error = validateRetryConfig(module.retry)
if (error) {
throw new Error(error)
}
}
}
// console.log('flow', computeUnlockedSteps(flow)) // del
// loadingSave = false // del
// return
Expand Down
20 changes: 19 additions & 1 deletion frontend/src/lib/components/flows/content/FlowRetries.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import type { FlowEditorContext } from '../types'
import { getStepPropPicker } from '../previousResults'
import { NEVER_TESTED_THIS_FAR } from '../models'
import { validateRetryConfig } from '$lib/utils'

interface Props {
flowModuleRetry: Retry | undefined
Expand Down Expand Up @@ -59,6 +60,10 @@
: NEVER_TESTED_THIS_FAR
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good use of derived stores to show the validation error. Consider if similar validation is needed for the constant retry settings or adding comments to clarify why only the exponential section is validated.

)

let validationError = $derived.by(() => {
return validateRetryConfig(flowModuleRetry)
})

function setConstantRetries() {
flowModuleRetry = {
...flowModuleRetry,
Expand Down Expand Up @@ -247,7 +252,20 @@
<span class="text-xs text-tertiary">delay = multiplier * base ^ (number of attempt)</span>
<input bind:value={flowModuleRetry.exponential.multiplier} type="number" />
<div class="text-xs font-bold !mt-2">Base (in seconds)</div>
<input bind:value={flowModuleRetry.exponential.seconds} type="number" step="1" />
<input
bind:value={flowModuleRetry.exponential.seconds}
type="number"
step="1"
min="1"
class={validationError ? 'border-red-500' : ''}
/>
{#if validationError}
<span class="text-xs text-red-500">{validationError}</span>
{:else}
<span class="text-xs text-tertiary"
>Must be ≥ 1. A base of 0 would cause immediate retries.</span
>
{/if}
<div class="text-xs font-bold !mt-2">Randomization factor (percentage)</div>
<div class="flex w-full gap-4">
{#if !$enterpriseLicense}
Expand Down
12 changes: 11 additions & 1 deletion frontend/src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { deepEqual } from 'fast-equals'
import YAML from 'yaml'
import { type UserExt } from './stores'
import { sendUserToast } from './toast'
import type { Job, RunnableKind, Script, ScriptLang } from './gen'
import type { Job, RunnableKind, Script, ScriptLang, Retry } from './gen'
import type { EnumType, SchemaProperty } from './common'
import type { Schema } from './common'
export { sendUserToast }
Expand Down Expand Up @@ -1624,3 +1624,13 @@ export function createCache<Keys extends Record<string, any>, T, InitialKeys ext
export async function wait(ms: number) {
return new Promise((resolve) => setTimeout(() => resolve(undefined), ms))
}

export function validateRetryConfig(retry: Retry | undefined): string | null {
if (retry?.exponential?.seconds !== undefined) {
const seconds = retry.exponential.seconds
if (typeof seconds !== 'number' || !Number.isInteger(seconds) || seconds < 1) {
return 'Exponential backoff base (seconds) must be a valid positive integer ≥ 1'
}
}
return null
}
1 change: 1 addition & 0 deletions openflow.openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ components:
type: integer
seconds:
type: integer
minimum: 1
random_factor:
type: integer
minimum: 0
Expand Down