Skip to content

fix(background-tasks-server): make background tasks run on the server flavour#5420

Closed
adrians5j wants to merge 1 commit into
nextfrom
adrian/self-hosted-bg-task-worker-path
Closed

fix(background-tasks-server): make background tasks run on the server flavour#5420
adrians5j wants to merge 1 commit into
nextfrom
adrian/self-hosted-bg-task-worker-path

Conversation

@adrians5j

@adrians5j adrians5j commented Jul 16, 2026

Copy link
Copy Markdown
Member

Every background task on the self-hosted (server) flavour stayed stuck pending — the worker never completed. Two root causes, both confirmed via runtime logging:

1. Worker asset path

WorkerService spawned the worker via new Worker(new URL("../worker/workerEntry.js", import.meta.url)). The app bundler rewrites that new URL(...) to a publicPath asset (/static/assets/workerEntry.<hash>.js), which new Worker() then can't load as a filesystem path → Cannot find module '/static/assets/...'.

import.meta.url resolves to this module inside its own dist (the bundler preserves it and doesn't inline the package), and the compiled worker + deps live beside it under dist/worker/ (relative + node-builtin imports only). So resolve it with path.join off import.meta.url — an expression the bundler leaves untouched — pointing at the real dist worker.

2. Task-event validation (AWS leak in the shared runner)

Past the worker, the shared TaskRunner's TaskEventValidation requires AWS Step Functions fieldsendpoint, executionName, stateMachineId:

VALIDATION_FAILED_INVALID_FIELDS — endpoint/executionName/stateMachineId: expected string, received undefined

AWS's Lambda passes them from the SFN execution context; the server worker's event didn't. Supply them from the server transport: executionName = the task id (a stable run id the runner uses for log entries); endpoint/stateMachineId are validate-only off-AWS.

Also surfaces the worker's error (previously discarded) so a failed task logs instead of silently staying pending.

Verified

End-to-end on the server flavour: image upload → worker spawns → POSTs /background-task → validation passes → task runs to completion, AI SDK invoked. AWS flavour unaffected (uses StepFunctionService, not this worker).

Note: AI image enrichment separately fails in local dev because it sends the external AI a localhost image URL (https://api2.localhost/...) it can't fetch — a deployment/ai-powerups concern, not a bg-tasks issue.

🤖 Generated with Claude Code

@adrians5j
adrians5j force-pushed the adrian/self-hosted-bg-task-worker-path branch 2 times, most recently from aa48b5b to 6502841 Compare July 16, 2026 09:40
… flavour

Two issues left every server-flavour background task stuck "pending" (worker never completed):

1. Worker asset path. The worker was spawned via
   `new Worker(new URL("../worker/workerEntry.js", import.meta.url))`. The app bundler rewrites that
   `new URL` to a publicPath-based asset ("/static/assets/workerEntry.<hash>.js") that `new Worker()`
   can't load as a filesystem path. `import.meta.url` resolves to this module inside its own dist (the
   bundler preserves it, doesn't inline the package), and the compiled worker + deps live beside it
   under dist/worker with only relative/node-builtin imports — so resolve it with `path.join` off
   `import.meta.url` (which the bundler leaves untouched), pointing at the real dist worker.

2. Task-event validation. The shared TaskRunner's TaskEventValidation requires AWS Step Functions
   fields (endpoint / executionName / stateMachineId). AWS's Lambda passes them from the SFN execution
   context; the server worker's event didn't have them, so every task failed validation. Supply them
   from the worker: executionName = the task id (a stable run id the runner uses for log entries);
   endpoint / stateMachineId are validate-only off-AWS.

Also surface the worker's error (previously discarded) so a failed task logs instead of silently
staying "pending". Verified end-to-end on the server flavour: image upload → worker spawns, POSTs the
background-task route, and the tasks run to completion. AWS flavour unaffected (uses StepFunctionService).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@adrians5j
adrians5j force-pushed the adrian/self-hosted-bg-task-worker-path branch from 6502841 to f557f54 Compare July 16, 2026 10:29
@adrians5j adrians5j changed the title fix(background-tasks-server): resolve worker asset path under the app publicPath fix(background-tasks-server): make background tasks run on the server flavour Jul 16, 2026
@adrians5j

Copy link
Copy Markdown
Member Author

Folded into #5414 (collapsed the server-flavour follow-ups into a single PR).

@adrians5j adrians5j closed this Jul 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant