This document describes the implementation of background execution and asynchronous job processing within Sim Studio. The system leverages Trigger.dev for job orchestration, enabling long-running workflows to execute outside the constraints of HTTP request/response cycles. This architecture is essential for handling webhooks, scheduled tasks, and large-scale AI agent workflows that may exceed standard serverless or edge function timeouts.
Sim Studio uses a distributed background job architecture. When a workflow execution is triggered asynchronously (either via a specific header, a webhook, or a schedule), the system offloads the processing to a background worker.
useExecutionStream. apps/sim/app/workspace/[workspaceId]/w/[workflowId]/hooks/use-workflow-execution.ts:157-157task) and manage background execution lifecycles. apps/sim/background/workflow-execution.ts4workflow-execution, webhook-execution, schedule-execution). apps/sim/background/webhook-execution.ts136 apps/sim/background/schedule-execution.ts3webhookIdempotency. apps/sim/background/webhook-execution.ts149-164Sources: apps/sim/app/api/workflows/[id]/execute/route.ts:7-8, apps/sim/background/workflow-execution.ts4 apps/sim/background/webhook-execution.ts149-164 apps/sim/background/schedule-execution.ts3
The integration is centralized in the apps/sim/background directory. Each file defines a Trigger.dev task that wraps the core execution logic. Configuration for the Trigger.dev build process, including external packages, is managed in trigger.config.ts.
| Concept | Code Entity | File Path |
|---|---|---|
| Job Definition | task() | apps/sim/background/workflow-execution.ts4 |
| Webhook Task | executeWebhookJob | apps/sim/background/webhook-execution.ts136 |
| Schedule Task | TriggerScheduleExecution | apps/sim/background/schedule-execution.ts35 |
| Job Correlation | AsyncExecutionCorrelation | apps/sim/background/workflow-execution.ts7 |
The following diagram illustrates how an external trigger (Webhook) moves from an API request into a background task.
Sources: apps/sim/app/api/webhooks/trigger/[path]/route.ts:161-168, apps/sim/background/webhook-execution.ts183-208 apps/sim/lib/workflows/executor/execution-core.ts218-225 apps/sim/lib/webhooks/processor.ts38
Webhooks are the primary source of background jobs. The system includes a pipeline for validation, challenge handling, and fan-out to multiple workflows.
verifyProviderAuth. apps/sim/app/api/webhooks/trigger/[path]/route.ts:107-113findAllWebhooksForPath iterates through all matching records. apps/sim/app/api/webhooks/trigger/[path]/route.ts:85-100IdempotencyService.createWebhookIdempotencyKey to generate a hash of the provider, headers, and body. apps/sim/background/webhook-execution.ts149-154| Concept | Code Entity | File Path |
|---|---|---|
| Body Parsing | parseWebhookBody | apps/sim/lib/webhooks/processor.ts106-156 |
| Challenge Logic | handleProviderChallenges | apps/sim/lib/webhooks/processor.ts158 |
| Verification | verifyProviderWebhook | apps/sim/lib/webhooks/processor.ts36 |
| Attachment Handling | WebhookAttachmentProcessor | apps/sim/background/webhook-execution.ts14 |
Sources: apps/sim/lib/webhooks/processor.ts106-156 apps/sim/app/api/webhooks/trigger/[path]/route.ts:85-100, apps/sim/background/webhook-execution.ts149-154 apps/sim/lib/webhooks/processor.ts158-191
Schedules are managed via a cron-based polling mechanism that dispatches jobs to Trigger.dev.
workflowSchedule for records requiring execution. apps/sim/background/schedule-execution.ts1lastQueuedAt via applyScheduleUpdate to prevent duplicate dispatch. apps/sim/background/schedule-execution.ts63-77ScheduleExecutionPayload to the background worker. apps/sim/background/schedule-execution.ts47MAX_CONSECUTIVE_FAILURES (3), the schedule is disabled. apps/sim/background/schedule-execution.ts33Sources: apps/sim/background/schedule-execution.ts63-77 apps/sim/background/schedule-execution.ts79-96 apps/sim/background/schedule-execution.ts139-157 apps/sim/background/schedule-execution.ts33
Before any background job executes the workflow core, it must pass through preprocessExecution. This ensures that background jobs respect the same security and billing constraints as synchronous ones. apps/sim/background/webhook-execution.ts196-208
workflowDeploymentVersion for execution. apps/sim/background/webhook-execution.ts21actorUserId for credit consumption. apps/sim/background/webhook-execution.ts198createTimeoutAbortController. apps/sim/background/webhook-execution.ts8Sources: apps/sim/background/webhook-execution.ts196-208 apps/sim/background/webhook-execution.ts54-65 apps/sim/background/webhook-execution.ts8
To provide a unified view in the "Terminal" and "Logs" UI, background jobs use a correlation object. apps/sim/background/webhook-execution.ts32-48
The LoggingSession class uses this correlation to aggregate logs across different stages of the background job (preprocessing, execution, and finalization). apps/sim/background/webhook-execution.ts188-193
Sources: apps/sim/background/webhook-execution.ts32-48 apps/sim/background/schedule-execution.ts46-61 apps/sim/background/webhook-execution.ts188-193
Refresh this wiki