Diffinery

homeflare/celldpublic

celld/docs/services/workers.md
200 lines · 9.9 KBRaw History
1# Workers
2
3A Worker is a stateless request handler that uses the Cloudflare Workers API.
4An application uses a Worker for the code that answers an HTTP request, such as
5an API endpoint, a router, a webhook receiver, or a front end for a binding.
6celld runs the deployment in a V8 isolate on a node of the fleet, so a Worker
7keeps the persistent state of an application in a binding instead. Read the
8[Cloudflare Workers documentation](https://developers.cloudflare.com/workers/runtime-apis/)
9for the standard API behavior.
10
11## Example
12
13The [hello example](../../examples/hello) returns a text response from a
14`fetch` handler.
15
16<!-- celld-example: hello -->
17
18## The request path
19
20An operator puts an ingress proxy or a load balancer in front of the fleet.
21The ingress terminates TLS, because celld serves plain HTTP on the public
22listener of a node. The load balancer can send a request to any node in its
23rotation, and every node embeds V8 and holds the current deployment. The node
24that accepts the request therefore runs the handler itself, and it forwards
25only a binding call that a different node owns.
26
27Cloudflare instead routes a request through its own global network and starts
28the Worker in a nearby data center. A celld fleet has only the machines that
29you run, so you control the placement and the routing. Read
30[How Workers works](https://developers.cloudflare.com/workers/reference/how-workers-works/)
31for the Cloudflare model.
32
33![An ingress terminates TLS and sends a request to any celld node, which runs the Worker in a V8 isolate, while a binding call goes to the node that owns the cell or to the fleet bucket](workers-flow.svg)
34
35## The handler
36
37An HTTP request invokes the `fetch(request, env, ctx)` handler that the module
38exports as its default export. The handler receives the inbound `Request`, and
39it must return a `Response`. A module can export more handlers, such as
40`scheduled` for a Cron Trigger or `queue` for a Queues consumer. Each export of
41the main module must be a handler object or a class, as in workerd. A Worker
42whose main module exports a string or a number therefore fails to start. Read
43the
44[handlers documentation](https://developers.cloudflare.com/workers/runtime-apis/handlers/)
45for the complete list, and the
46[fetch handler documentation](https://developers.cloudflare.com/workers/runtime-apis/handlers/fetch/)
47for the parameters.
48
49The `ctx` argument controls the lifetime of the invocation. `ctx.waitUntil()`
50keeps the isolate alive for a promise after the response goes to the client.
51`ctx.passThroughOnException()` has no effect on celld, because celld has no CDN
52that can answer in place of a failed Worker.
53
54## State and bindings
55
56The `env` argument contains the bindings that the Wrangler configuration
57declares. A binding gives the Worker a permission and an API together, so the
58code needs no credential for the target service. Read the
59[bindings documentation](https://developers.cloudflare.com/workers/runtime-apis/bindings/)
60for the configuration syntax.
61
62A binding call leaves the isolate, and the target decides where the call goes.
63A KV namespace, a D1 database, a queue, and a Durable Object are each a cell,
64and one node owns a cell at a time, so celld routes the call to that owner. An
65R2 binding has no cell, and it reads and writes the fleet bucket directly.
66
67celld runs many requests in one isolate, so a value in the module scope can
68still be present for a later request. That value is not a safe place for
69application state. The load balancer can send the next request to a different
70node, and celld retires an isolate when the request load falls or when a new
71deployment supersedes it, so the value can also disappear. A Worker must
72therefore not depend on mutable in-memory state, and it must write the state
73that it must keep into a binding. The Cloudflare
74[best practices](https://developers.cloudflare.com/workers/best-practices/workers-best-practices/)
75give the same rule.
76
77## Differences from Cloudflare
78
79- celld does not manage a custom domain or terminate TLS. Terminate TLS in the
80 ingress proxy.
81- celld does not supply a Workers AI binding. Call an AI provider from the
82 application. The process rejects `CELLD_AI_BINDING` and `CELLD_AI_URL`, and a
83 deployment rejects an `ai` declaration.
84
85The [Cloudflare compatibility](../cloudflare-compat.md#services) page lists the
86runtime APIs and the unsupported services.
87
88## Python Workers
89
90A Worker whose `main` is a `.py` file runs on celld with the
91[Cloudflare Python Workers](https://developers.cloudflare.com/workers/languages/python/)
92API: a `Default` class that extends `WorkerEntrypoint`, the `Response`
93and `fetch` of the `workers` package, and bindings on `self.env`. The same
94project runs on Cloudflare without a change.
95
96The [Python example](../../examples/python) uses the SDK and a vendored
97package to handle a request.
98
99<!-- celld-example: python -->
100
101celld supports a bounded part of the Python Workers platform. This page
102lists what that part is.
103
104### Build a project
105
106Run `uv run pywrangler sync` to vendor the project's packages with
107Cloudflare's `pywrangler`. Then run `celld dev .` to start the project.
108
109`pywrangler sync` writes the packages to `python_modules/`, including the
110Workers SDK (`workers-runtime-sdk`). The build rejects an incomplete Workers
111SDK. `celld deploy` embeds the files below the directory of `main` that
112Wrangler uploads with its default module rules: `.py`, `.txt`, `.html`,
113`.sql`, `.bin`, and `.wasm` files. It also embeds the files of
114`python_modules/`, and puts all of them in one bundle with the Pyodide
115runtime. The Worker can open each embedded file under `/session/metadata`,
116as on Cloudflare. Nothing is downloaded when the Worker runs.
117
118The first build downloads the Pyodide runtime files from the Pyodide CDN
119and checks the size and the SHA-256 of each one. Later builds reuse them
120from `$XDG_CACHE_HOME/celld/pyodide-0.28.3`, or from
121`~/.cache/celld/pyodide-0.28.3` when `XDG_CACHE_HOME` is not set. Set
122`CELLD_PYTHON_RUNTIME_DIR` to use another directory; a directory that
123already holds the files lets a build run offline. A download fails when the
124CDN sends no data for 60 seconds, so a stalled network does not stop the
125build without an error. The build needs `esbuild` on `PATH`, as a JavaScript build
126does, or `CELLD_ESBUILD` set to its path.
127
128A Python deployment requires the `python-workers-v1` feature.
129`celld deploy` does not examine the nodes of the fleet, so the
130deployment succeeds even when a node does not have the feature. A running node that
131predates the feature logs an error and continues to serve its current
132deployment, so the fleet then serves two versions. A node that predates
133the feature and starts while a Python deployment is current exits with
134an error. Therefore, upgrade every node before the first Python
135deployment, and do not replace this celld version with an earlier one
136while a Python deployment is current.
137
138### Runtime version
139
140celld runs Pyodide 0.28.3 with CPython 3.13.2 and the `pyodide_2025_0`
141wheel ABI. This is the runtime line that Cloudflare selects with
142`python_workers_20250116` (Cloudflare runs Pyodide 0.28.2 with its own
143patches). The configuration must select that line and the vendored SDK:
144
145- `compatibility_flags` includes `python_workers`.
146- `compatibility_date` has the form `YYYY-MM-DD`.
147- `compatibility_date` is 2026-04-21 or later, which turns on
148 `enable_python_external_sdk`, `python_no_global_handlers`, and the
149 `python_modules` search path. With an earlier date, a flag must turn on
150 each behavior that the date does not turn on:
151 `python_workers_force_new_vendor_path` before 2025-08-11,
152 `python_no_global_handlers` before 2025-08-14, `python_workers_20250116`
153 before 2025-09-29, and `enable_python_external_sdk` before 2026-04-21.
154- `compatibility_date` is before 2026-09-08, or the flags include
155 `no_python_workers_314`. From 2026-09-08 Cloudflare runs Pyodide 314
156 (Python 3.14), which needs wheels for another ABI.
157
158celld refuses other settings at deploy time. `python_process_pth_files`
159follows its compatibility date (2026-05-26).
160
161### Supported
162
163| API | Notes |
164| --- | --- |
165| `Default(WorkerEntrypoint).fetch` | One instance for each request, as on Cloudflare |
166| `workers.Response`, `Response.json`, request body, text and headers | Compared with workerd in the tests |
167| `workers.fetch` | Outbound HTTP |
168| `self.env` bindings | Tested with KV. Other bindings are the objects a JavaScript Worker gets, untested from Python |
169| `self.ctx.waitUntil` | Python awaitables are kept alive until they settle |
170| Pure-Python packages in `python_modules/` | Tested with `beautifulsoup4` |
171| `from js import ...` | The JavaScript globals of the isolate |
172
173### Not supported
174
175- Durable Objects, Workflows, Cron Triggers and Queue consumers written
176 in Python. `celld deploy` refuses a Python project that declares them.
177- Named entrypoint classes and RPC to Python methods.
178- Packages with compiled extensions (`.so` files), and the standard
179 library modules that Pyodide ships as separate packages: `ssl`,
180 `sqlite3`, `lzma`, and the OpenSSL-backed `hashlib` algorithms.
181- `pyodide.ffi.run_sync`, and `workers.import_from_javascript` for
182 modules other than `cloudflare:workers` and `cloudflare:sockets`. celld
183 hides WebAssembly stack switching (JSPI) from the runtime, because under
184 JSPI each async entry leaks C stack until the interpreter stops.
185- `process.exit()` throws an error in a Python Worker.
186- Memory snapshots and the import patches of the `_cloudflare` package.
187 Package patches that need them, such as synchronous FastAPI handlers,
188 do not apply.
189
190### Failure behavior
191
192The interpreter starts on the first request that an isolate handles, and
193concurrent first requests share that start. If the main module raises an
194error, every request to that isolate fails with the same traceback.
195
196A fatal interpreter error, such as a C stack overflow from deep
197recursion, fails the request in flight and the other requests that the
198isolate is running. celld then replaces the isolate, as Cloudflare does.
199The next request goes to another isolate, so it can wait for a new
200interpreter to start.