fix(async): fixed bug where async workflows were not running due to relative path reference

This commit is contained in:
Waleed Latif
2025-03-25 15:41:04 -07:00
parent 34b2e4389c
commit e6bf5cf521
2 changed files with 62 additions and 10 deletions
-2
View File
@@ -11,8 +11,6 @@ docs/node_modules
!.yarn/plugins
!.yarn/releases
!.yarn/versions
package.json
package-lock.json
# testing
/coverage
+62 -8
View File
@@ -18,16 +18,70 @@ export async function updateWorkflowRunCounts(workflowId: string, runs: number =
throw new Error(`Workflow ${workflowId} not found`)
}
const response = await fetch(`/api/workflows/${workflowId}/stats?runs=${runs}`, {
method: 'POST',
})
// Get the origin from the environment or use direct DB update as fallback
const origin =
process.env.NEXT_PUBLIC_APP_URL ||
(typeof window !== 'undefined' ? window.location.origin : '')
if (!response.ok) {
const error = await response.json()
throw new Error(error.error || 'Failed to update workflow stats')
if (origin) {
// Use absolute URL with origin
const response = await fetch(`${origin}/api/workflows/${workflowId}/stats?runs=${runs}`, {
method: 'POST',
})
if (!response.ok) {
const error = await response.json()
throw new Error(error.error || 'Failed to update workflow stats')
}
return response.json()
} else {
logger.warn(`No origin available, updating workflow stats directly via DB`)
// Update workflow directly through database
await db
.update(workflowTable)
.set({
runCount: workflow.runCount + runs,
lastRunAt: new Date(),
})
.where(eq(workflowTable.id, workflowId))
// Update user stats if needed
if (workflow.userId) {
const userStatsRecord = await db
.select()
.from(userStats)
.where(eq(userStats.userId, workflow.userId))
.limit(1)
if (userStatsRecord.length === 0) {
// Create new record
await db.insert(userStats).values({
id: crypto.randomUUID(),
userId: workflow.userId,
totalManualExecutions: runs,
totalApiCalls: 0,
totalWebhookTriggers: 0,
totalScheduledExecutions: 0,
totalTokensUsed: 0,
totalCost: '0.00',
lastActive: new Date(),
})
} else {
// Update existing record
await db
.update(userStats)
.set({
totalManualExecutions: userStatsRecord[0].totalManualExecutions + runs,
lastActive: new Date(),
})
.where(eq(userStats.userId, workflow.userId))
}
}
return { success: true, runsAdded: runs }
}
return response.json()
} catch (error) {
logger.error(`Error updating workflow run counts:`, error)
throw error