substantial 0.2.0b2__tar.gz → 0.3.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- substantial-0.2.0b2/README.md → substantial-0.3.0/PKG-INFO +322 -28
- substantial-0.2.0b2/PKG-INFO → substantial-0.3.0/README.md +304 -45
- {substantial-0.2.0b2 → substantial-0.3.0}/pyproject.toml +14 -8
- {substantial-0.2.0b2 → substantial-0.3.0}/python/substantial/__init__.py +21 -4
- substantial-0.3.0/python/substantial/_proto.py +77 -0
- substantial-0.3.0/python/substantial/boundary/v1/agent_pb2.py +52 -0
- {substantial-0.2.0b2 → substantial-0.3.0}/python/substantial/boundary/v1/agent_pb2.pyi +123 -109
- substantial-0.3.0/python/substantial/boundary/v1/boundary_pb2.py +45 -0
- {substantial-0.2.0b2 → substantial-0.3.0}/python/substantial/boundary/v1/boundary_pb2.pyi +68 -78
- substantial-0.3.0/python/substantial/boundary/v1/plan_pb2.py +103 -0
- substantial-0.3.0/python/substantial/boundary/v1/plan_pb2.pyi +418 -0
- substantial-0.3.0/python/substantial/boundary/v1/run_pb2.py +99 -0
- {substantial-0.2.0b2 → substantial-0.3.0}/python/substantial/boundary/v1/run_pb2.pyi +326 -289
- {substantial-0.2.0b2 → substantial-0.3.0}/python/substantial/child.py +73 -16
- {substantial-0.2.0b2 → substantial-0.3.0}/python/substantial/client.py +297 -66
- {substantial-0.2.0b2 → substantial-0.3.0}/python/substantial/clock.py +6 -0
- {substantial-0.2.0b2 → substantial-0.3.0}/python/substantial/context.py +157 -11
- {substantial-0.2.0b2 → substantial-0.3.0}/python/substantial/core.py +18 -0
- substantial-0.3.0/python/substantial/cost.py +208 -0
- {substantial-0.2.0b2 → substantial-0.3.0}/python/substantial/durations.py +8 -1
- {substantial-0.2.0b2 → substantial-0.3.0}/python/substantial/errors.py +84 -10
- {substantial-0.2.0b2 → substantial-0.3.0}/python/substantial/executor.py +1 -0
- {substantial-0.2.0b2 → substantial-0.3.0}/python/substantial/iterate.py +5 -3
- substantial-0.3.0/python/substantial/layout/v1/layout_pb2.py +103 -0
- {substantial-0.2.0b2 → substantial-0.3.0}/python/substantial/layout/v1/layout_pb2.pyi +391 -271
- {substantial-0.2.0b2 → substantial-0.3.0}/python/substantial/log.py +1 -0
- {substantial-0.2.0b2 → substantial-0.3.0}/python/substantial/machines.py +34 -4
- {substantial-0.2.0b2 → substantial-0.3.0}/python/substantial/messages.py +11 -3
- {substantial-0.2.0b2 → substantial-0.3.0}/python/substantial/names.py +34 -0
- substantial-0.3.0/python/substantial/observe.py +166 -0
- {substantial-0.2.0b2 → substantial-0.3.0}/python/substantial/plan.py +29 -7
- {substantial-0.2.0b2 → substantial-0.3.0}/python/substantial/replay.py +123 -18
- {substantial-0.2.0b2 → substantial-0.3.0}/python/substantial/saga.py +8 -0
- substantial-0.3.0/python/substantial/schedule.py +397 -0
- {substantial-0.2.0b2 → substantial-0.3.0}/python/substantial/scheduler.py +61 -10
- {substantial-0.2.0b2 → substantial-0.3.0}/python/substantial/settings.py +20 -23
- {substantial-0.2.0b2 → substantial-0.3.0}/python/substantial/step.py +54 -12
- {substantial-0.2.0b2 → substantial-0.3.0}/python/substantial/stores/__init__.py +17 -3
- substantial-0.3.0/python/substantial/stores/attempts.py +44 -0
- {substantial-0.2.0b2 → substantial-0.3.0}/python/substantial/stores/base.py +3 -0
- {substantial-0.2.0b2 → substantial-0.3.0}/python/substantial/stores/credentials.py +6 -0
- {substantial-0.2.0b2 → substantial-0.3.0}/python/substantial/stores/fs.py +4 -0
- {substantial-0.2.0b2 → substantial-0.3.0}/python/substantial/stores/memory.py +2 -0
- {substantial-0.2.0b2 → substantial-0.3.0}/python/substantial/stores/redis.py +2 -0
- {substantial-0.2.0b2 → substantial-0.3.0}/python/substantial/stores/redis_connect.py +10 -0
- {substantial-0.2.0b2 → substantial-0.3.0}/python/substantial/stores/redis_store.py +7 -0
- {substantial-0.2.0b2 → substantial-0.3.0}/python/substantial/stores/retry.py +9 -0
- {substantial-0.2.0b2 → substantial-0.3.0}/python/substantial/stores/s3.py +4 -0
- substantial-0.3.0/python/substantial/stores/s3_store.py +368 -0
- {substantial-0.2.0b2 → substantial-0.3.0}/python/substantial/testing/_env.py +53 -15
- {substantial-0.2.0b2 → substantial-0.3.0}/python/substantial/testing/_store.py +2 -0
- {substantial-0.2.0b2 → substantial-0.3.0}/python/substantial/testing/_time.py +31 -10
- {substantial-0.2.0b2 → substantial-0.3.0}/python/substantial/testing/_workers.py +44 -2
- {substantial-0.2.0b2 → substantial-0.3.0}/python/substantial/testing/_world.py +81 -25
- {substantial-0.2.0b2 → substantial-0.3.0}/python/substantial/worker.py +83 -21
- {substantial-0.2.0b2 → substantial-0.3.0}/python/substantial/workflow.py +29 -18
- {substantial-0.2.0b2 → substantial-0.3.0}/redis/create.lua +4 -0
- {substantial-0.2.0b2 → substantial-0.3.0}/redis/delete.lua +4 -0
- substantial-0.3.0/wasm/SHA256SUMS +1 -0
- substantial-0.3.0/wasm/substantial.wasm +0 -0
- substantial-0.2.0b2/python/substantial/_proto.py +0 -28
- substantial-0.2.0b2/python/substantial/boundary/v1/agent_pb2.py +0 -61
- substantial-0.2.0b2/python/substantial/boundary/v1/boundary_pb2.py +0 -56
- substantial-0.2.0b2/python/substantial/boundary/v1/plan_pb2.py +0 -86
- substantial-0.2.0b2/python/substantial/boundary/v1/plan_pb2.pyi +0 -294
- substantial-0.2.0b2/python/substantial/boundary/v1/run_pb2.py +0 -104
- substantial-0.2.0b2/python/substantial/layout/v1/layout_pb2.py +0 -94
- substantial-0.2.0b2/python/substantial/stores/s3_store.py +0 -317
- substantial-0.2.0b2/wasm/SHA256SUMS +0 -1
- substantial-0.2.0b2/wasm/substantial.wasm +0 -0
- {substantial-0.2.0b2 → substantial-0.3.0}/.gitignore +0 -0
- {substantial-0.2.0b2 → substantial-0.3.0}/LICENSE +0 -0
- {substantial-0.2.0b2 → substantial-0.3.0}/python/substantial/py.typed +0 -0
- {substantial-0.2.0b2 → substantial-0.3.0}/python/substantial/stores/redis_notify.py +0 -0
- {substantial-0.2.0b2 → substantial-0.3.0}/python/substantial/testing/__init__.py +0 -0
|
@@ -1,3 +1,21 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: substantial
|
|
3
|
+
Version: 0.3.0
|
|
4
|
+
Summary: Brokerless durable execution: the Python host of the Substantial core
|
|
5
|
+
Project-URL: Repository, https://github.com/zifeo/substantial
|
|
6
|
+
License-Expression: MPL-2.0
|
|
7
|
+
License-File: LICENSE
|
|
8
|
+
Requires-Python: >=3.12
|
|
9
|
+
Requires-Dist: protobuf>=4.21.1
|
|
10
|
+
Requires-Dist: rfc8785>=0.1.3
|
|
11
|
+
Requires-Dist: wasmtime>=7.0.0
|
|
12
|
+
Provides-Extra: redis
|
|
13
|
+
Requires-Dist: redis>=5.3.0; extra == 'redis'
|
|
14
|
+
Provides-Extra: s3
|
|
15
|
+
Requires-Dist: aiohttp>=3.9.0; extra == 's3'
|
|
16
|
+
Requires-Dist: botocore>=1.29.13; extra == 's3'
|
|
17
|
+
Description-Content-Type: text/markdown
|
|
18
|
+
|
|
1
19
|
# Substantial
|
|
2
20
|
|
|
3
21
|
Brokerless durable execution for Rust, Python and TypeScript.
|
|
@@ -36,7 +54,7 @@ async def checkout(c: s.Context, order: str, amount: int) -> dict:
|
|
|
36
54
|
|
|
37
55
|
async def main() -> None:
|
|
38
56
|
async with (
|
|
39
|
-
s.Substantial.create("memory://shop"
|
|
57
|
+
s.Substantial.create("memory://shop") as sub,
|
|
40
58
|
sub.worker([checkout], preset="script"),
|
|
41
59
|
):
|
|
42
60
|
run = await sub.start(checkout, {"order": "a-42", "amount": 1999}, id="order-a-42")
|
|
@@ -102,7 +120,7 @@ async fn checkout(c: s::Context, Order { order, amount }: Order) -> s::Result<Re
|
|
|
102
120
|
|
|
103
121
|
#[tokio::main]
|
|
104
122
|
async fn main() -> anyhow::Result<()> {
|
|
105
|
-
let sub = s::Substantial::create("memory://shop").
|
|
123
|
+
let sub = s::Substantial::create("memory://shop").await?;
|
|
106
124
|
let worker = sub.worker(checkout).preset(s::Preset::Script).start()?;
|
|
107
125
|
let order = Order { order: "a-42".into(), amount: 1999 };
|
|
108
126
|
let run = sub.start(checkout, order).id("order-a-42").await?;
|
|
@@ -151,7 +169,7 @@ const checkout = s.workflow("checkout", async (c, { order, amount }: { order: st
|
|
|
151
169
|
return { payment, tracking };
|
|
152
170
|
});
|
|
153
171
|
|
|
154
|
-
await using sub = await s.Substantial.create("memory://shop"
|
|
172
|
+
await using sub = await s.Substantial.create("memory://shop");
|
|
155
173
|
await using _worker = sub.worker([checkout], { preset: "script" });
|
|
156
174
|
const run = await sub.start(checkout, { order: "a-42", amount: 1999 }, { id: "order-a-42" });
|
|
157
175
|
await run.send("address", "1 rue du Lac, Lausanne");
|
|
@@ -170,32 +188,34 @@ Run it with `node checkout.ts` or `deno run -A checkout.ts`. Steps and workflows
|
|
|
170
188
|
|
|
171
189
|
## Install
|
|
172
190
|
|
|
173
|
-
|
|
191
|
+
Substantial runs on Linux, macOS and WSL.
|
|
192
|
+
|
|
193
|
+
Python 3.12 or later:
|
|
174
194
|
|
|
175
195
|
```sh
|
|
176
|
-
uv add substantial
|
|
177
|
-
uv add "substantial[s3]
|
|
178
|
-
uv add "substantial[redis]
|
|
196
|
+
uv add substantial # or: pip install substantial
|
|
197
|
+
uv add "substantial[s3]" # with S3 stores, or: pip install "substantial[s3]"
|
|
198
|
+
uv add "substantial[redis]" # with Redis stores, or: pip install "substantial[redis]"
|
|
179
199
|
```
|
|
180
200
|
|
|
181
|
-
The
|
|
201
|
+
The `s3` extra adds aiohttp, which sends the requests of `s3://` stores, and botocore, which signs them; the `redis` extra adds redis-py, which Redis stores need.
|
|
182
202
|
|
|
183
203
|
Rust 1.97 or later, with what the example uses:
|
|
184
204
|
|
|
185
205
|
```sh
|
|
186
|
-
cargo add substantial
|
|
206
|
+
cargo add substantial
|
|
187
207
|
cargo add tokio --features macros,rt-multi-thread
|
|
188
208
|
cargo add serde --features derive
|
|
189
209
|
cargo add anyhow
|
|
190
210
|
```
|
|
191
211
|
|
|
192
|
-
Redis stores need the `redis` feature: `cargo add substantial
|
|
212
|
+
Redis stores need the `redis` feature: `cargo add substantial --features redis`, and the [Wasm runner](#wasm-workflows) the `wasm-runner` feature.
|
|
193
213
|
|
|
194
214
|
TypeScript, on Node 24 or later, or Deno, from [JSR](https://jsr.io/@substantial/substantial):
|
|
195
215
|
|
|
196
216
|
```sh
|
|
197
|
-
npx jsr add @substantial/substantial
|
|
198
|
-
deno add jsr:@substantial/substantial
|
|
217
|
+
npx jsr add @substantial/substantial
|
|
218
|
+
deno add jsr:@substantial/substantial
|
|
199
219
|
```
|
|
200
220
|
|
|
201
221
|
`s3://` stores also need the AWS SDK: `npm install @aws-sdk/client-s3`, or `deno add npm:@aws-sdk/client-s3`. Redis stores need `@redis/client`: `npm install @redis/client`, or `deno add npm:@redis/client`.
|
|
@@ -277,6 +297,65 @@ const approved = await c.waitUntil(() => approvers.length >= 2, { timeout: s.day
|
|
|
277
297
|
|
|
278
298
|
A sleep's end is fixed when it first runs, and a replay never sleeps again. `wait_until` checks its predicate after each message, child end and timer. More in [I-8].
|
|
279
299
|
|
|
300
|
+
### Polling and schedules
|
|
301
|
+
|
|
302
|
+
```python
|
|
303
|
+
url = await c.poll("export_url", provider.export_url, job, every=s.minutes(1), timeout=s.hours(2)) # None after 2 hours
|
|
304
|
+
|
|
305
|
+
|
|
306
|
+
@s.workflow
|
|
307
|
+
async def nightly(c: s.Context) -> None:
|
|
308
|
+
async for at in c.schedule(): # 03:00 in Zurich, missed nights backfilled
|
|
309
|
+
await c.start(report, {"day": f"{at:%Y-%m-%d}"}, id=f"report-{at:%Y-%m-%d}")
|
|
310
|
+
|
|
311
|
+
await sub.schedule(nightly, "nightly", s.Cron("0 3 * * *", "Europe/Zurich"), revision=1, backfill=7)
|
|
312
|
+
```
|
|
313
|
+
|
|
314
|
+
<details>
|
|
315
|
+
<summary>In Rust and TypeScript</summary>
|
|
316
|
+
|
|
317
|
+
```rust
|
|
318
|
+
let url: Option<String> = c
|
|
319
|
+
.poll("export_url", move || { let job = job.clone(); async move { provider::export_url(&job).await } })
|
|
320
|
+
.every(s::minutes(1))
|
|
321
|
+
.timeout(s::hours(2))
|
|
322
|
+
.await?;
|
|
323
|
+
|
|
324
|
+
#[s::workflow]
|
|
325
|
+
async fn nightly(c: s::Context) -> s::Result<()> {
|
|
326
|
+
c.schedule(|c, at| async move {
|
|
327
|
+
let day = at.format("%Y-%m-%d").to_string();
|
|
328
|
+
c.start(&report, Report { day: day.clone() }).id(&format!("report-{day}")).await?;
|
|
329
|
+
Ok(ControlFlow::Continue(()))
|
|
330
|
+
})
|
|
331
|
+
.await
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
sub.schedule(nightly, "nightly", Some(s::Cron::new("0 3 * * *", "Europe/Zurich")?), 1)
|
|
335
|
+
.backfill(7)
|
|
336
|
+
.await?;
|
|
337
|
+
```
|
|
338
|
+
|
|
339
|
+
```ts
|
|
340
|
+
const url = await c.poll("export_url", () => provider.exportUrl(job), {
|
|
341
|
+
every: s.minutes(1),
|
|
342
|
+
timeout: s.hours(2),
|
|
343
|
+
});
|
|
344
|
+
|
|
345
|
+
export const nightly = s.workflow("nightly", async (c) => {
|
|
346
|
+
for await (const at of c.schedule()) {
|
|
347
|
+
const day = at.toISOString().slice(0, 10);
|
|
348
|
+
await c.start(report, { day }, { id: `report-${day}` });
|
|
349
|
+
}
|
|
350
|
+
});
|
|
351
|
+
|
|
352
|
+
await sub.schedule(nightly, "nightly", new s.Cron("0 3 * * *", "Europe/Zurich"), { revision: 1, backfill: 7 });
|
|
353
|
+
```
|
|
354
|
+
|
|
355
|
+
</details>
|
|
356
|
+
|
|
357
|
+
`poll` tries a step until it returns a value, sleeping between tries, each try recorded. A schedule is a run looping on `c.schedule()`, configured by `sub.schedule`: every pod can call it at startup, a config applies only if its revision is higher, fires missed while no worker ran come oldest first, up to `backfill` of them (5 unless given, 7 nights here), and clock changes fire once. More in [I-30] and [I-31].
|
|
358
|
+
|
|
280
359
|
### Messages
|
|
281
360
|
|
|
282
361
|
```python
|
|
@@ -284,6 +363,7 @@ approvers: list[str] = []
|
|
|
284
363
|
c.handle("approve", approvers.append) # every "approve" so far, then each new one
|
|
285
364
|
c.handle({"add": add, "remove": remove}) # several names bound together
|
|
286
365
|
by = await c.receive("by") # the next "by"
|
|
366
|
+
note = await c.receive("note", timeout=s.hours(1)) # or WaitTimeout after an hour
|
|
287
367
|
await c.send(other_chain, "note", "hello") # to another run
|
|
288
368
|
await register(webhook, reply_to=c.chain) # this run's id, for an answer
|
|
289
369
|
```
|
|
@@ -296,6 +376,7 @@ let approvers = s::Var::new(Vec::<String>::new()); // sh
|
|
|
296
376
|
c.handle("approve", approvers.handler(|a, name: String| a.push(name)))?;
|
|
297
377
|
c.handle_all(s::Handlers::new().on("add", add).on("remove", remove))?;
|
|
298
378
|
let by: u32 = c.receive("by").await?;
|
|
379
|
+
let note: String = c.receive("note").timeout(s::hours(1)).await?;
|
|
299
380
|
c.send(&other_chain, "note", "hello").await?;
|
|
300
381
|
register(webhook, c.chain().to_owned()).await?;
|
|
301
382
|
```
|
|
@@ -305,19 +386,22 @@ const approvers: string[] = [];
|
|
|
305
386
|
c.handle<string>("approve", (name) => approvers.push(name));
|
|
306
387
|
c.handle({ add, remove });
|
|
307
388
|
const by = await c.receive<number>("by");
|
|
389
|
+
const note = await c.receive<string>("note", { timeout: s.hours(1) });
|
|
308
390
|
await c.send(otherChain, "note", "hello");
|
|
309
391
|
await register(webhook, c.chain);
|
|
310
392
|
```
|
|
311
393
|
|
|
312
394
|
</details>
|
|
313
395
|
|
|
314
|
-
Messages come from clients (`run.send`) or other runs (`c.send`), and a message id makes a resend delivered once. Sending to a run that ended raises `RunEnded`, to a chain that does not exist `RunNotFound`, from a run too. Handlers bound together see their past messages in arrival order, across names. A run consumes
|
|
396
|
+
Messages come from clients (`run.send`) or other runs (`c.send`), and a message id makes a resend delivered once. Sending to a run that ended raises `RunEnded`, to a chain that does not exist `RunNotFound`, from a run too. Handlers bound together see their past messages in arrival order, across names. A run consumes one sender's messages in the order sent; across a continue as new, a message that arrives as the run continues may be consumed after one sent later. More in [I-9].
|
|
315
397
|
|
|
316
398
|
### Children
|
|
317
399
|
|
|
318
400
|
```python
|
|
319
401
|
check = await c.start(verify, {"archive": archive}) # a run of its own
|
|
320
402
|
ok = await check.result()
|
|
403
|
+
await other.cancel("not needed") # a child ends cancelled
|
|
404
|
+
paid = await c.result("payment-42") # the end of any chain, a child or not
|
|
321
405
|
```
|
|
322
406
|
|
|
323
407
|
<details>
|
|
@@ -326,16 +410,20 @@ ok = await check.result()
|
|
|
326
410
|
```rust
|
|
327
411
|
let check = c.start(&verify, VerifyInput { archive }).await?; // a run of its own
|
|
328
412
|
let ok = check.result().await?;
|
|
413
|
+
other.cancel("not needed").await?;
|
|
414
|
+
let paid: Paid = c.result("payment-42").await?;
|
|
329
415
|
```
|
|
330
416
|
|
|
331
417
|
```ts
|
|
332
418
|
const check = await c.start(verify, { archive }); // a run of its own
|
|
333
419
|
const ok = await check.result();
|
|
420
|
+
await other.cancel("not needed");
|
|
421
|
+
const paid = await c.result<Paid>("payment-42");
|
|
334
422
|
```
|
|
335
423
|
|
|
336
424
|
</details>
|
|
337
425
|
|
|
338
|
-
A child may start itself, for recursion over a tree, and may be a workflow of another language, through a stub of the same name. More in [I-11] and [I-5].
|
|
426
|
+
A parent cancels a child it started as a client would, its outcome recorded. `c.result` awaits the end of any chain, without polling: the awaited run tells the waiting one when it ends. A child may start itself, for recursion over a tree, and may be a workflow of another language, through a stub of the same name. More in [I-11] and [I-5].
|
|
339
427
|
|
|
340
428
|
### Loops and continue as new
|
|
341
429
|
|
|
@@ -353,14 +441,17 @@ async for w in c.iterate(Walk()):
|
|
|
353
441
|
|
|
354
442
|
```rust
|
|
355
443
|
let walk = c
|
|
356
|
-
.iterate(Walk::default(), |_, mut w|
|
|
357
|
-
let
|
|
358
|
-
|
|
359
|
-
|
|
444
|
+
.iterate(Walk::default(), |_, mut w| {
|
|
445
|
+
let (source, target) = (source.clone(), target.clone());
|
|
446
|
+
async move {
|
|
447
|
+
let page = list_page(source, w.after.clone()).await?;
|
|
448
|
+
if page.is_empty() {
|
|
449
|
+
return Ok(ControlFlow::Break(w));
|
|
450
|
+
}
|
|
451
|
+
copy_files(page.clone(), target).await?;
|
|
452
|
+
w.after = page.last().cloned();
|
|
453
|
+
Ok(ControlFlow::Continue(w))
|
|
360
454
|
}
|
|
361
|
-
copy_files(page.clone(), target.clone()).await?;
|
|
362
|
-
w.after = page.last().cloned();
|
|
363
|
-
Ok(ControlFlow::Continue(w))
|
|
364
455
|
})
|
|
365
456
|
.await?;
|
|
366
457
|
```
|
|
@@ -465,8 +556,8 @@ A cancelled run raises `Cancelled` where it waits; a step already running finish
|
|
|
465
556
|
### Clients
|
|
466
557
|
|
|
467
558
|
```python
|
|
468
|
-
sub = await s.Substantial.create("file:/var/lib/shop"
|
|
469
|
-
async with s.Substantial.open(url) as sub: ...
|
|
559
|
+
sub = await s.Substantial.create("file:/var/lib/shop") # or s.Substantial.open(url)
|
|
560
|
+
async with s.Substantial.open(url) as sub: ... # closed at the end
|
|
470
561
|
run = await sub.start(checkout, {"order": "a-42", "amount": 1999}, id="order-a-42")
|
|
471
562
|
run = sub.run(checkout, "order-a-42") # a handle on an existing run
|
|
472
563
|
await run.send("address", "1 rue du Lac", id="address-a-42")
|
|
@@ -479,7 +570,7 @@ progress = await run.read("progress") # what the run published w
|
|
|
479
570
|
<summary>In Rust and TypeScript</summary>
|
|
480
571
|
|
|
481
572
|
```rust
|
|
482
|
-
let sub = s::Substantial::create("file:/var/lib/shop").
|
|
573
|
+
let sub = s::Substantial::create("file:/var/lib/shop").await?; // or s::Substantial::open(url)
|
|
483
574
|
let run = sub.start(checkout, order).id("order-a-42").await?;
|
|
484
575
|
let run = sub.run(checkout, "order-a-42"); // a handle on an existing run
|
|
485
576
|
run.send("address", "1 rue du Lac").id("address-a-42").await?;
|
|
@@ -489,7 +580,7 @@ let progress: Option<Progress> = run.read("progress").await?;
|
|
|
489
580
|
```
|
|
490
581
|
|
|
491
582
|
```ts
|
|
492
|
-
await using sub = await s.Substantial.create("file:/var/lib/shop"
|
|
583
|
+
await using sub = await s.Substantial.create("file:/var/lib/shop"); // or s.Substantial.open(url); closed at the end of the scope
|
|
493
584
|
let run = await sub.start(checkout, { order: "a-42", amount: 1999 }, { id: "order-a-42" });
|
|
494
585
|
run = sub.run(checkout, "order-a-42"); // a handle on an existing run
|
|
495
586
|
await run.send("address", "1 rue du Lac", { id: "address-a-42" });
|
|
@@ -502,6 +593,132 @@ const progress = await run.read("progress");
|
|
|
502
593
|
|
|
503
594
|
`create` makes the namespace or opens it; `open` refuses one that does not exist. A Rust `Substantial` is freed when dropped. More in [I-16] and [I-17].
|
|
504
595
|
|
|
596
|
+
### Live runs, history and retry
|
|
597
|
+
|
|
598
|
+
```python
|
|
599
|
+
async for info in sub.runs(checkout): # pending, waiting on time or messages, every version
|
|
600
|
+
print(info.run, info.version, info.waiting_on, info.due_at)
|
|
601
|
+
|
|
602
|
+
for event in await run.history(): # the events its replays read
|
|
603
|
+
print(event.segment, event.at, event.kind, event.data)
|
|
604
|
+
|
|
605
|
+
try:
|
|
606
|
+
await run.result()
|
|
607
|
+
except s.RunFailed:
|
|
608
|
+
... # fix the code, or wait out the outage
|
|
609
|
+
await run.retry() # recorded steps return their values, the failed step runs again
|
|
610
|
+
```
|
|
611
|
+
|
|
612
|
+
<details>
|
|
613
|
+
<summary>In Rust and TypeScript</summary>
|
|
614
|
+
|
|
615
|
+
```rust
|
|
616
|
+
use futures::TryStreamExt;
|
|
617
|
+
|
|
618
|
+
let mut runs = sub.runs(&checkout);
|
|
619
|
+
while let Some(info) = runs.try_next().await? {
|
|
620
|
+
println!("{} {:?} {} {:?}", info.run, info.version, info.waiting_on, info.due_at);
|
|
621
|
+
}
|
|
622
|
+
for event in run.history().await? {
|
|
623
|
+
println!("{} {} {} {}", event.segment, event.at, event.kind, event.data);
|
|
624
|
+
}
|
|
625
|
+
if run.result().await.is_err() {
|
|
626
|
+
run.retry().await?;
|
|
627
|
+
}
|
|
628
|
+
```
|
|
629
|
+
|
|
630
|
+
```ts
|
|
631
|
+
for await (const info of sub.runs(checkout)) console.log(info.run, info.version, info.waitingOn, info.dueAt);
|
|
632
|
+
for (const event of await run.history()) console.log(event.segment, event.at, event.kind, event.data);
|
|
633
|
+
try {
|
|
634
|
+
await run.result();
|
|
635
|
+
} catch (e) {
|
|
636
|
+
if (!(e instanceof s.RunFailed)) throw e;
|
|
637
|
+
await run.retry();
|
|
638
|
+
}
|
|
639
|
+
```
|
|
640
|
+
|
|
641
|
+
</details>
|
|
642
|
+
|
|
643
|
+
`runs` reads a workflow's queue, never its histories: ended runs are not listed, though one that just ended may be for a moment. `retry` starts a new run of the chain that replays the failed one without its failure, so a payment that succeeded is not charged again and the failed step gets a fresh retry budget; `s.step_key()` stays the same. More in [I-17].
|
|
644
|
+
|
|
645
|
+
### Cost, budgets and observing
|
|
646
|
+
|
|
647
|
+
```python
|
|
648
|
+
@s.step
|
|
649
|
+
async def summarize(text: str) -> str:
|
|
650
|
+
reply = await llm.complete(text, idempotency_key=s.step_key())
|
|
651
|
+
s.charge("USD", reply.cost_nano_usd) # this attempt's spend, integer nano-dollars
|
|
652
|
+
return reply.text
|
|
653
|
+
|
|
654
|
+
@s.workflow(budget={"requests": 5_000, "USD": 2_000_000_000}) # each run: 5,000 store requests, $2
|
|
655
|
+
async def digest(c: s.Context, urls: list[str]) -> str:
|
|
656
|
+
summary = await summarize(" ".join(urls))
|
|
657
|
+
cost = await c.cost() # recorded, as c.now() is
|
|
658
|
+
c.log.info("%d requests, %s", cost.requests, cost.amounts)
|
|
659
|
+
return summary
|
|
660
|
+
|
|
661
|
+
def observe(event: s.Observed) -> None: # an s.StoreOp, s.Attempt, s.Replay or s.Scan
|
|
662
|
+
print(event) # forward to your metrics
|
|
663
|
+
|
|
664
|
+
sub = await s.Substantial.open("s3://s3.us-east-1.amazonaws.com/acme/prod?region=us-east-1", credentials="env",
|
|
665
|
+
prices=s.Prices("aws-s3-standard-us-east-1-2026-11-09"), observe=observe)
|
|
666
|
+
run = await sub.start(digest, {"urls": urls}, budget={"USD": 5_000_000_000}) # over the workflow's
|
|
667
|
+
```
|
|
668
|
+
|
|
669
|
+
<details>
|
|
670
|
+
<summary>In Rust and TypeScript</summary>
|
|
671
|
+
|
|
672
|
+
```rust
|
|
673
|
+
#[s::step]
|
|
674
|
+
async fn summarize(text: String) -> anyhow::Result<String> {
|
|
675
|
+
let reply = llm::complete(&text, &s::step_key()).await?;
|
|
676
|
+
s::charge("USD", reply.cost_nano_usd)?;
|
|
677
|
+
Ok(reply.text)
|
|
678
|
+
}
|
|
679
|
+
|
|
680
|
+
#[s::workflow(budget = [("requests", 5_000), ("USD", 2_000_000_000)])]
|
|
681
|
+
async fn digest(c: s::Context, input: Digest) -> s::Result<String> {
|
|
682
|
+
let summary = summarize(input.urls.join(" ")).await?;
|
|
683
|
+
let cost = c.cost().await?;
|
|
684
|
+
c.log.info(format!("{} requests, {:?}", cost.requests, cost.amounts));
|
|
685
|
+
Ok(summary)
|
|
686
|
+
}
|
|
687
|
+
|
|
688
|
+
let sub = s::Substantial::open("s3://s3.us-east-1.amazonaws.com/acme/prod?region=us-east-1")
|
|
689
|
+
.credentials(s::Credentials::Env)
|
|
690
|
+
.prices(s::Prices::named("aws-s3-standard-us-east-1-2026-11-09")?)
|
|
691
|
+
.observe(|event: &s::Observed| println!("{event:?}"))
|
|
692
|
+
.await?;
|
|
693
|
+
let run = sub.start(&digest, input).budget([("USD", 5_000_000_000)]).await?;
|
|
694
|
+
```
|
|
695
|
+
|
|
696
|
+
```ts
|
|
697
|
+
const summarize = s.step("summarize", async (text: string) => {
|
|
698
|
+
const reply = await llm.complete(text, { idempotencyKey: s.stepKey(), signal: s.stepSignal() });
|
|
699
|
+
s.charge("USD", reply.costNanoUsd); // a bigint
|
|
700
|
+
return reply.text;
|
|
701
|
+
});
|
|
702
|
+
|
|
703
|
+
const digest = s.workflow("digest", async (c, input: { urls: string[] }) => {
|
|
704
|
+
const summary = await summarize(input.urls.join(" "));
|
|
705
|
+
const cost = await c.cost();
|
|
706
|
+
c.log.info(`${cost.requests} requests`, cost.amounts);
|
|
707
|
+
return summary;
|
|
708
|
+
}, { budget: { requests: 5_000n, USD: 2_000_000_000n } });
|
|
709
|
+
|
|
710
|
+
const sub = await s.Substantial.open("s3://s3.us-east-1.amazonaws.com/acme/prod?region=us-east-1", {
|
|
711
|
+
credentials: "env",
|
|
712
|
+
prices: new s.Prices("aws-s3-standard-us-east-1-2026-11-09"),
|
|
713
|
+
observe: (event) => console.log(event),
|
|
714
|
+
});
|
|
715
|
+
const run = await sub.start(digest, { urls }, { budget: { USD: 5_000_000_000n } });
|
|
716
|
+
```
|
|
717
|
+
|
|
718
|
+
</details>
|
|
719
|
+
|
|
720
|
+
The core counts each run's store requests, the adapter's retries included, and with `prices` their money; steps add what they charge, children their totals. A run past a limit of its budget raises `s.BudgetExceeded` where it waits or calls next, may still roll back or publish, and ends failed. `observe` gets every store operation, step attempt, replay and scan, to feed any metrics system. More in [I-27], [I-28] and [I-29].
|
|
721
|
+
|
|
505
722
|
### Versions
|
|
506
723
|
|
|
507
724
|
```python
|
|
@@ -563,7 +780,7 @@ const sub = await s.Substantial.open(redisUrl, {
|
|
|
563
780
|
|
|
564
781
|
</details>
|
|
565
782
|
|
|
566
|
-
S3 takes `"env"` (`AWS_ACCESS_KEY_ID`, `AWS_SECRET_ACCESS_KEY`, `AWS_SESSION_TOKEN`), explicit `s.Credentials(...)`, or a function, sync or async, returning keys with an optional `expires_at`: it is called at open and again within 5 minutes of that expiry, for roles and other temporary keys. `s3+http://` reaches a local server. Redis takes none, `"env"` (`REDIS_PASSWORD`, `REDIS_USERNAME`), a password, or such a function returning one (`s::Credentials::password_provider` in Rust). Ended runs are deleted 30 days after
|
|
783
|
+
S3 takes `"env"` (`AWS_ACCESS_KEY_ID`, `AWS_SECRET_ACCESS_KEY`, `AWS_SESSION_TOKEN`), explicit `s.Credentials(...)`, or a function, sync or async, returning keys with an optional `expires_at`: it is called at open and again within 5 minutes of that expiry, for roles and other temporary keys. `s3+http://` reaches a local server. Redis takes none, `"env"` (`REDIS_PASSWORD`, `REDIS_USERNAME`), a password, or such a function returning one (`s::Credentials::password_provider` in Rust). Ended runs are deleted 30 days after they end, or after the `retention` a worker or a workflow sets, at most 100 years; never delete keys under a namespace yourself, by hand or with a lifecycle rule. More in [I-16].
|
|
567
784
|
|
|
568
785
|
### Workers
|
|
569
786
|
|
|
@@ -591,10 +808,82 @@ await sub.worker([checkout, verify], { preset: "service", concurrency: 32 }).ser
|
|
|
591
808
|
|
|
592
809
|
A preset, `script` or `service`, sets every setting; `concurrency`, `max_idle`, `shutdown_grace`, `claim_ttl`, `continue_as_new_after`, `step_timeout` and `retention` can be overridden by name. `serve()` stops on SIGTERM or Ctrl-C, letting steps in flight end. A worker logs a warning for each run that ends failed or cancelled, and a worker that dies logs at once and fails the `result()` waits of its process. The runs of a worker that crashed are taken over by the others within 35 seconds. More in [I-18].
|
|
593
810
|
|
|
811
|
+
A job run on a schedule, such as a Kubernetes CronJob, drains instead of serving:
|
|
812
|
+
|
|
813
|
+
```python
|
|
814
|
+
await sub.worker([nightly, report], preset="script").drain(within=s.minutes(2))
|
|
815
|
+
```
|
|
816
|
+
|
|
817
|
+
<details>
|
|
818
|
+
<summary>In Rust and TypeScript</summary>
|
|
819
|
+
|
|
820
|
+
```rust
|
|
821
|
+
sub.worker((nightly, report)).preset(s::Preset::Script).drain(s::minutes(2)).await?;
|
|
822
|
+
```
|
|
823
|
+
|
|
824
|
+
```ts
|
|
825
|
+
await sub.worker([nightly, report], { preset: "script" }).drain({ within: s.minutes(2) });
|
|
826
|
+
```
|
|
827
|
+
|
|
828
|
+
</details>
|
|
829
|
+
|
|
830
|
+
`drain` serves every run due now or within 2 minutes, takes over the runs of crashed workers whose claims expire by then, and returns once nothing is due within that horizon and nothing is in flight; between two jobs no worker lists the store. A schedule it serves needs a `backfill` of at least the job's period divided by the cron's. More in [I-18] and the [integration] guide.
|
|
831
|
+
|
|
832
|
+
### Wasm workflows
|
|
833
|
+
|
|
834
|
+
A Rust worker also serves workflows compiled to WebAssembly, which it loads from their bytes when it starts or later: a workflow's code changes without a rebuild of the worker, and runs isolated from it. A Wasm workflow is written with the crate `substantial` itself, as a `cdylib`, and the same source also builds as a native workflow, as long as its `#[workflow]` takes no option, since the worker gives a Wasm workflow its name and settings:
|
|
835
|
+
|
|
836
|
+
```rust
|
|
837
|
+
use serde::Deserialize;
|
|
838
|
+
use substantial as s;
|
|
839
|
+
|
|
840
|
+
#[derive(Deserialize)]
|
|
841
|
+
struct Input {
|
|
842
|
+
name: String,
|
|
843
|
+
}
|
|
844
|
+
|
|
845
|
+
#[s::step]
|
|
846
|
+
async fn greet(name: &str) -> Result<String, std::io::Error> {
|
|
847
|
+
Ok(format!("hello {name}"))
|
|
848
|
+
}
|
|
849
|
+
|
|
850
|
+
#[s::workflow]
|
|
851
|
+
async fn greeting(c: s::Context, input: Input) -> s::Result<String> {
|
|
852
|
+
c.sleep(s::days(1)).await?;
|
|
853
|
+
greet(&input.name).await
|
|
854
|
+
}
|
|
855
|
+
|
|
856
|
+
s::export!(greeting, steps = [greet]);
|
|
857
|
+
```
|
|
858
|
+
|
|
859
|
+
It is compiled for `wasm32-unknown-unknown`, then made a component of the world in [`wit/workflow.wit`](wit/workflow.wit):
|
|
860
|
+
|
|
861
|
+
```sh
|
|
862
|
+
cargo build --release --target wasm32-unknown-unknown
|
|
863
|
+
wasm-tools component new target/wasm32-unknown-unknown/release/greeting.wasm -o greeting.wasm
|
|
864
|
+
```
|
|
865
|
+
|
|
866
|
+
A worker with the `wasm-runner` feature loads it and serves it as any workflow, within the memory and the time between two verbs it is given:
|
|
867
|
+
|
|
868
|
+
```rust
|
|
869
|
+
use substantial::wasm::{Limits, Module};
|
|
870
|
+
|
|
871
|
+
let limits = Limits { memory: 64 << 20, code_time: s::seconds(1) };
|
|
872
|
+
let module = Module::new(&std::fs::read("greeting.wasm")?, limits, |_| Ok(()))?;
|
|
873
|
+
let greeting = module.workflow("greeting");
|
|
874
|
+
let worker = sub.worker(greeting.clone()).preset(s::Preset::Service).start()?;
|
|
875
|
+
sub.start(&greeting, serde_json::json!({ "name": "Ada" })).await?;
|
|
876
|
+
```
|
|
877
|
+
|
|
878
|
+
A module loaded after its worker started runs within a workflow of the worker's own, which calls `Module::run` with the run's context, for instance on the module its input names by digest.
|
|
879
|
+
|
|
880
|
+
A Wasm workflow has no network, clock or filesystem: its workflow code calls only Substantial's verbs, each recorded, and its steps only the interfaces the worker adds with the closure of `Module::new`, a wasmtime linker, whose functions may be async. Each replay and each step attempt runs in an instance of its own, and a module that traps, or passes its memory or its code's time, fails what it ran with `WasmTrap` while the worker goes on: a step past its timeout fails with `StepTimeout` as any step, and an error of the worker's own interface traps its step, so a failure that may pass comes back as a value of that interface. Its workflow code calls steps, with their retry and timeout, `sleep`, `sleep_until`, `receive`, with its timeout, `send`, `publish`, `now`, `random`, `uuid` and `c.log`, and a step calls `s::step_key()`. A value JSON cannot hold, such as a NaN, is refused before it leaves the module, as in every host. The decisions are in [the Wasm workflow notes](docs/decisions/wasm-workflows.md).
|
|
881
|
+
|
|
594
882
|
## How it works
|
|
595
883
|
|
|
596
|
-
- **No broker, no server.** Workers and clients coordinate through the store alone, with four operations: an exclusive create, a get, a list and a delete. A run's history is a sequence of keys, each created once and never overwritten. A worker takes a run with a claim, an exclusive create it renews while it works, and finds due runs, timers and messages by listing due entries.
|
|
884
|
+
- **No broker, no server.** Workers and clients coordinate through the store alone, with four operations: an exclusive create, a get, a list and a delete. A run's history is a sequence of keys, each created once and never overwritten. A worker takes a run with a claim, an exclusive create it renews while it works, and finds due runs, timers and messages by listing the due entries of each workflow it serves, one prefix per workflow.
|
|
597
885
|
- **Checked at open.** Opening a namespace creates a key twice and refuses a store that accepts both, as an S3-compatible store ignoring conditional writes would.
|
|
886
|
+
- **What it costs.** On S3, a run that starts, calls 3 steps, sleeps a day and completes makes 13 PUT, 6 GET, 9 LIST and 13 free DELETE over its life, collection included: $1.12 a month for 10,000 runs. An idle worker of the `service` preset lists each workflow it serves every 30 s, $0.43 a month per workflow.
|
|
598
887
|
- **One core.** The protocol lives in a core compiled to WebAssembly, with no I/O, clock or randomness of its own. Every host loads the same module and only runs your code and talks to the store, so a namespace can be shared by workers and clients in all three languages.
|
|
599
888
|
|
|
600
889
|
The store contract and the core's boundary are in [the reference][I-22]. What each verb does to the store, its round trips, its failure behaviour and its monthly cost on S3 are in [the architecture](https://github.com/zifeo/substantial/blob/main/docs/architecture.md).
|
|
@@ -620,7 +909,7 @@ cargo test --workspace # or uv run pytest, or npm test
|
|
|
620
909
|
|
|
621
910
|
Each host has two examples on a file or memory store, `first_workflow` and `backup`: `cargo run -p substantial --example first_workflow -- /tmp/demo`, `uv run python/examples/first_workflow.py /tmp/demo-py`, `node --conditions=substantial-source typescript/examples/first_workflow.ts /tmp/demo-ts`.
|
|
622
911
|
|
|
623
|
-
`rust/wasm` and `rust/proto` are symlinks to `wasm/` and `proto/`, so that the published crate carries them
|
|
912
|
+
`rust/wasm` and `rust/proto` are symlinks to `wasm/` and `proto/`, so that the published crate carries them. `npm run gen` regenerates the TypeScript protobuf code from `proto/`.
|
|
624
913
|
|
|
625
914
|
## License
|
|
626
915
|
|
|
@@ -642,6 +931,11 @@ Each host has two examples on a file or memory store, `first_workflow` and `back
|
|
|
642
931
|
[I-18]: https://github.com/zifeo/substantial/blob/main/docs/interfaces.md#i-18-workers
|
|
643
932
|
[I-21]: https://github.com/zifeo/substantial/blob/main/docs/interfaces.md#i-21-actions-in-each-host
|
|
644
933
|
[I-22]: https://github.com/zifeo/substantial/blob/main/docs/interfaces.md#i-22-for-implementers
|
|
934
|
+
[I-27]: https://github.com/zifeo/substantial/blob/main/docs/interfaces.md#i-27-cost-and-charges
|
|
935
|
+
[I-28]: https://github.com/zifeo/substantial/blob/main/docs/interfaces.md#i-28-budgets
|
|
936
|
+
[I-29]: https://github.com/zifeo/substantial/blob/main/docs/interfaces.md#i-29-observing
|
|
937
|
+
[I-30]: https://github.com/zifeo/substantial/blob/main/docs/interfaces.md#i-30-polling
|
|
938
|
+
[I-31]: https://github.com/zifeo/substantial/blob/main/docs/interfaces.md#i-31-schedules
|
|
645
939
|
[integration]: https://github.com/zifeo/substantial/blob/main/docs/integration.md
|
|
646
940
|
[reference]: https://github.com/zifeo/substantial/blob/main/docs/interfaces.md
|
|
647
941
|
[testing]: https://github.com/zifeo/substantial/blob/main/docs/testing.md
|