@streamsy/projection 0.6.0 → 0.8.0
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.
- package/README.md +19 -6
- package/dist/index.d.ts +26 -4
- package/dist/index.js +180 -36
- package/package.json +5 -4
package/README.md
CHANGED
|
@@ -77,7 +77,7 @@ outside the transaction. It runs at least once and passes its result to
|
|
|
77
77
|
|
|
78
78
|
`Commit.withCompensation(f)` calls `f(result, cause, unit)` after successful
|
|
79
79
|
processing if the commit fails. It is also available as a `.pipe` combinator.
|
|
80
|
-
For durable external delivery, use
|
|
80
|
+
For durable external delivery, use [`@streamsy/outbox`](https://streamsy.dev/docs/outbox).
|
|
81
81
|
|
|
82
82
|
`Commit.make` defines `load`, `apply`, and optional `remove`. Custom Commits
|
|
83
83
|
extend ranges on retry. Never save after a failed handler; compare-and-set on
|
|
@@ -211,15 +211,28 @@ Each input must give every projection param, so that one id picks one binding.
|
|
|
211
211
|
never commit here, so they never trigger. Do not also run an attached binding
|
|
212
212
|
under `Projection.follow`.
|
|
213
213
|
|
|
214
|
-
|
|
215
|
-
commit and before the run, the binding is late
|
|
216
|
-
write. The next run catches up from the
|
|
217
|
-
available. Removal before that run
|
|
218
|
-
by another process on the same SQL
|
|
214
|
+
By default `Projection.attach` keeps the set of bindings with work in memory.
|
|
215
|
+
If the process stops after a commit and before the run, the binding is late
|
|
216
|
+
until its source gets its next write. The next run catches up from the
|
|
217
|
+
checkpoint while the source history remains available. Removal before that run
|
|
218
|
+
uses the final gone unit instead. A commit by another process on the same SQL
|
|
219
|
+
file does not show.
|
|
220
|
+
|
|
221
|
+
Pass `durable: true` to close that gap. Each commit then also writes a pending
|
|
222
|
+
mark for each binding it touches, in the same transaction, to
|
|
223
|
+
`streamsy_projection_pending`. A binding that catches up deletes its mark,
|
|
224
|
+
unless a write during the run marked it again. When the layer starts, it runs
|
|
225
|
+
the bindings that still have marks, up to `batch` (default 25) per pass. A
|
|
226
|
+
binding that fails waits alone, from 1 s doubling to 1 hour, and its
|
|
227
|
+
neighbours keep running. Durable mode needs a local memory or SQL storage
|
|
228
|
+
layer.
|
|
219
229
|
|
|
220
230
|
On Cloudflare, acquire a wakeup in the object's layer scope. Local commits
|
|
221
231
|
fill the same runner's pending set; `next` answers "now" while it has work,
|
|
222
232
|
and each alarm drains one pass (100 units per binding by default).
|
|
233
|
+
`ProjectionWakeup.attach` is durable by default, because Cloudflare evicts idle
|
|
234
|
+
objects. After an eviction or a deploy, the object's acquire reads the marks
|
|
235
|
+
and arms the alarm. Pass `durable: false` for the in-memory set only.
|
|
223
236
|
|
|
224
237
|
```ts
|
|
225
238
|
StreamsyObject.make({
|
package/dist/index.d.ts
CHANGED
|
@@ -282,6 +282,18 @@ interface AttachOptions {
|
|
|
282
282
|
readonly limit?: number;
|
|
283
283
|
/** Retry a failed drain pass. Defaults to capped, jittered exponential backoff. */
|
|
284
284
|
readonly retry?: Schedule.Schedule<unknown>;
|
|
285
|
+
/**
|
|
286
|
+
* Keep a pending mark for each binding in the storage owner, written in the
|
|
287
|
+
* transaction of the commit that needs the run. After a restart or an
|
|
288
|
+
* eviction, the runner finds the marks and runs those bindings. A binding
|
|
289
|
+
* that fails waits alone, 1 s doubling to 1 hour, and its neighbours run.
|
|
290
|
+
* Needs a local memory or SQL storage layer.
|
|
291
|
+
*
|
|
292
|
+
* Default: false for `Projection.attach`, true for `ProjectionWakeup.attach`.
|
|
293
|
+
*/
|
|
294
|
+
readonly durable?: boolean;
|
|
295
|
+
/** With `durable`, the due marks that one drain pass reads. Default 25. */
|
|
296
|
+
readonly batch?: number;
|
|
285
297
|
}
|
|
286
298
|
/**
|
|
287
299
|
* Run a projection whenever a local commit writes one of its inputs. The layer
|
|
@@ -292,8 +304,10 @@ interface AttachOptions {
|
|
|
292
304
|
* Use it on long-lived hosts (Bun, Node). On a Cloudflare object, use
|
|
293
305
|
* `ProjectionWakeup.attach(projection)`.
|
|
294
306
|
*
|
|
295
|
-
*
|
|
296
|
-
* before the run leaves that binding late until its source gets its
|
|
307
|
+
* By default the set of bindings with work lives in memory. A crash after a
|
|
308
|
+
* commit and before the run leaves that binding late until its source gets its
|
|
309
|
+
* next write. With `durable: true`, a pending mark in storage covers that
|
|
310
|
+
* window, and the layer runs due marks when it starts.
|
|
297
311
|
* Inputs on another owner never commit here, so they never trigger. Do not run
|
|
298
312
|
* an attached binding under `Projection.follow` too: use one writer per key.
|
|
299
313
|
*
|
|
@@ -405,14 +419,22 @@ export declare const ProjectionRetry: Context.Reference<Schedule.Schedule<unknow
|
|
|
405
419
|
* Local commits choose bindings; each alarm drains the same runner used by
|
|
406
420
|
* Projection.attach. Acquire it in the object layer's scope, before any
|
|
407
421
|
* request writes.
|
|
422
|
+
*
|
|
423
|
+
* Durable by default: each commit writes a pending mark for its bindings in
|
|
424
|
+
* the same transaction, so an eviction or a deploy between the commit and the
|
|
425
|
+
* alarm loses no work. The host's acquire reconcile reads the marks and arms
|
|
426
|
+
* the alarm. Pass `durable: false` for the in-memory set only.
|
|
408
427
|
*/
|
|
409
428
|
declare const attach: <I extends InputMap, E, R, Q, Result, P, OR = never>(projection: Attachable<Executable<I, E, R, Q, Result, P, unknown, OR>>, options?: AttachOptions) => Effect.Effect<{
|
|
410
429
|
name: string;
|
|
411
|
-
next: Effect.Effect<import("effect/Option").Option<number>,
|
|
430
|
+
next: Effect.Effect<import("effect/Option").Option<number>, import("@streamsy/core").StorageFault, never>;
|
|
412
431
|
run: Effect.Effect<{
|
|
413
432
|
more: boolean;
|
|
414
|
-
}
|
|
433
|
+
} | {
|
|
434
|
+
more: boolean;
|
|
435
|
+
}, E | ProjectionFault | import("@streamsy/core").StorageFault, OR | Q | InputServices<I> | Exclude<R, P>>;
|
|
415
436
|
retry: import("effect/Schedule").Schedule<unknown, unknown, never, never>;
|
|
437
|
+
deferred: Effect.Effect<number, import("@streamsy/core").StorageFault, never>;
|
|
416
438
|
}, never, import("effect/Scope").Scope | import("@streamsy/core").Storage>;
|
|
417
439
|
export declare const ProjectionWakeup: {
|
|
418
440
|
attach: typeof attach;
|
package/dist/index.js
CHANGED
|
@@ -3,7 +3,7 @@ import { a as rangesOf, c as canonicalParams, d as withParams, f as ProjectionFa
|
|
|
3
3
|
import { Array, Cause, Clock, Context, Data, Duration, Effect, Exit, Function, Layer, Option, Pipeable, Predicate, PubSub, Record, Ref, Result, Schedule, Schema, Stream } from "effect";
|
|
4
4
|
import { CommitBoundary, DurableState, Offset, Storage, StreamId, ZERO_OFFSET, compare, isStreamsFault } from "@streamsy/core";
|
|
5
5
|
import { Streams, StreamsWriter } from "@streamsy/core/protocol";
|
|
6
|
-
import { CurrentOwner, compileSegments, isForeignOwner, ownerOf } from "@streamsy/core/internal";
|
|
6
|
+
import { CommitHooks, CurrentOwner, MAX_PRODUCER_ID_LENGTH, ProjectionMarks, compileSegments, fitKey, isForeignOwner, ownerOf } from "@streamsy/core/internal";
|
|
7
7
|
import { SqlClient } from "effect/sql";
|
|
8
8
|
// src/batch.ts
|
|
9
9
|
/** An input that was skipped this pass: nothing read, nothing known beyond its cursor. */
|
|
@@ -401,8 +401,13 @@ const transaction = withPrepare(make({
|
|
|
401
401
|
})
|
|
402
402
|
}));
|
|
403
403
|
// src/commit/producer.ts
|
|
404
|
-
/**
|
|
405
|
-
|
|
404
|
+
/**
|
|
405
|
+
* The pinned tuple identity: the params join the id so two parameterisations never share a sequence.
|
|
406
|
+
* An id within the storage limit is used as it is, because outputs already hold producer state under it.
|
|
407
|
+
* A longer id becomes `<id>/v<version>/#<sha256 of the full id>`. A long projection id is cut by
|
|
408
|
+
* code point so the result is at most the limit.
|
|
409
|
+
*/
|
|
410
|
+
const producerId = (id, version, params) => fitKey(`${id}/v${version}${Record.keys(params).length === 0 ? "" : `/${canonicalParams(params)}`}`, MAX_PRODUCER_ID_LENGTH, `${id}/v${version}/`);
|
|
406
411
|
/** The head of one output. A missing output on the checkpoint's owner is created here. */
|
|
407
412
|
const prepareOutput = (output, store) => {
|
|
408
413
|
const { wire, owner: outputOwner } = output.service;
|
|
@@ -497,12 +502,13 @@ const producer = pin(make({
|
|
|
497
502
|
if (before.record.state !== void 0) Object.assign(pinned, { state: before.record.state });
|
|
498
503
|
token = yield* owner.compareAndSet(projection, before.token, pinned).pipe(Effect.mapError(asPin));
|
|
499
504
|
}
|
|
505
|
+
const appendAs = yield* producerId(projection.id, projection.version, projection.params);
|
|
500
506
|
for (const [name, output] of Record.toEntries(streams)) {
|
|
501
507
|
const seq = seqs[name];
|
|
502
508
|
const items = processed.items[name] ?? [];
|
|
503
509
|
if (seq === void 0 || !Array.isReadonlyArrayNonEmpty(items)) continue;
|
|
504
510
|
yield* appendPinned(output.address, output.service.wire, items, {
|
|
505
|
-
producerId:
|
|
511
|
+
producerId: appendAs,
|
|
506
512
|
producerEpoch: projection.generation,
|
|
507
513
|
producerSeq: seq
|
|
508
514
|
});
|
|
@@ -991,6 +997,9 @@ const follow = (projection, options = {}) => Stream.unwrap(Effect.gen(function*
|
|
|
991
997
|
return [yield* progressOf(projection, result), result];
|
|
992
998
|
}));
|
|
993
999
|
})).pipe(Stream.withSpan("Projection.follow"));
|
|
1000
|
+
const MAX_DEFER_MS = 36e5;
|
|
1001
|
+
/** The wait before a failed binding runs again: 1 s, doubling, at most 1 hour. */
|
|
1002
|
+
const deferDelay = (attempts) => Math.min(1e3 * 2 ** Math.max(0, attempts - 1), MAX_DEFER_MS);
|
|
994
1003
|
/**
|
|
995
1004
|
* The binding of a written stream id, if the projection reads that id.
|
|
996
1005
|
*
|
|
@@ -1025,6 +1034,27 @@ const matcher = (declaration) => {
|
|
|
1025
1034
|
return Option.none();
|
|
1026
1035
|
};
|
|
1027
1036
|
};
|
|
1037
|
+
const ParamsJson = Schema.fromJsonString(Schema.Record(Schema.String, Schema.String));
|
|
1038
|
+
/**
|
|
1039
|
+
* The binding of a pending mark's params, if they still decode with the
|
|
1040
|
+
* projection's param codecs and encode back to the same key.
|
|
1041
|
+
*/
|
|
1042
|
+
const bindingOf = (declaration, key) => Option.flatMap(Schema.decodeOption(ParamsJson)(key), (encoded) => {
|
|
1043
|
+
const decoded = {};
|
|
1044
|
+
for (const [name, codec] of Record.toEntries(declaration.params)) {
|
|
1045
|
+
const value = encoded[name];
|
|
1046
|
+
if (value === void 0) return Option.none();
|
|
1047
|
+
const result = Schema.decodeOption(codec)(value);
|
|
1048
|
+
if (Option.isNone(result)) return Option.none();
|
|
1049
|
+
decoded[name] = result.value;
|
|
1050
|
+
}
|
|
1051
|
+
try {
|
|
1052
|
+
const binding = declaration.at(decoded);
|
|
1053
|
+
return canonicalParams(binding.params) === key ? Option.some(binding) : Option.none();
|
|
1054
|
+
} catch {
|
|
1055
|
+
return Option.none();
|
|
1056
|
+
}
|
|
1057
|
+
});
|
|
1028
1058
|
/**
|
|
1029
1059
|
* A binding that faults with `history-unavailable` on an input whose head is
|
|
1030
1060
|
* absent can never run: its source is gone, and an id never returns. With a
|
|
@@ -1044,16 +1074,45 @@ const sourceGone = (inputs, cause) => Effect.gen(function* () {
|
|
|
1044
1074
|
}), Effect.orElseSucceed(() => false));
|
|
1045
1075
|
});
|
|
1046
1076
|
/**
|
|
1077
|
+
* Register the commit hook that marks each binding a transaction writes, in
|
|
1078
|
+
* that transaction. Dies when the storage layer has no marks, for example
|
|
1079
|
+
* over remote storage.
|
|
1080
|
+
*/
|
|
1081
|
+
const durableMarks = (projection, match) => Effect.gen(function* () {
|
|
1082
|
+
const hooks = yield* Effect.serviceOption(CommitHooks);
|
|
1083
|
+
const marks = yield* Effect.serviceOption(ProjectionMarks);
|
|
1084
|
+
if (Option.isNone(hooks) || Option.isNone(marks)) return yield* Effect.die(/* @__PURE__ */ new Error(`Projection ${projection} cannot attach durably: the storage layer keeps no projection marks. Use a local memory or SQL storage layer, or pass durable: false.`));
|
|
1085
|
+
yield* hooks.value.beforeCommit((ids) => {
|
|
1086
|
+
const keys = /* @__PURE__ */ new Set();
|
|
1087
|
+
for (const id of ids) {
|
|
1088
|
+
const found = match(id);
|
|
1089
|
+
if (Option.isSome(found)) keys.add(found.value[0]);
|
|
1090
|
+
}
|
|
1091
|
+
return keys.size === 0 ? Effect.void : marks.value.mark(projection, [...keys]);
|
|
1092
|
+
});
|
|
1093
|
+
return marks.value;
|
|
1094
|
+
});
|
|
1095
|
+
/**
|
|
1047
1096
|
* One scoped commit subscription and pending set, shared by the two host
|
|
1048
1097
|
* adapters. The subscription exists when this effect returns, so a commit
|
|
1049
1098
|
* after that point always marks its binding.
|
|
1099
|
+
*
|
|
1100
|
+
* With `durable`, a commit hook also writes a pending mark for each binding in
|
|
1101
|
+
* the commit's transaction. The in-memory set stays the fast path. A binding
|
|
1102
|
+
* that catches up clears its mark with a `seq` compare, so a write during the
|
|
1103
|
+
* run keeps it. After a restart, `next`, `wait` and `drain` find the marks in
|
|
1104
|
+
* storage. A failed binding defers its own mark, and its neighbours run.
|
|
1050
1105
|
*/
|
|
1051
|
-
const runnerOf = (declaration,
|
|
1106
|
+
const runnerOf = (declaration, options = {}) => {
|
|
1107
|
+
const limit = options.limit ?? 100;
|
|
1108
|
+
const batch = options.batch ?? 25;
|
|
1052
1109
|
if (!Number.isSafeInteger(limit) || limit <= 0) throw new RangeError("Projection.attach limit must be a positive safe integer");
|
|
1110
|
+
if (!Number.isSafeInteger(batch) || batch <= 0) throw new RangeError("Projection.attach batch must be a positive safe integer");
|
|
1053
1111
|
const match = matcher(declaration);
|
|
1054
1112
|
return Effect.gen(function* () {
|
|
1055
1113
|
const committed = yield* (yield* Storage).committed;
|
|
1056
1114
|
const pending = /* @__PURE__ */ new Map();
|
|
1115
|
+
const marks = options.durable === true ? yield* durableMarks(declaration.id, match) : void 0;
|
|
1057
1116
|
const add = (ids) => {
|
|
1058
1117
|
for (const id of ids) {
|
|
1059
1118
|
const found = match(id);
|
|
@@ -1061,33 +1120,102 @@ const runnerOf = (declaration, limit = 100) => {
|
|
|
1061
1120
|
}
|
|
1062
1121
|
};
|
|
1063
1122
|
const absorb = Effect.map(PubSub.takeUpTo(committed, Number.POSITIVE_INFINITY), Array.forEach(add));
|
|
1123
|
+
const next = Effect.gen(function* () {
|
|
1124
|
+
yield* absorb;
|
|
1125
|
+
if (pending.size > 0) return Option.some(yield* Clock.currentTimeMillis);
|
|
1126
|
+
return marks === void 0 ? Option.none() : yield* marks.nextDue(declaration.id);
|
|
1127
|
+
});
|
|
1128
|
+
const take = Effect.map(PubSub.take(committed), add);
|
|
1129
|
+
const waitDue = (store) => Effect.gen(function* () {
|
|
1130
|
+
const due = yield* store.nextDue(declaration.id);
|
|
1131
|
+
if (Option.isNone(due)) return yield* take;
|
|
1132
|
+
const delay = due.value - (yield* Clock.currentTimeMillis);
|
|
1133
|
+
return yield* delay <= 0 ? Effect.void : take.pipe(Effect.timeoutOrElse({
|
|
1134
|
+
duration: delay,
|
|
1135
|
+
orElse: () => Effect.void
|
|
1136
|
+
}));
|
|
1137
|
+
});
|
|
1138
|
+
/** Wait for a commit, or in durable mode for the next due mark. */
|
|
1139
|
+
const wait = Effect.gen(function* () {
|
|
1140
|
+
yield* absorb;
|
|
1141
|
+
if (pending.size > 0) return void 0;
|
|
1142
|
+
return yield* marks === void 0 ? take : waitDue(marks);
|
|
1143
|
+
});
|
|
1144
|
+
const dropped = (key) => Effect.logWarning(`Projection ${declaration.id} dropped binding ${key}: its source was removed before its first run`);
|
|
1145
|
+
/**
|
|
1146
|
+
* Every binding runs once, even if a peer fails; failed or bounded work
|
|
1147
|
+
* stays pending. A binding whose source went before its first run is
|
|
1148
|
+
* dropped, because no later run can succeed.
|
|
1149
|
+
*/
|
|
1150
|
+
const drainInMemory = Effect.gen(function* () {
|
|
1151
|
+
let failure;
|
|
1152
|
+
for (const [key, binding] of Array.fromIterable(pending)) {
|
|
1153
|
+
pending.delete(key);
|
|
1154
|
+
const exit = yield* Effect.exit(run(binding, { limit }));
|
|
1155
|
+
if (Exit.isSuccess(exit) && exit.value.status !== "limit-reached") continue;
|
|
1156
|
+
if (Exit.isFailure(exit) && (yield* sourceGone(binding.inputs, exit.cause))) {
|
|
1157
|
+
yield* dropped(key);
|
|
1158
|
+
continue;
|
|
1159
|
+
}
|
|
1160
|
+
pending.set(key, binding);
|
|
1161
|
+
if (Exit.isFailure(exit)) failure ??= exit.cause;
|
|
1162
|
+
}
|
|
1163
|
+
yield* absorb;
|
|
1164
|
+
if (failure !== void 0) return yield* Effect.failCause(failure);
|
|
1165
|
+
return { more: pending.size > 0 };
|
|
1166
|
+
});
|
|
1167
|
+
/**
|
|
1168
|
+
* The in-memory set plus up to `batch` due marks. A binding that catches up
|
|
1169
|
+
* clears its mark by `seq`. A failed binding defers its own mark and does
|
|
1170
|
+
* not fail the pass.
|
|
1171
|
+
*/
|
|
1172
|
+
const drainDurable = (store) => Effect.gen(function* () {
|
|
1173
|
+
const id = declaration.id;
|
|
1174
|
+
const now = yield* Clock.currentTimeMillis;
|
|
1175
|
+
const work = new Map(pending);
|
|
1176
|
+
pending.clear();
|
|
1177
|
+
for (const row of yield* store.due(id, now, batch)) {
|
|
1178
|
+
if (work.has(row.params)) continue;
|
|
1179
|
+
const binding = bindingOf(declaration, row.params);
|
|
1180
|
+
if (Option.isNone(binding)) {
|
|
1181
|
+
yield* Effect.logWarning(`Projection ${id} removed pending mark ${row.params}: its params no longer decode`);
|
|
1182
|
+
yield* store.remove(id, row.params);
|
|
1183
|
+
continue;
|
|
1184
|
+
}
|
|
1185
|
+
work.set(row.params, binding.value);
|
|
1186
|
+
}
|
|
1187
|
+
const states = yield* store.read(id, [...work.keys()]);
|
|
1188
|
+
for (const [key, binding] of work) {
|
|
1189
|
+
const exit = yield* Effect.exit(run(binding, { limit }));
|
|
1190
|
+
const state = states.get(key);
|
|
1191
|
+
if (Exit.isSuccess(exit)) {
|
|
1192
|
+
if (exit.value.status === "limit-reached") pending.set(key, binding);
|
|
1193
|
+
else if (state !== void 0) yield* store.clear(id, key, state.seq);
|
|
1194
|
+
continue;
|
|
1195
|
+
}
|
|
1196
|
+
if (yield* sourceGone(binding.inputs, exit.cause)) {
|
|
1197
|
+
yield* dropped(key);
|
|
1198
|
+
yield* store.remove(id, key);
|
|
1199
|
+
continue;
|
|
1200
|
+
}
|
|
1201
|
+
const attempts = (state?.attempts ?? 0) + 1;
|
|
1202
|
+
const delay = deferDelay(attempts);
|
|
1203
|
+
yield* store.defer(id, key, attempts, (yield* Clock.currentTimeMillis) + delay);
|
|
1204
|
+
yield* Effect.logWarning(`Projection ${id} binding ${key} failed (attempt ${attempts}); it runs again in ${delay} ms`, exit.cause);
|
|
1205
|
+
}
|
|
1206
|
+
yield* absorb;
|
|
1207
|
+
if (pending.size > 0) return { more: true };
|
|
1208
|
+
const after = yield* store.nextDue(id);
|
|
1209
|
+
return { more: Option.isSome(after) && after.value <= (yield* Clock.currentTimeMillis) };
|
|
1210
|
+
});
|
|
1064
1211
|
return {
|
|
1065
|
-
next
|
|
1066
|
-
|
|
1067
|
-
return pending.size > 0 ? Option.some(yield* Clock.currentTimeMillis) : Option.none();
|
|
1068
|
-
}),
|
|
1069
|
-
wait: Effect.gen(function* () {
|
|
1070
|
-
yield* absorb;
|
|
1071
|
-
if (pending.size === 0) add(yield* PubSub.take(committed));
|
|
1072
|
-
}),
|
|
1212
|
+
next,
|
|
1213
|
+
wait,
|
|
1073
1214
|
drain: Effect.fn("Projection.drain")(function* () {
|
|
1074
1215
|
yield* absorb;
|
|
1075
|
-
|
|
1076
|
-
|
|
1077
|
-
|
|
1078
|
-
const exit = yield* Effect.exit(run(binding, { limit }));
|
|
1079
|
-
if (Exit.isSuccess(exit) && exit.value.status !== "limit-reached") continue;
|
|
1080
|
-
if (Exit.isFailure(exit) && (yield* sourceGone(binding.inputs, exit.cause))) {
|
|
1081
|
-
yield* Effect.logWarning(`Projection ${declaration.id} dropped binding ${key}: its source was removed before its first run`);
|
|
1082
|
-
continue;
|
|
1083
|
-
}
|
|
1084
|
-
pending.set(key, binding);
|
|
1085
|
-
if (Exit.isFailure(exit)) failure ??= exit.cause;
|
|
1086
|
-
}
|
|
1087
|
-
yield* absorb;
|
|
1088
|
-
if (failure !== void 0) return yield* Effect.failCause(failure);
|
|
1089
|
-
return { more: pending.size > 0 };
|
|
1090
|
-
})()
|
|
1216
|
+
return yield* marks === void 0 ? drainInMemory : drainDurable(marks);
|
|
1217
|
+
})(),
|
|
1218
|
+
deferred: marks === void 0 ? Effect.succeed(0) : marks.deferred(declaration.id)
|
|
1091
1219
|
};
|
|
1092
1220
|
});
|
|
1093
1221
|
};
|
|
@@ -1101,8 +1229,10 @@ const runnerOf = (declaration, limit = 100) => {
|
|
|
1101
1229
|
* Use it on long-lived hosts (Bun, Node). On a Cloudflare object, use
|
|
1102
1230
|
* `ProjectionWakeup.attach(projection)`.
|
|
1103
1231
|
*
|
|
1104
|
-
*
|
|
1105
|
-
* before the run leaves that binding late until its source gets its
|
|
1232
|
+
* By default the set of bindings with work lives in memory. A crash after a
|
|
1233
|
+
* commit and before the run leaves that binding late until its source gets its
|
|
1234
|
+
* next write. With `durable: true`, a pending mark in storage covers that
|
|
1235
|
+
* window, and the layer runs due marks when it starts.
|
|
1106
1236
|
* Inputs on another owner never commit here, so they never trigger. Do not run
|
|
1107
1237
|
* an attached binding under `Projection.follow` too: use one writer per key.
|
|
1108
1238
|
*
|
|
@@ -1113,11 +1243,15 @@ const runnerOf = (declaration, limit = 100) => {
|
|
|
1113
1243
|
* ```
|
|
1114
1244
|
*/
|
|
1115
1245
|
const attach$1 = (projection, options = {}) => {
|
|
1116
|
-
const runner = runnerOf(projection,
|
|
1246
|
+
const runner = runnerOf(projection, {
|
|
1247
|
+
limit: options.limit,
|
|
1248
|
+
durable: options.durable ?? false,
|
|
1249
|
+
batch: options.batch
|
|
1250
|
+
});
|
|
1117
1251
|
return Layer.effectDiscard(Effect.gen(function* () {
|
|
1118
1252
|
const { wait, drain } = yield* runner;
|
|
1119
1253
|
const retry = options.retry ?? (yield* ProjectionRetry);
|
|
1120
|
-
yield* wait.pipe(Effect.andThen(drain
|
|
1254
|
+
yield* wait.pipe(Effect.andThen(drain), Effect.tapError((error) => Effect.logError(`Projection.attach ${projection.id} failed`, error)), Effect.retry(retry), Effect.repeat(Schedule.forever), Effect.withSpan("Projection.attach"), Effect.forkScoped);
|
|
1121
1255
|
}));
|
|
1122
1256
|
};
|
|
1123
1257
|
// src/current.ts
|
|
@@ -1277,16 +1411,26 @@ const each = (handle) => (batch, unit) => Effect.forEach(items(batch), (entry) =
|
|
|
1277
1411
|
* Local commits choose bindings; each alarm drains the same runner used by
|
|
1278
1412
|
* Projection.attach. Acquire it in the object layer's scope, before any
|
|
1279
1413
|
* request writes.
|
|
1414
|
+
*
|
|
1415
|
+
* Durable by default: each commit writes a pending mark for its bindings in
|
|
1416
|
+
* the same transaction, so an eviction or a deploy between the commit and the
|
|
1417
|
+
* alarm loses no work. The host's acquire reconcile reads the marks and arms
|
|
1418
|
+
* the alarm. Pass `durable: false` for the in-memory set only.
|
|
1280
1419
|
*/
|
|
1281
1420
|
const attach = (projection, options = {}) => {
|
|
1282
|
-
const runner = runnerOf(projection,
|
|
1421
|
+
const runner = runnerOf(projection, {
|
|
1422
|
+
limit: options.limit,
|
|
1423
|
+
durable: options.durable ?? true,
|
|
1424
|
+
batch: options.batch
|
|
1425
|
+
});
|
|
1283
1426
|
return Effect.gen(function* () {
|
|
1284
|
-
const { next, drain } = yield* runner;
|
|
1427
|
+
const { next, drain, deferred } = yield* runner;
|
|
1285
1428
|
return {
|
|
1286
1429
|
name: `streamsy/projection/${encodeURIComponent(projection.id)}`,
|
|
1287
1430
|
next,
|
|
1288
1431
|
run: drain,
|
|
1289
|
-
retry: options.retry ?? (yield* ProjectionRetry)
|
|
1432
|
+
retry: options.retry ?? (yield* ProjectionRetry),
|
|
1433
|
+
deferred
|
|
1290
1434
|
};
|
|
1291
1435
|
});
|
|
1292
1436
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@streamsy/projection",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.8.0",
|
|
4
4
|
"description": "Checkpointed Effect projections over one or more retained streams.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -37,7 +37,7 @@
|
|
|
37
37
|
"test:dialects": "bun --bun vitest run test/sql-dialects.test.ts"
|
|
38
38
|
},
|
|
39
39
|
"dependencies": {
|
|
40
|
-
"@streamsy/core": "^0.
|
|
40
|
+
"@streamsy/core": "^0.8.0"
|
|
41
41
|
},
|
|
42
42
|
"devDependencies": {
|
|
43
43
|
"@cloudflare/workers-types": "4.20251228.0",
|
|
@@ -47,8 +47,9 @@
|
|
|
47
47
|
"@effect/sql-sqlite-bun": "4.0.5",
|
|
48
48
|
"@effect/sql-sqlite-do": "4.0.5",
|
|
49
49
|
"@effect/vitest": "4.0.5",
|
|
50
|
-
"@streamsy/
|
|
51
|
-
"@streamsy/
|
|
50
|
+
"@streamsy/outbox": "0.8.0",
|
|
51
|
+
"@streamsy/serve": "0.8.0",
|
|
52
|
+
"@streamsy/storage": "0.8.0",
|
|
52
53
|
"@types/bun": "^1.4.3",
|
|
53
54
|
"effect": "4.0.5",
|
|
54
55
|
"miniflare": "4.20260730.0",
|