Contract-driven embedding worker with pluggable providers, schema validation, and REST API.
420
A contract-driven queue worker for enrich-span jobs with pluggable embedding providers (cloudflare, openai, mock), schema consistency checks, and migration scaffolding.
contracts/jobs/enrich-span.yaml during startup.enrich-span payloads into deterministic embedding metadata.EMBEDDING_PROVIDER) using a provider port + adapter pattern.health, execute) for local tests and dry-runs.API/Queue input
-> worker.index.handle_job
-> worker.registry (contract + handler validation)
-> features.enrich_span.service
-> shared.di.providers (provider resolution)
-> infra.clients.<provider>
-> normalized result
Client / Queue Event
|
v
+-----------------------+
| api.execute(...) | (or direct worker.handle_job)
+-----------------------+
|
| opens span: "api.execute"
v
+-----------------------+
| worker.handle_job |
+-----------------------+
|
| load_config(env)
| - dimensions
| - embedding_provider
|
| opens span: "queue.consume"
v
+-----------------------+
| JOB_REGISTRY lookup |
+-----------------------+
|
| if job missing -> KeyError("Unknown job ...")
v
+-------------------------------+
| registry built at import-time |
+-------------------------------+
|
| load + validate contract
| validate handler signature
v
+-----------------------+
| job.handler(...) |
| enrich_span(...) |
+-----------------------+
|
| validate payload/dataclass
| validate dimensions > 0
| validate text non-empty
v
+-------------------------------+
| resolve_embedding_provider() |
+-------------------------------+
| | |
cloudflare openai mock
| | |
+------------+-------------+
|
v
+-------------------------------------------+
| provider.create_embedding(request, dims) |
+-------------------------------------------+
|
v
+-------------------------------------------+
| return normalized result |
+-------------------------------------------+
contracts/ — YAML job contract + OpenAPI + GraphQL + Proto schemas.src/worker/ — config, registry, queue entrypoint.src/features/enrich_span/ — feature-level orchestration.src/infra/clients/ — provider-specific adapters.src/shared/ — ports, DI, errors, tracing, utils.database/migrations/ — SQL migrations and rollback.tests/ — unit, integration, and contract consistency tests.scripts/ — local run/test/migrate/health scripts.| Variable | Default | Description |
|---|---|---|
CF_ACCOUNT_ID | local-account | Cloudflare account identifier for deployment context |
CF_QUEUE_NAME | span-enrichment | Queue name consumed by worker |
EMBEDDING_DIMENSIONS | 1536 | Output dimensions metadata |
WORKER_CONCURRENCY | 5 | Max concurrent worker slots |
WORKER_RATE_LIMIT_PER_SEC | 50 | Rate guardrail |
WORKER_BACKOFF_MS | 500 | Retry backoff metadata |
EMBEDDING_PROVIDER | cloudflare | Provider adapter key: cloudflare / openai / mock |
DEPLOYMENT_ENV | dev | Tracing attribute value |
cd python/queue-embedding-worker
bash scripts/test.sh
scripts/test.sh validates the contract first, then runs pytest.
cd python/queue-embedding-worker
bash scripts/health-check.sh
cd python/queue-embedding-worker
bash scripts/run.sh
Note: This project currently provides in-process API helpers (
src/api/index.py), not an HTTP server binary. The curl examples below are reference payload/response examples for future HTTP gateway wiring.
curl -s http://localhost:8080/health
{
"status": "ok",
"queue": "span-enrichment",
"concurrency": 5,
"rate_limit_per_sec": 50
}
curl -s -X POST http://localhost:8080/execute/enrich-span \
-H 'content-type: application/json' \
-d '{
"trace_id": "trace-1",
"span_id": "span-1",
"model": "text-embedding-3-small",
"text": "payment failed for order #123"
}'
{
"status": "processed",
"job": "enrich-span",
"result": {
"trace_id": "trace-1",
"span_id": "span-1",
"embedding_key": "emb_1234567890abcdef123456",
"dimensions": 1536,
"model": "text-embedding-3-small",
"provider": "cloudflare"
}
}
enrich-span contract is maintained in multiple forms and checked for consistency by tests:
contracts/jobs/enrich-span.yamlcontracts/api.yamlcontracts/graphql/schema.graphqlcontracts/proto/enrich_span.protoUse tests in tests/contracts/ to catch schema drift.
The worker is fully containerized and pushed to Docker Hub.
# Set your PAT
export DOCKER_PAT=your_pat
# Run deployment script (builds, tags, and pushes)
bash scripts/deploy_docker.sh
Use the development compose file for hot-reloading:
docker compose -f deploy/docker/docker-compose.dev.yaml up --build
The management API will be available at http://localhost:8002.
The worker now includes a FastAPI management layer (port 8000 in prod).
| Endpoint | Method | Description |
|---|---|---|
/health | GET | Check worker status and config. |
/execute | POST | Trigger a dry-run job execution. |
curl -X POST http://localhost:8002/execute \
-H "Content-Type: application/json" \
-d '{
"job_name": "enrich-span",
"message": {
"trace_id": "t1",
"span_id": "s1",
"model": "text-embedding-3-small",
"text": "hello world"
}
}'
database/migrations/README.md.database/schema.lock.Content type
Image
Digest
sha256:dff501cc5…
Size
167.3 MB
Last updated
4 months ago
docker pull chiefj/queue-embedding-worker