From afcc66afc6a21aa9526ee1b6693bba1216bd5153 Mon Sep 17 00:00:00 2001 From: Waleed Latif Date: Wed, 21 May 2025 20:15:51 -0700 Subject: [PATCH] fix(execution): async executions when envvars are not set (#397) --- apps/sim/app/api/schedules/execute/route.ts | 8 ++++---- apps/sim/app/api/workflows/[id]/execute/route.ts | 11 ++++++----- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/apps/sim/app/api/schedules/execute/route.ts b/apps/sim/app/api/schedules/execute/route.ts index 945966b2dd..4dcc8949b6 100644 --- a/apps/sim/app/api/schedules/execute/route.ts +++ b/apps/sim/app/api/schedules/execute/route.ts @@ -155,6 +155,7 @@ export async function GET(req: NextRequest) { const mergedStates = mergeSubblockState(blocks) + // Retrieve environment variables for this user (if any). const [userEnv] = await db .select() .from(environment) @@ -162,13 +163,12 @@ export async function GET(req: NextRequest) { .limit(1) if (!userEnv) { - logger.error( - `[${requestId}] No environment variables found for user ${workflowRecord.userId}` + logger.debug( + `[${requestId}] No environment record found for user ${workflowRecord.userId}. Proceeding with empty variables.` ) - throw new Error('No environment variables found for this user') } - const variables = EnvVarsSchema.parse(userEnv.variables) + const variables = EnvVarsSchema.parse(userEnv?.variables ?? {}) const currentBlockStates = await Object.entries(mergedStates).reduce( async (accPromise, [id, block]) => { diff --git a/apps/sim/app/api/workflows/[id]/execute/route.ts b/apps/sim/app/api/workflows/[id]/execute/route.ts index 5ad00fc1a3..bad5621b20 100644 --- a/apps/sim/app/api/workflows/[id]/execute/route.ts +++ b/apps/sim/app/api/workflows/[id]/execute/route.ts @@ -102,7 +102,7 @@ async function executeWorkflow(workflow: any, requestId: string, input?: any) { // Use the same execution flow as in scheduled executions const mergedStates = mergeSubblockState(blocks) - // Retrieve environment variables for this user + // Fetch the user's environment variables (if any) const [userEnv] = await db .select() .from(environment) @@ -110,12 +110,13 @@ async function executeWorkflow(workflow: any, requestId: string, input?: any) { .limit(1) if (!userEnv) { - logger.error(`[${requestId}] No environment variables found for user: ${workflow.userId}`) - throw new Error('No environment variables found for this user') + logger.debug( + `[${requestId}] No environment record found for user ${workflow.userId}. Proceeding with empty variables.` + ) } - // Parse and validate environment variables - const variables = EnvVarsSchema.parse(userEnv.variables) + // Parse and validate environment variables. + const variables = EnvVarsSchema.parse(userEnv?.variables ?? {}) // Replace environment variables in the block states const currentBlockStates = await Object.entries(mergedStates).reduce(