Skip to content

Commit 17aebe4

Browse files
committed
improvement(trigger): fold the deleted-scope log and result into one in-scope closure per task
1 parent 22c5789 commit 17aebe4

2 files changed

Lines changed: 15 additions & 40 deletions

File tree

‎apps/sim/background/webhook-execution.ts‎

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1289,19 +1289,17 @@ async function executeWebhookJobInternal(
12891289
executedAt: new Date().toISOString(),
12901290
provider: payload.provider,
12911291
})
1292-
/** A run whose workspace or workflow was deleted has no owner left to alert and no log to write. */
1293-
const logDeletedScope = (scope: ExecutionScope) =>
1292+
const completeWithDeletedScope = (scope: ExecutionScope) => {
12941293
logger.info(`[${requestId}] Webhook execution ended: its ${scope} was deleted`, {
12951294
workflowId: payload.workflowId,
12961295
executionId,
12971296
})
1298-
1299-
const deletedScope = await resolveDeletedExecutionScope(error, loggingSession)
1300-
if (deletedScope) {
1301-
logDeletedScope(deletedScope)
13021297
return failedRunResult()
13031298
}
13041299

1300+
const deletedScope = await resolveDeletedExecutionScope(error, loggingSession)
1301+
if (deletedScope) return completeWithDeletedScope(deletedScope)
1302+
13051303
// A failure inside workflow execution (block error, provider 4xx, missing required field, etc.)
13061304
// is finalized by core and already recorded in the execution logs. That is a user/workflow error,
13071305
// not a trigger.dev job fault — complete the run normally so we don't fire a false alert. Errors
@@ -1351,10 +1349,7 @@ async function executeWebhookJobInternal(
13511349
}
13521350

13531351
const scopeDeletedOnCompletion = loggingSession.getDeletedScope()
1354-
if (scopeDeletedOnCompletion) {
1355-
logDeletedScope(scopeDeletedOnCompletion)
1356-
return failedRunResult()
1357-
}
1352+
if (scopeDeletedOnCompletion) return completeWithDeletedScope(scopeDeletedOnCompletion)
13581353

13591354
if (retryableSetupCause) {
13601355
throw new RetryableSetupError(errorMessage, { cause: retryableSetupCause })

‎apps/sim/background/workflow-execution.ts‎

Lines changed: 10 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -130,24 +130,6 @@ function completeWithRecordedFailure(workflowId: string, executionId: string, er
130130
return buildRecordedJobFailureResult({ workflowId, executionId, error: failure.message })
131131
}
132132

133-
/**
134-
* Completes, without alerting, a job whose workspace or workflow was deleted: no owner is left to
135-
* act on an alert and no log is left to record the failure in.
136-
*/
137-
function completeWithDeletedScope(
138-
requestId: string,
139-
workflowId: string,
140-
executionId: string,
141-
scope: ExecutionScope,
142-
error: unknown
143-
) {
144-
logger.info(`[${requestId}] Workflow execution ended: its ${scope} was deleted`, {
145-
workflowId,
146-
executionId,
147-
})
148-
return completeWithRecordedFailure(workflowId, executionId, error)
149-
}
150-
151133
function requireLegacyWorkflowJobString(value: unknown, field: string): string {
152134
if (typeof value !== 'string' || !value.trim()) {
153135
throw new Error(`Legacy workflow job ${field} must be a non-empty string`)
@@ -358,11 +340,17 @@ export async function executeWorkflowJob(
358340
executionId,
359341
})
360342

343+
const completeWithDeletedScope = (scope: ExecutionScope) => {
344+
logger.info(`[${requestId}] Workflow execution ended: its ${scope} was deleted`, {
345+
workflowId,
346+
executionId,
347+
})
348+
return completeWithRecordedFailure(workflowId, executionId, error)
349+
}
350+
361351
// Checked before the timeout: a deleted scope leaves nobody to alert about a slow run.
362352
const deletedScope = await resolveDeletedExecutionScope(error, loggingSession)
363-
if (deletedScope) {
364-
return completeWithDeletedScope(requestId, workflowId, executionId, deletedScope, error)
365-
}
353+
if (deletedScope) return completeWithDeletedScope(deletedScope)
366354

367355
if (error instanceof ExecutionTimeoutError) throw error
368356

@@ -388,15 +376,7 @@ export async function executeWorkflowJob(
388376
})
389377

390378
const scopeDeletedOnCompletion = loggingSession.getDeletedScope()
391-
if (scopeDeletedOnCompletion) {
392-
return completeWithDeletedScope(
393-
requestId,
394-
workflowId,
395-
executionId,
396-
scopeDeletedOnCompletion,
397-
error
398-
)
399-
}
379+
if (scopeDeletedOnCompletion) return completeWithDeletedScope(scopeDeletedOnCompletion)
400380

401381
// Complete only when the rejection is durably logged; an unsaved one must still alert.
402382
if (

0 commit comments

Comments
 (0)