@anatta.io/tva 0.1.0 → 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/README.md +1 -1
- package/dist/adapters/cloudflare/index.d.ts +3 -3
- package/dist/adapters/cloudflare/index.js +2 -2
- package/dist/adapters/cloudflare/journal.d.ts +14 -11
- package/dist/adapters/cloudflare/journal.js +43 -23
- package/dist/adapters/cloudflare/{jobs.d.ts → pipelines.d.ts} +23 -23
- package/dist/adapters/cloudflare/{jobs.js → pipelines.js} +21 -21
- package/dist/adapters/cloudflare/workflow-pipelines.d.ts +66 -0
- package/dist/adapters/cloudflare/{workflow-jobs.js → workflow-pipelines.js} +46 -42
- package/dist/adapters/journal-records.d.ts +2 -1
- package/dist/adapters/journal-records.js +3 -2
- package/dist/adapters/memory/index.d.ts +1 -1
- package/dist/adapters/memory/index.js +1 -1
- package/dist/adapters/memory/journal.d.ts +5 -4
- package/dist/adapters/memory/journal.js +32 -13
- package/dist/adapters/memory/manifest.d.ts +1 -1
- package/dist/adapters/memory/manifest.js +1 -1
- package/dist/adapters/memory/{jobs.d.ts → pipelines.d.ts} +16 -16
- package/dist/adapters/memory/{jobs.js → pipelines.js} +6 -6
- package/dist/adapters/postgres/journal.d.ts +14 -11
- package/dist/adapters/postgres/journal.js +46 -23
- package/dist/adapters/postgres/manifest.d.ts +1 -1
- package/dist/adapters/postgres/manifest.js +1 -1
- package/dist/adapters/reference-executor.d.ts +26 -26
- package/dist/adapters/reference-executor.js +62 -57
- package/dist/contracts/index.d.ts +1 -1
- package/dist/contracts/index.js +1 -1
- package/dist/contracts/journal.d.ts +26 -16
- package/dist/contracts/manifest.d.ts +2 -2
- package/dist/contracts/{jobs.d.ts → pipelines.d.ts} +64 -51
- package/dist/pipelines/index.d.ts +1 -0
- package/dist/pipelines/index.js +2 -0
- package/dist/pipelines/pipeline.d.ts +142 -0
- package/dist/pipelines/pipeline.js +140 -0
- package/dist/pipelines/use-case-step.d.ts +24 -0
- package/dist/pipelines/use-case-step.js +34 -0
- package/dist/use-cases/index.d.ts +3 -0
- package/dist/use-cases/index.js +3 -0
- package/dist/use-cases/run-use-case.d.ts +33 -0
- package/dist/{actions/run-action.js → use-cases/run-use-case.js} +22 -18
- package/dist/{actions/action-consumer.d.ts → use-cases/use-case-consumer.d.ts} +11 -12
- package/dist/use-cases/use-case-consumer.js +98 -0
- package/dist/use-cases/use-case.d.ts +116 -0
- package/dist/use-cases/use-case.js +75 -0
- package/dist/webhooks/create-webhook-handler.d.ts +6 -6
- package/dist/webhooks/create-webhook-handler.js +32 -25
- package/dist/webhooks/dead-letter-consumer.d.ts +1 -1
- package/dist/webhooks/dead-letter-consumer.js +5 -5
- package/dist/webhooks/define-registry.d.ts +13 -13
- package/dist/webhooks/define-registry.js +15 -15
- package/dist/webhooks/once.d.ts +3 -3
- package/dist/webhooks/once.js +5 -5
- package/dist/webhooks/redact-customer-bodies.d.ts +1 -1
- package/dist/webhooks/redact-customer-bodies.js +1 -1
- package/dist/webhooks/replay-webhooks.d.ts +8 -8
- package/dist/webhooks/replay-webhooks.js +29 -26
- package/dist/webhooks/sweep-stuck-tasks.d.ts +32 -0
- package/dist/webhooks/sweep-stuck-tasks.js +73 -0
- package/dist/webhooks/webhook-consumer.d.ts +19 -18
- package/dist/webhooks/webhook-consumer.js +29 -24
- package/migrations/0012_rename_webhook_task_action_to_use_case.sql +11 -0
- package/migrations/0013_rename_job_tables_to_pipeline.sql +24 -0
- package/migrations/0016_add_webhook_task_requeues.sql +3 -0
- package/migrations/postgres/0012_rename_webhook_task_action_to_use_case.sql +5 -0
- package/migrations/postgres/0013_rename_job_tables_to_pipeline.sql +22 -0
- package/migrations/postgres/0016_add_webhook_task_requeues.sql +3 -0
- package/package.json +13 -6
- package/dist/actions/action-consumer.js +0 -90
- package/dist/actions/action.d.ts +0 -67
- package/dist/actions/action.js +0 -12
- package/dist/actions/index.d.ts +0 -3
- package/dist/actions/index.js +0 -4
- package/dist/actions/run-action.d.ts +0 -31
- package/dist/adapters/cloudflare/workflow-jobs.d.ts +0 -57
- /package/dist/contracts/{jobs.js → pipelines.js} +0 -0
|
@@ -1,34 +1,34 @@
|
|
|
1
1
|
import type { QueueBatch, QueueConsumer, QueueMessage, Toolkit } from '../contracts/index.js';
|
|
2
|
-
import type {
|
|
3
|
-
import type { JournalDeps } from '../
|
|
4
|
-
import { type
|
|
2
|
+
import type { UseCase } from '../use-cases/use-case.js';
|
|
3
|
+
import type { JournalDeps } from '../use-cases/run-use-case.js';
|
|
4
|
+
import { type UseCaseConsumerOptions } from '../use-cases/use-case-consumer.js';
|
|
5
5
|
import { type WebhookMessage } from './create-webhook-handler.js';
|
|
6
6
|
import type { WebhookRegistry } from './define-registry.js';
|
|
7
|
-
export type WebhookConsumerOptions<Deps> = Omit<
|
|
7
|
+
export type WebhookConsumerOptions<Deps> = Omit<UseCaseConsumerOptions<Deps>, 'job' | 'toolkit'> & {
|
|
8
8
|
/** Also reads each raw body from `blobStore`, where the edge wrote it. */
|
|
9
9
|
readonly toolkit: JournalDeps & Pick<Toolkit, 'blobStore'>;
|
|
10
10
|
/** The same registry the edge takes. Its entries may override `retryDelaySeconds` and `maxAttempts`. */
|
|
11
11
|
readonly registry: WebhookRegistry<Deps>;
|
|
12
12
|
};
|
|
13
|
-
/** The consumer's options, with one
|
|
13
|
+
/** The consumer's options, with one use case's retry settings applied. */
|
|
14
14
|
export type WebhookSettleOptions<Deps> = Omit<WebhookConsumerOptions<Deps>, 'registry'> & {
|
|
15
|
-
/** The queue's own limit; `maxAttempts` is the
|
|
15
|
+
/** The queue's own limit; `maxAttempts` is the use case's, at most this. */
|
|
16
16
|
readonly queueMaxAttempts: number;
|
|
17
17
|
};
|
|
18
|
-
/** One topic's
|
|
19
|
-
export type
|
|
20
|
-
/** The
|
|
18
|
+
/** One topic's use case, with the parse that turns a raw body into its input. */
|
|
19
|
+
export type WebhookUseCase<Deps> = {
|
|
20
|
+
/** The use case's name, which names its task in the delivery's receipt. */
|
|
21
21
|
readonly name: string;
|
|
22
22
|
readonly settle: (batch: QueueBatch<WebhookMessage>, message: QueueMessage<WebhookMessage>, options: WebhookSettleOptions<Deps>) => Promise<void>;
|
|
23
23
|
};
|
|
24
24
|
/**
|
|
25
|
-
* Binds `
|
|
25
|
+
* Binds `useCase` to its `parse`. A missing body or a throw from `parse` is a
|
|
26
26
|
* fault: the message is retried and, after the last attempt, dead-lettered
|
|
27
27
|
* with no unit.
|
|
28
28
|
*
|
|
29
29
|
* Moves the delivery's task in the journal around the run: `processing`
|
|
30
30
|
* first, then `done` when the message is acked, `failed` when it is retried,
|
|
31
|
-
* or `dead` on the
|
|
31
|
+
* or `dead` on the use case's last attempt, the latter two with the reason:
|
|
32
32
|
* the last retryable reason code, or `fault` for a throw. A dead task's message is retried
|
|
33
33
|
* only on the queue's last attempt too, so the queue dead-letters it, and is
|
|
34
34
|
* acked before that. These are required writes, so one
|
|
@@ -36,16 +36,17 @@ export type WebhookAction<Deps> = {
|
|
|
36
36
|
* to `processing`, because it is already `done` or was never receipted, is
|
|
37
37
|
* logged and acked without running.
|
|
38
38
|
*/
|
|
39
|
-
export declare function
|
|
39
|
+
export declare function webhookUseCase<Input, Reason extends string, Deps>(useCase: UseCase<Input, Reason, Deps>, parse: (body: string, message: WebhookMessage) => Input): WebhookUseCase<Deps>;
|
|
40
40
|
/**
|
|
41
|
-
* The consumer of `toolkit.webhookQueue`: each message runs the one
|
|
42
|
-
* names and is acked or retried on its own (`
|
|
43
|
-
*
|
|
44
|
-
* whose
|
|
45
|
-
* §7.1. Throws when
|
|
41
|
+
* The consumer of `toolkit.webhookQueue`: each message runs the one use case it
|
|
42
|
+
* names and is acked or retried on its own (`settleUseCaseMessage`), so one
|
|
43
|
+
* use case failing does not hold back another for the same delivery. A message
|
|
44
|
+
* whose use case is not registered is logged and acked, never retried: design
|
|
45
|
+
* §7.1. Throws when a use case's `maxAttempts` is above the queue's.
|
|
46
46
|
*/
|
|
47
47
|
export declare function createWebhookConsumer<Deps>({ registry, ...options }: WebhookConsumerOptions<Deps>): QueueConsumer<WebhookMessage>;
|
|
48
48
|
export type { WebhookMessage } from './create-webhook-handler.js';
|
|
49
|
-
export { defineRegistry,
|
|
49
|
+
export { defineRegistry, WEBHOOK_MAX_USE_CASES_PER_TOPIC, type WebhookRegistration, type WebhookRegistry, type WebhookTopicSettings, } from './define-registry.js';
|
|
50
50
|
export { createDeadLetterConsumer } from './dead-letter-consumer.js';
|
|
51
51
|
export { replayWebhooks, type ReplayDeps, type ReplayOutcome, type ReplayResult, } from './replay-webhooks.js';
|
|
52
|
+
export { DEFAULT_MAX_REQUEUES, DEFAULT_STUCK_AFTER_SECONDS, sweepStuckTasks, type SweepStuckTasksDeps, type SweepStuckTasksOptions, type SweepStuckTasksResult, } from './sweep-stuck-tasks.js';
|
|
@@ -1,13 +1,13 @@
|
|
|
1
|
-
import { retryDelayFor,
|
|
1
|
+
import { retryDelayFor, settleUseCaseMessage, } from '../use-cases/use-case-consumer.js';
|
|
2
2
|
import { webhookDeliveryId, } from './create-webhook-handler.js';
|
|
3
3
|
/**
|
|
4
|
-
* Binds `
|
|
4
|
+
* Binds `useCase` to its `parse`. A missing body or a throw from `parse` is a
|
|
5
5
|
* fault: the message is retried and, after the last attempt, dead-lettered
|
|
6
6
|
* with no unit.
|
|
7
7
|
*
|
|
8
8
|
* Moves the delivery's task in the journal around the run: `processing`
|
|
9
9
|
* first, then `done` when the message is acked, `failed` when it is retried,
|
|
10
|
-
* or `dead` on the
|
|
10
|
+
* or `dead` on the use case's last attempt, the latter two with the reason:
|
|
11
11
|
* the last retryable reason code, or `fault` for a throw. A dead task's message is retried
|
|
12
12
|
* only on the queue's last attempt too, so the queue dead-letters it, and is
|
|
13
13
|
* acked before that. These are required writes, so one
|
|
@@ -15,14 +15,18 @@ import { webhookDeliveryId, } from './create-webhook-handler.js';
|
|
|
15
15
|
* to `processing`, because it is already `done` or was never receipted, is
|
|
16
16
|
* logged and acked without running.
|
|
17
17
|
*/
|
|
18
|
-
export function
|
|
18
|
+
export function webhookUseCase(useCase, parse) {
|
|
19
19
|
return {
|
|
20
|
-
name:
|
|
20
|
+
name: useCase.name,
|
|
21
21
|
settle: async (batch, message, options) => {
|
|
22
22
|
const { journal, clock, logger } = options.toolkit;
|
|
23
23
|
const deliveryId = webhookDeliveryId(message.body);
|
|
24
|
-
const context = {
|
|
25
|
-
|
|
24
|
+
const context = {
|
|
25
|
+
deliveryId,
|
|
26
|
+
useCase: useCase.name,
|
|
27
|
+
messageId: message.id,
|
|
28
|
+
};
|
|
29
|
+
const move = async (to, reasonCode) => journal.moveTask(deliveryId, useCase.name, to, clock.now(), reasonCode);
|
|
26
30
|
const retry = () => {
|
|
27
31
|
batch.retry(message.id, {
|
|
28
32
|
delaySeconds: retryDelayFor(message.attempt, options.retryDelaySeconds),
|
|
@@ -45,7 +49,7 @@ export function webhookAction(action, parse) {
|
|
|
45
49
|
}
|
|
46
50
|
// Held until the receipt says how the message was settled.
|
|
47
51
|
let settled;
|
|
48
|
-
const reason = await
|
|
52
|
+
const reason = await settleUseCaseMessage({
|
|
49
53
|
messages: batch.messages,
|
|
50
54
|
ack: () => {
|
|
51
55
|
settled = {};
|
|
@@ -53,13 +57,13 @@ export function webhookAction(action, parse) {
|
|
|
53
57
|
retry: (_id, retryOptions) => {
|
|
54
58
|
settled = { retry: retryOptions };
|
|
55
59
|
},
|
|
56
|
-
}, message,
|
|
60
|
+
}, message, useCase, async (body) => {
|
|
57
61
|
const bytes = await options.toolkit.blobStore.get(body.bodyKey);
|
|
58
62
|
if (bytes === undefined) {
|
|
59
63
|
throw new Error(`webhook body missing: ${body.bodyKey}`);
|
|
60
64
|
}
|
|
61
65
|
return parse(new TextDecoder().decode(bytes), body);
|
|
62
|
-
},
|
|
66
|
+
}, options);
|
|
63
67
|
const acked = settled !== undefined && !('retry' in settled);
|
|
64
68
|
const dead = !acked && message.attempt >= options.maxAttempts;
|
|
65
69
|
try {
|
|
@@ -83,26 +87,26 @@ export function webhookAction(action, parse) {
|
|
|
83
87
|
};
|
|
84
88
|
}
|
|
85
89
|
/**
|
|
86
|
-
* The consumer of `toolkit.webhookQueue`: each message runs the one
|
|
87
|
-
* names and is acked or retried on its own (`
|
|
88
|
-
*
|
|
89
|
-
* whose
|
|
90
|
-
* §7.1. Throws when
|
|
90
|
+
* The consumer of `toolkit.webhookQueue`: each message runs the one use case it
|
|
91
|
+
* names and is acked or retried on its own (`settleUseCaseMessage`), so one
|
|
92
|
+
* use case failing does not hold back another for the same delivery. A message
|
|
93
|
+
* whose use case is not registered is logged and acked, never retried: design
|
|
94
|
+
* §7.1. Throws when a use case's `maxAttempts` is above the queue's.
|
|
91
95
|
*/
|
|
92
96
|
export function createWebhookConsumer({ registry, ...options }) {
|
|
93
|
-
for (const { source, topic,
|
|
97
|
+
for (const { source, topic, useCase, maxAttempts } of registry.useCases) {
|
|
94
98
|
if (maxAttempts !== undefined && maxAttempts > options.maxAttempts) {
|
|
95
|
-
throw new TypeError(`webhooks: ${source} ${topic} ${
|
|
99
|
+
throw new TypeError(`webhooks: ${source} ${topic} ${useCase.name} maxAttempts (${maxAttempts}) is above the queue's (${options.maxAttempts})`);
|
|
96
100
|
}
|
|
97
101
|
}
|
|
98
102
|
return async (batch) => {
|
|
99
103
|
for (const message of batch.messages) {
|
|
100
|
-
const { source, topic, shop, webhookId,
|
|
104
|
+
const { source, topic, shop, webhookId, useCase } = message.body;
|
|
101
105
|
const registration = registry
|
|
102
|
-
.
|
|
103
|
-
.find((entry) => entry.
|
|
106
|
+
.useCasesFor(source, topic)
|
|
107
|
+
.find((entry) => entry.useCase.name === useCase);
|
|
104
108
|
if (registration !== undefined) {
|
|
105
|
-
await registration.
|
|
109
|
+
await registration.useCase.settle(batch, message, {
|
|
106
110
|
...options,
|
|
107
111
|
retryDelaySeconds: registration.retryDelaySeconds ?? options.retryDelaySeconds,
|
|
108
112
|
maxAttempts: registration.maxAttempts ?? options.maxAttempts,
|
|
@@ -110,10 +114,10 @@ export function createWebhookConsumer({ registry, ...options }) {
|
|
|
110
114
|
});
|
|
111
115
|
continue;
|
|
112
116
|
}
|
|
113
|
-
options.toolkit.logger.warn('webhook
|
|
117
|
+
options.toolkit.logger.warn('webhook use case not registered', {
|
|
114
118
|
source,
|
|
115
119
|
topic,
|
|
116
|
-
|
|
120
|
+
useCase,
|
|
117
121
|
shop,
|
|
118
122
|
webhookId,
|
|
119
123
|
messageId: message.id,
|
|
@@ -122,6 +126,7 @@ export function createWebhookConsumer({ registry, ...options }) {
|
|
|
122
126
|
}
|
|
123
127
|
};
|
|
124
128
|
}
|
|
125
|
-
export { defineRegistry,
|
|
129
|
+
export { defineRegistry, WEBHOOK_MAX_USE_CASES_PER_TOPIC, } from './define-registry.js';
|
|
126
130
|
export { createDeadLetterConsumer } from './dead-letter-consumer.js';
|
|
127
131
|
export { replayWebhooks, } from './replay-webhooks.js';
|
|
132
|
+
export { DEFAULT_MAX_REQUEUES, DEFAULT_STUCK_AFTER_SECONDS, sweepStuckTasks, } from './sweep-stuck-tasks.js';
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
-- A task's use case column was `action`, the word React Router uses for a
|
|
2
|
+
-- route's action. The value, the use case's name, is unchanged.
|
|
3
|
+
-- RENAME COLUMN rewrites the primary key and both indexes in place. SQLite
|
|
4
|
+
-- cannot rename an index, so the per-use-case index is dropped and created
|
|
5
|
+
-- again under its new name.
|
|
6
|
+
ALTER TABLE webhook_tasks RENAME COLUMN action TO use_case;
|
|
7
|
+
|
|
8
|
+
DROP INDEX webhook_tasks_action_state;
|
|
9
|
+
|
|
10
|
+
-- Serves deadTasks({ useCase }).
|
|
11
|
+
CREATE INDEX webhook_tasks_use_case_state ON webhook_tasks (use_case, state, updated_at, delivery_id);
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
-- Jobs are now Pipelines (decision 0014). The reference executor's two
|
|
2
|
+
-- tables, their indexes and the instance's definition-name column follow.
|
|
3
|
+
-- Every row and value is unchanged.
|
|
4
|
+
--
|
|
5
|
+
-- SQLite rewrites the indexes and the CHECK constraints to the new table name
|
|
6
|
+
-- in place. It cannot rename an index, so both are dropped and created again
|
|
7
|
+
-- under their new names. The Postgres twin is
|
|
8
|
+
-- postgres/0013_rename_job_tables_to_pipeline.sql.
|
|
9
|
+
ALTER TABLE job_instances RENAME TO pipeline_instances;
|
|
10
|
+
|
|
11
|
+
ALTER TABLE job_steps RENAME TO pipeline_steps;
|
|
12
|
+
|
|
13
|
+
-- The definition's name. `job` would read as RunRow.job, which is a run's label.
|
|
14
|
+
ALTER TABLE pipeline_instances RENAME COLUMN job TO pipeline;
|
|
15
|
+
|
|
16
|
+
DROP INDEX job_instances_due;
|
|
17
|
+
|
|
18
|
+
DROP INDEX job_instances_ended_at;
|
|
19
|
+
|
|
20
|
+
-- Serves the sweeper's due() read, as job_instances_due did.
|
|
21
|
+
CREATE INDEX pipeline_instances_due ON pipeline_instances (status, next_wake_at);
|
|
22
|
+
|
|
23
|
+
-- Serves the sweeper's retention delete, as job_instances_ended_at did.
|
|
24
|
+
CREATE INDEX pipeline_instances_ended_at ON pipeline_instances (ended_at);
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
-- The Postgres twin of ../0012_rename_webhook_task_action_to_use_case.sql,
|
|
2
|
+
-- which says why. Postgres renames the index in place.
|
|
3
|
+
ALTER TABLE webhook_tasks RENAME COLUMN action TO use_case;
|
|
4
|
+
|
|
5
|
+
ALTER INDEX webhook_tasks_action_state RENAME TO webhook_tasks_use_case_state;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
-- The Postgres twin of ../0013_rename_job_tables_to_pipeline.sql, which says
|
|
2
|
+
-- why. Postgres renames the indexes in place, and keeps the generated
|
|
3
|
+
-- constraint names unless they are renamed too, so they are.
|
|
4
|
+
ALTER TABLE job_instances RENAME TO pipeline_instances;
|
|
5
|
+
|
|
6
|
+
ALTER TABLE job_steps RENAME TO pipeline_steps;
|
|
7
|
+
|
|
8
|
+
ALTER TABLE pipeline_instances RENAME COLUMN job TO pipeline;
|
|
9
|
+
|
|
10
|
+
ALTER INDEX job_instances_due RENAME TO pipeline_instances_due;
|
|
11
|
+
|
|
12
|
+
ALTER INDEX job_instances_ended_at RENAME TO pipeline_instances_ended_at;
|
|
13
|
+
|
|
14
|
+
ALTER TABLE pipeline_instances RENAME CONSTRAINT job_instances_pkey TO pipeline_instances_pkey;
|
|
15
|
+
|
|
16
|
+
ALTER TABLE pipeline_instances RENAME CONSTRAINT job_instances_status_check TO pipeline_instances_status_check;
|
|
17
|
+
|
|
18
|
+
ALTER TABLE pipeline_instances RENAME CONSTRAINT job_instances_cause_check TO pipeline_instances_cause_check;
|
|
19
|
+
|
|
20
|
+
ALTER TABLE pipeline_instances RENAME CONSTRAINT job_instances_check TO pipeline_instances_check;
|
|
21
|
+
|
|
22
|
+
ALTER TABLE pipeline_steps RENAME CONSTRAINT job_steps_pkey TO pipeline_steps_pkey;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@anatta.io/tva",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "The platform primitives a Shopify React Router app does not come with",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/contracts/index.js",
|
|
@@ -46,10 +46,15 @@
|
|
|
46
46
|
"types": "./dist/webhooks/webhook-consumer.d.ts",
|
|
47
47
|
"default": "./dist/webhooks/webhook-consumer.js"
|
|
48
48
|
},
|
|
49
|
-
"./
|
|
50
|
-
"@anatta.io/source": "./src/
|
|
51
|
-
"types": "./dist/
|
|
52
|
-
"default": "./dist/
|
|
49
|
+
"./use-cases": {
|
|
50
|
+
"@anatta.io/source": "./src/use-cases/index.ts",
|
|
51
|
+
"types": "./dist/use-cases/index.d.ts",
|
|
52
|
+
"default": "./dist/use-cases/index.js"
|
|
53
|
+
},
|
|
54
|
+
"./pipelines": {
|
|
55
|
+
"@anatta.io/source": "./src/pipelines/index.ts",
|
|
56
|
+
"types": "./dist/pipelines/index.d.ts",
|
|
57
|
+
"default": "./dist/pipelines/index.js"
|
|
53
58
|
},
|
|
54
59
|
"./webhooks/edge": {
|
|
55
60
|
"@anatta.io/source": "./src/webhooks/create-webhook-handler.ts",
|
|
@@ -73,9 +78,10 @@
|
|
|
73
78
|
"!migrations/**/*.md"
|
|
74
79
|
],
|
|
75
80
|
"publishConfig": {
|
|
76
|
-
"access": "
|
|
81
|
+
"access": "public"
|
|
77
82
|
},
|
|
78
83
|
"workspaces": [
|
|
84
|
+
".",
|
|
79
85
|
"examples/cloudflare",
|
|
80
86
|
"examples/cyber-onboarding"
|
|
81
87
|
],
|
|
@@ -120,6 +126,7 @@
|
|
|
120
126
|
},
|
|
121
127
|
"devDependencies": {
|
|
122
128
|
"@aws-sdk/client-s3": "^3.1155.0",
|
|
129
|
+
"@changesets/cli": "^3.0.3",
|
|
123
130
|
"@cloudflare/vitest-pool-workers": "0.22.0",
|
|
124
131
|
"@shopify/shopify-api": "^15.0.0",
|
|
125
132
|
"@shopify/shopify-app-session-storage": "^7.0.0",
|
|
@@ -1,90 +0,0 @@
|
|
|
1
|
-
import { failed } from './action.js';
|
|
2
|
-
import { runAction, withRun } from './run-action.js';
|
|
3
|
-
// Cloudflare's queueDelayMaxSeconds, the lowest of the adapters'.
|
|
4
|
-
const MAX_RETRY_DELAY_SECONDS = 86_400;
|
|
5
|
-
/**
|
|
6
|
-
* The queue transport for an Action: one message, one run, one unit.
|
|
7
|
-
*
|
|
8
|
-
* A decision is acked unless it is `retryable`. A throw, a failed journal
|
|
9
|
-
* write among them, is logged and retried. Either way the message is settled
|
|
10
|
-
* on its own, and the rest of the batch is not run again.
|
|
11
|
-
*
|
|
12
|
-
* A run per message, not per batch: at-least-once delivery can put two
|
|
13
|
-
* messages for one unit in a batch, and in one run the second would trip
|
|
14
|
-
* `runAction`'s one-unit-per-run rule and spend a retry.
|
|
15
|
-
*/
|
|
16
|
-
export function createActionConsumer(action, options) {
|
|
17
|
-
return async (batch) => {
|
|
18
|
-
for (const message of batch.messages) {
|
|
19
|
-
await settleActionMessage(batch, message, action, (body) => body, options);
|
|
20
|
-
}
|
|
21
|
-
};
|
|
22
|
-
}
|
|
23
|
-
/**
|
|
24
|
-
* Runs `action` on one message and settles it. On the last attempt a
|
|
25
|
-
* retryable decision or a throw is recorded as `failed`, `retries-exhausted`,
|
|
26
|
-
* and the message is still retried, so the queue dead-letters it.
|
|
27
|
-
*
|
|
28
|
-
* Resolves to why it retried: the last retryable reason code, or `fault` for
|
|
29
|
-
* a throw. Undefined when it acked.
|
|
30
|
-
*/
|
|
31
|
-
export async function settleActionMessage(batch, message, action, toInput, { toolkit, deps, job, retryDelaySeconds, maxAttempts, }) {
|
|
32
|
-
const runId = crypto.randomUUID();
|
|
33
|
-
const exhausted = { yes: false };
|
|
34
|
-
const retry = () => {
|
|
35
|
-
batch.retry(message.id, {
|
|
36
|
-
delaySeconds: retryDelayFor(message.attempt, retryDelaySeconds),
|
|
37
|
-
});
|
|
38
|
-
};
|
|
39
|
-
try {
|
|
40
|
-
const decision = await withRun(toolkit, { runId, job, trigger: 'queue', mode: 'live' }, async (scope) => runAction(message.attempt >= maxAttempts
|
|
41
|
-
? exhaustRetries(action, exhausted)
|
|
42
|
-
: action, await toInput(message.body), scope, deps));
|
|
43
|
-
if (exhausted.yes) {
|
|
44
|
-
retry();
|
|
45
|
-
return exhausted.reason;
|
|
46
|
-
}
|
|
47
|
-
if ('retryable' in decision && decision.retryable === true) {
|
|
48
|
-
retry();
|
|
49
|
-
return decision.reasonCode;
|
|
50
|
-
}
|
|
51
|
-
batch.ack(message.id);
|
|
52
|
-
return undefined;
|
|
53
|
-
}
|
|
54
|
-
catch (error) {
|
|
55
|
-
toolkit.logger.error('action failed', {
|
|
56
|
-
action: action.name,
|
|
57
|
-
runId,
|
|
58
|
-
messageId: message.id,
|
|
59
|
-
error: String(error),
|
|
60
|
-
});
|
|
61
|
-
retry();
|
|
62
|
-
return 'fault';
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
/** The delay before `attempt`'s retry: `retryDelaySeconds` doubled per attempt, up to a day. */
|
|
66
|
-
export const retryDelayFor = (attempt, retryDelaySeconds) => Math.min(retryDelaySeconds * 2 ** (attempt - 1), MAX_RETRY_DELAY_SECONDS);
|
|
67
|
-
/** `action`, with a retryable decision or a throw turned into `retries-exhausted`. */
|
|
68
|
-
function exhaustRetries(action, exhausted) {
|
|
69
|
-
return {
|
|
70
|
-
...action,
|
|
71
|
-
async run(input, context) {
|
|
72
|
-
let lastReason;
|
|
73
|
-
try {
|
|
74
|
-
const decision = await action.run(input, context);
|
|
75
|
-
if (!('retryable' in decision && decision.retryable === true)) {
|
|
76
|
-
return decision;
|
|
77
|
-
}
|
|
78
|
-
lastReason = decision.reasonCode;
|
|
79
|
-
exhausted.reason = decision.reasonCode;
|
|
80
|
-
}
|
|
81
|
-
catch (error) {
|
|
82
|
-
lastReason = String(error);
|
|
83
|
-
exhausted.reason = 'fault';
|
|
84
|
-
}
|
|
85
|
-
exhausted.yes = true;
|
|
86
|
-
context.logger.error('retries exhausted', { lastReason });
|
|
87
|
-
return failed('retries-exhausted');
|
|
88
|
-
},
|
|
89
|
-
};
|
|
90
|
-
}
|
package/dist/actions/action.d.ts
DELETED
|
@@ -1,67 +0,0 @@
|
|
|
1
|
-
import type { Logger, UnitKey } from '../contracts/index.js';
|
|
2
|
-
/**
|
|
3
|
-
* AN ACTION TAKES TYPED INPUT AND RETURNS A DECISION: what the system chose to
|
|
4
|
-
* do with one unit, and why. It never writes to the journal. `runAction` is
|
|
5
|
-
* the only way to call one, and it records the decision, so the record does
|
|
6
|
-
* not depend on the author remembering to write it (#94).
|
|
7
|
-
*
|
|
8
|
-
* A definition value, like a `Job`, not a base class to extend. A class that
|
|
9
|
-
* `implements Action` is fine.
|
|
10
|
-
*
|
|
11
|
-
* An Action runs inline and reaches one outcome per unit. Work that needs
|
|
12
|
-
* retries between its steps, or must survive a restart part way through, is a
|
|
13
|
-
* `Job`.
|
|
14
|
-
*
|
|
15
|
-
* Cleanup happens inside `run`: free what it claimed, then return the
|
|
16
|
-
* decision.
|
|
17
|
-
*/
|
|
18
|
-
export type Action<Input, Reason extends string, Deps> = {
|
|
19
|
-
/** Added to every log line the Action writes. */
|
|
20
|
-
readonly name: string;
|
|
21
|
-
/** The journal's `unitType`, for example `order`. */
|
|
22
|
-
readonly unitType: string;
|
|
23
|
-
/** The journal's unit key. A pure function of the input. */
|
|
24
|
-
readonly key: (input: Input) => UnitKey;
|
|
25
|
-
run(input: Input, context: ActionContext<Deps>): Promise<Decision<Reason>>;
|
|
26
|
-
};
|
|
27
|
-
export type ActionContext<Deps> = {
|
|
28
|
-
readonly deps: Deps;
|
|
29
|
-
/** Adds `action`, `runId` and `unitKey` to every line. */
|
|
30
|
-
readonly logger: Logger;
|
|
31
|
-
/**
|
|
32
|
-
* Calls `fn` and records it as an `ok` or `failed` call with its duration.
|
|
33
|
-
* Returns what `fn` returns, and rethrows what it throws. A property, not a
|
|
34
|
-
* method, so an Action can destructure it from the context.
|
|
35
|
-
*/
|
|
36
|
-
readonly track: <T>(system: string, operation: string, fn: () => Promise<T>) => Promise<T>;
|
|
37
|
-
};
|
|
38
|
-
type DecisionExtras = {
|
|
39
|
-
readonly externalIds?: Readonly<Record<string, string>>;
|
|
40
|
-
};
|
|
41
|
-
type RetryableExtras = DecisionExtras & {
|
|
42
|
-
/**
|
|
43
|
-
* Try the unit again later: a queue retries the message, and an HTTP
|
|
44
|
-
* transport answers 500. Same word as `ToolFault.retryable`.
|
|
45
|
-
*/
|
|
46
|
-
readonly retryable?: boolean;
|
|
47
|
-
};
|
|
48
|
-
/**
|
|
49
|
-
* Every outcome but `written` carries a reason code from the domain's closed
|
|
50
|
-
* list, and only `failed` and `skipped` can be retryable: an `unchanged` unit
|
|
51
|
-
* has nothing left to do. A throw is not a decision: it is a fault, it records
|
|
52
|
-
* nothing, and the transport retries it.
|
|
53
|
-
*/
|
|
54
|
-
export type Decision<Reason extends string> = ({
|
|
55
|
-
readonly outcome: 'written';
|
|
56
|
-
} & DecisionExtras) | ({
|
|
57
|
-
readonly outcome: 'failed' | 'skipped';
|
|
58
|
-
readonly reasonCode: Reason;
|
|
59
|
-
} & RetryableExtras) | ({
|
|
60
|
-
readonly outcome: 'unchanged' | 'would-write';
|
|
61
|
-
readonly reasonCode: Reason;
|
|
62
|
-
} & DecisionExtras);
|
|
63
|
-
export declare function written(extras?: DecisionExtras): Decision<never>;
|
|
64
|
-
export declare function skipped<R extends string>(reasonCode: R, extras?: RetryableExtras): Decision<R>;
|
|
65
|
-
export declare function failed<R extends string>(reasonCode: R, extras?: RetryableExtras): Decision<R>;
|
|
66
|
-
export declare function unchanged<R extends string>(reasonCode: R, extras?: DecisionExtras): Decision<R>;
|
|
67
|
-
export {};
|
package/dist/actions/action.js
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
export function written(extras = {}) {
|
|
2
|
-
return { outcome: 'written', ...extras };
|
|
3
|
-
}
|
|
4
|
-
export function skipped(reasonCode, extras = {}) {
|
|
5
|
-
return { outcome: 'skipped', reasonCode, ...extras };
|
|
6
|
-
}
|
|
7
|
-
export function failed(reasonCode, extras = {}) {
|
|
8
|
-
return { outcome: 'failed', reasonCode, ...extras };
|
|
9
|
-
}
|
|
10
|
-
export function unchanged(reasonCode, extras = {}) {
|
|
11
|
-
return { outcome: 'unchanged', reasonCode, ...extras };
|
|
12
|
-
}
|
package/dist/actions/index.d.ts
DELETED
package/dist/actions/index.js
DELETED
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
import type { RunId, RunRow, Toolkit } from '../contracts/index.js';
|
|
2
|
-
import type { Action, Decision } from './action.js';
|
|
3
|
-
/** What the runner writes with. A `Toolkit` is one. */
|
|
4
|
-
export type JournalDeps = Pick<Toolkit, 'journal' | 'clock' | 'logger'>;
|
|
5
|
-
/** A started run. Only `withRun` makes one. */
|
|
6
|
-
export type RunScope = {
|
|
7
|
-
readonly runId: RunId;
|
|
8
|
-
};
|
|
9
|
-
/**
|
|
10
|
-
* Starts a run, calls `fn` with it, and finishes the run when `fn` settles.
|
|
11
|
-
* The run id is the caller's, or a new UUID.
|
|
12
|
-
*
|
|
13
|
-
* A failed `startRun` rejects before `fn` runs. A failed `finishRun` is logged,
|
|
14
|
-
* not thrown: the units are already recorded, and a rejection here would make
|
|
15
|
-
* a transport retry work that is done, for the sake of an end time.
|
|
16
|
-
*/
|
|
17
|
-
export declare function withRun<T>(deps: JournalDeps, run: Pick<RunRow, 'job' | 'trigger' | 'mode'> & {
|
|
18
|
-
readonly runId?: RunId;
|
|
19
|
-
}, fn: (scope: RunScope) => Promise<T>): Promise<T>;
|
|
20
|
-
/**
|
|
21
|
-
* The only way to call an Action. Times it, collects the calls it tracks, and
|
|
22
|
-
* records its decision as one unit of `scope`'s run.
|
|
23
|
-
*
|
|
24
|
-
* A throw from the Action records nothing and is rethrown: a fault is not a
|
|
25
|
-
* decision, and the transport decides what it means. A rejected `recordUnit`
|
|
26
|
-
* reaches the caller too, so a queue consumer retries the message.
|
|
27
|
-
*
|
|
28
|
-
* A unit runs at most once per run, and a second call for its key throws:
|
|
29
|
-
* recording it again would replace the first decision's row and calls.
|
|
30
|
-
*/
|
|
31
|
-
export declare function runAction<Input, Reason extends string, Deps>(action: Action<Input, Reason, Deps>, input: Input, scope: RunScope, deps: Deps): Promise<Decision<Reason>>;
|
|
@@ -1,57 +0,0 @@
|
|
|
1
|
-
import { WorkflowEntrypoint } from 'cloudflare:workers';
|
|
2
|
-
import type { JobRegistration, Jobs, JsonValue, RunId } from '../../contracts/index.js';
|
|
3
|
-
/**
|
|
4
|
-
* DURABLE JOBS ON CLOUDFLARE WORKFLOWS.
|
|
5
|
-
*
|
|
6
|
-
* Two halves, over one Workflow binding:
|
|
7
|
-
*
|
|
8
|
-
* - `workflowJobsEntrypoint` returns ONE GENERIC `WorkflowEntrypoint` class
|
|
9
|
-
* that runs every registered Job. It finds the Job from the instance id's
|
|
10
|
-
* prefix: a Job name has no `-`, so the id's first `-` ends the name
|
|
11
|
-
* (`JobInstanceId`). Export it from the Worker's main module and bind it as
|
|
12
|
-
* a Workflow.
|
|
13
|
-
* - `createWorkflowJobs` is the `Jobs` a route calls. It creates and reads
|
|
14
|
-
* instances through that binding.
|
|
15
|
-
*
|
|
16
|
-
* One binding serves every Job. Workflows scopes ids to a binding and
|
|
17
|
-
* `status(id)` takes no Job name, so a binding per Job would make `status`
|
|
18
|
-
* guess which binding to ask.
|
|
19
|
-
*
|
|
20
|
-
* THE CLASS HOLDS NO JOB LOGIC. Every step is one `step.do` with the step's
|
|
21
|
-
* own retries and timeout, and the exhaustion handler is one more. Deployed
|
|
22
|
-
* Workflows replays `run()` after a suspension and local Workflows does not
|
|
23
|
-
* (docs/spikes/cloudflare-workflows.md, "Three more findings"), so the code
|
|
24
|
-
* between the `step.do` calls only reads the event and what earlier
|
|
25
|
-
* `step.do` calls returned. A test that passes locally does not prove this.
|
|
26
|
-
*/
|
|
27
|
-
/** What an instance is created with. The id carries the Job name and key. */
|
|
28
|
-
export type WorkflowJobsParams = {
|
|
29
|
-
readonly input: JsonValue;
|
|
30
|
-
readonly runId: RunId;
|
|
31
|
-
};
|
|
32
|
-
/** The Workflow binding the executor creates and reads instances through. */
|
|
33
|
-
export type JobsWorkflow = Workflow<WorkflowJobsParams>;
|
|
34
|
-
export type WorkflowJobsEntrypointOptions = {
|
|
35
|
-
/** Defaults to `CLOUDFLARE_MANIFEST.stepResultMaxBytes`, 1 MiB. */
|
|
36
|
-
readonly stepResultMaxBytes?: number;
|
|
37
|
-
};
|
|
38
|
-
/**
|
|
39
|
-
* The generic Workflow class. `registrations` is called with the Worker's
|
|
40
|
-
* `env` in each invocation, so a deps builder can close over the bindings:
|
|
41
|
-
*
|
|
42
|
-
* export const JobsWorkflow = workflowJobsEntrypoint((env: Env) => [
|
|
43
|
-
* (register) => register(selfSignUp, () => createSignUpDeps(env)),
|
|
44
|
-
* ])
|
|
45
|
-
*
|
|
46
|
-
* It must return the same registrations `createWorkflowJobs` was given.
|
|
47
|
-
*/
|
|
48
|
-
export declare function workflowJobsEntrypoint<Env>(registrations: (env: Env) => readonly JobRegistration[], options?: WorkflowJobsEntrypointOptions): new (ctx: ExecutionContext, env: Env) => WorkflowEntrypoint<Env, WorkflowJobsParams>;
|
|
49
|
-
export type WorkflowJobsOptions = {
|
|
50
|
-
readonly workflow: JobsWorkflow;
|
|
51
|
-
readonly registrations: readonly JobRegistration[];
|
|
52
|
-
};
|
|
53
|
-
/**
|
|
54
|
-
* `Jobs` over the Workflow binding that `workflowJobsEntrypoint`'s class is
|
|
55
|
-
* bound to. Throws when it is built if a registration's name breaks the rule.
|
|
56
|
-
*/
|
|
57
|
-
export declare function createWorkflowJobs(options: WorkflowJobsOptions): Jobs;
|
|
File without changes
|