spool-sdk 0.0.0-stage → 0.2.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/LICENSE +202 -0
- package/README.md +149 -2
- package/dist/backoff.d.ts +15 -0
- package/dist/backoff.js +51 -0
- package/dist/backoff.js.map +1 -0
- package/dist/channel.d.ts +13 -0
- package/dist/channel.js +46 -0
- package/dist/channel.js.map +1 -0
- package/dist/context.d.ts +221 -0
- package/dist/context.js +331 -0
- package/dist/context.js.map +1 -0
- package/dist/duration.d.ts +22 -0
- package/dist/duration.js +39 -0
- package/dist/duration.js.map +1 -0
- package/dist/errors.d.ts +115 -0
- package/dist/errors.js +143 -0
- package/dist/errors.js.map +1 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.js +6 -0
- package/dist/index.js.map +1 -0
- package/dist/json.d.ts +8 -0
- package/dist/json.js +24 -0
- package/dist/json.js.map +1 -0
- package/dist/logger.d.ts +9 -0
- package/dist/logger.js +12 -0
- package/dist/logger.js.map +1 -0
- package/dist/proto/workers_pb.d.ts +900 -0
- package/dist/proto/workers_pb.js +168 -0
- package/dist/worker.d.ts +51 -0
- package/dist/worker.js +543 -0
- package/dist/worker.js.map +1 -0
- package/dist/workflow.d.ts +13 -0
- package/dist/workflow.js +10 -0
- package/dist/workflow.js.map +1 -0
- package/package.json +49 -3
- package/src/backoff.ts +66 -0
- package/src/channel.ts +45 -0
- package/src/context.ts +604 -0
- package/src/duration.ts +50 -0
- package/src/errors.ts +164 -0
- package/src/index.ts +26 -0
- package/src/json.ts +23 -0
- package/src/logger.ts +19 -0
- package/src/worker.ts +658 -0
- package/src/workflow.ts +20 -0
package/src/backoff.ts
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
export interface BackoffPolicy {
|
|
2
|
+
/** The first delay is drawn from [0, baseMs). */
|
|
3
|
+
readonly baseMs: number;
|
|
4
|
+
/** No delay exceeds this. */
|
|
5
|
+
readonly capMs: number;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Full jitter: a uniform draw from [0, min(cap, base × 2^attempt)). Many workers that lost
|
|
10
|
+
* the engine at the same moment spread their retries out instead of returning together.
|
|
11
|
+
*/
|
|
12
|
+
export function backoffDelay(
|
|
13
|
+
attempt: number,
|
|
14
|
+
policy: BackoffPolicy,
|
|
15
|
+
random: () => number = Math.random,
|
|
16
|
+
): number {
|
|
17
|
+
// 2^attempt overflows to Infinity for large attempts; min() keeps the result finite.
|
|
18
|
+
const ceiling = Math.min(policy.capMs, policy.baseMs * 2 ** attempt);
|
|
19
|
+
return Math.floor(random() * ceiling);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** Resolves after `ms`, or rejects with the signal's reason as soon as it aborts. */
|
|
23
|
+
export function sleep(ms: number, signal: AbortSignal): Promise<void> {
|
|
24
|
+
return new Promise((resolve, reject) => {
|
|
25
|
+
if (signal.aborted) {
|
|
26
|
+
reject(abortReason(signal));
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
29
|
+
const onAbort = () => {
|
|
30
|
+
clearTimeout(timer);
|
|
31
|
+
reject(abortReason(signal));
|
|
32
|
+
};
|
|
33
|
+
const timer = setTimeout(() => {
|
|
34
|
+
signal.removeEventListener("abort", onAbort);
|
|
35
|
+
resolve();
|
|
36
|
+
}, ms);
|
|
37
|
+
signal.addEventListener("abort", onAbort, { once: true });
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** Settles like `promise`, or rejects with the signal's reason if it aborts first. */
|
|
42
|
+
export function abortable<T>(promise: Promise<T>, signal: AbortSignal): Promise<T> {
|
|
43
|
+
if (signal.aborted) return Promise.reject(abortReason(signal));
|
|
44
|
+
return new Promise((resolve, reject) => {
|
|
45
|
+
const onAbort = () => {
|
|
46
|
+
reject(abortReason(signal));
|
|
47
|
+
};
|
|
48
|
+
signal.addEventListener("abort", onAbort, { once: true });
|
|
49
|
+
promise.then(
|
|
50
|
+
(value) => {
|
|
51
|
+
signal.removeEventListener("abort", onAbort);
|
|
52
|
+
resolve(value);
|
|
53
|
+
},
|
|
54
|
+
(error: unknown) => {
|
|
55
|
+
signal.removeEventListener("abort", onAbort);
|
|
56
|
+
reject(error instanceof Error ? error : new Error(String(error)));
|
|
57
|
+
},
|
|
58
|
+
);
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** The SDK always aborts with an Error; anything else is wrapped so callers get one. */
|
|
63
|
+
function abortReason(signal: AbortSignal): Error {
|
|
64
|
+
const reason: unknown = signal.reason;
|
|
65
|
+
return reason instanceof Error ? reason : new Error(String(reason));
|
|
66
|
+
}
|
package/src/channel.ts
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* An unbounded in-memory queue read as an async iterable: producers push, one consumer
|
|
3
|
+
* iterates. The worker's outgoing stream: runs, the heartbeat timer and the pull logic all
|
|
4
|
+
* push requests into it. Producers bound what they push; the channel does not.
|
|
5
|
+
*/
|
|
6
|
+
export class Channel<T> implements AsyncIterable<T> {
|
|
7
|
+
#items: T[] = [];
|
|
8
|
+
#closed = false;
|
|
9
|
+
#error: Error | undefined;
|
|
10
|
+
#wake: (() => void) | undefined;
|
|
11
|
+
|
|
12
|
+
/** Queues an item; returns false, dropping it, once the channel is closed. */
|
|
13
|
+
push(item: T): boolean {
|
|
14
|
+
if (this.#closed) return false;
|
|
15
|
+
this.#items.push(item);
|
|
16
|
+
this.#wake?.();
|
|
17
|
+
return true;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/** Ends the iteration after the queued items; with an error, the iterator then throws it. */
|
|
21
|
+
close(error?: Error): void {
|
|
22
|
+
if (this.#closed) return;
|
|
23
|
+
this.#closed = true;
|
|
24
|
+
this.#error = error;
|
|
25
|
+
this.#wake?.();
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
async *[Symbol.asyncIterator](): AsyncIterator<T> {
|
|
29
|
+
for (;;) {
|
|
30
|
+
const item = this.#items.shift();
|
|
31
|
+
if (item !== undefined) {
|
|
32
|
+
yield item;
|
|
33
|
+
continue;
|
|
34
|
+
}
|
|
35
|
+
if (this.#closed) {
|
|
36
|
+
if (this.#error !== undefined) throw this.#error;
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
await new Promise<void>((resolve) => {
|
|
40
|
+
this.#wake = resolve;
|
|
41
|
+
});
|
|
42
|
+
this.#wake = undefined;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
}
|