Skip to content

Fix #523: point API_BASE_URL at workers.dev to allow self-fetch - #524

Merged
neuromechanist merged 1 commit into
devfrom
fix/issue-523-worker-self-fetch-base-url
May 16, 2026
Merged

neuromechanist merged 1 commit into
devfrom
fix/issue-523-worker-self-fetch-base-url

Conversation

@neuromechanist

Copy link
Copy Markdown
Contributor

Fixes #523.

Root cause

runEnrichmentForDataset in backend/src/services/dataset-reindex.ts does a Worker self-fetch:

```ts
const res = await fetch(`${env.API_BASE_URL.replace(/\/$/, "")}/webhooks/llm-enrich`, { method: "POST", ... });
```

On the SCCN prod config, `API_BASE_URL = "https://api.nemar.org"\` — the Worker's own custom-domain hostname. Cloudflare Workers reject fetch() calls to the Worker's own custom domain (returns instant 522, NOT a true edge timeout). The `*.workers.dev` URL for the same Worker is allowed.

Diagnostic evidence (curl against prod admin reindex with skip_sync=true):
```
{"dataset_id":"on005262","enrichment":{"status":"failed","ref":"main","error":"HTTP 522: error code: 522"},"sync":{"status":"skipped"}}
HTTP=200 time=0.295683s
```

The 0.3s response time is the smoking gun — a real Cloudflare 522 (origin connection timeout) takes the full 15s connect_timeout.

Fix

One-line config change: `API_BASE_URL` in `backend/wrangler-sccn.toml` from `https://api.nemar.org\` to `https://nemar-api.sccn-org.workers.dev\`. Both URLs route to the same Worker; only the workers.dev path is whitelisted for self-fetch.

The legacy `backend/wrangler.toml` is unchanged (it deploys to api.osc.earth/nemar in maintenance mode and doesn't need self-fetch).

Verification plan

After deploy:

Follow-up

The proper long-term fix is a service binding ( `[[services]] binding = SELF` + `env.SELF.fetch()` instead of HTTP round-trip). Tracked in #523 — service binding eliminates the HTTP round-trip entirely (lower latency + lower subrequest cost). This PR is the tactical unblock.

…523)

Cloudflare Workers reject fetch() calls to the Worker's own custom-domain hostname (the API_BASE_URL self-fetch in runEnrichmentForDataset surfaced as instant HTTP 522 in 0.3s, not a true edge timeout). Switching API_BASE_URL on the SCCN prod config from https://api.nemar.org to the workers.dev fallback https://nemar-api.sccn-org.workers.dev fixes the self-fetch path without code changes. Both URLs hit the same Worker code.

Root-caused while running 'nemar admin reindex --all' post-v0.8.18 deploy: 51/51 on* datasets reported enrich:failed with the same HTTP 522 0.3s pattern. Service-binding refactor is the proper long-term fix — tracked separately in #523.
@github-actions

Copy link
Copy Markdown

📦 Package Preview

Your package is ready to test!

bun install -g nemar-cli@PR524

Version: 0.8.18-PR524.ccf8894
Commit: ccf8894


🤖 Updated on each push.

@neuromechanist
neuromechanist merged commit ac60ca0 into dev May 16, 2026
9 checks passed
@neuromechanist
neuromechanist deleted the fix/issue-523-worker-self-fetch-base-url branch May 16, 2026 08:01

This branch was previously deployed

1 inactive deployment
npm — ccf88941 Deployed May 16, 2026 by neuromechanist via publish #947
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