@temelj/workflow 0.0.0-alpha.1
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 +21 -0
- package/README.md +38 -0
- package/dist/attributes.d.mts +15 -0
- package/dist/attributes.mjs +30 -0
- package/dist/client/create.d.mts +31 -0
- package/dist/client/create.mjs +67 -0
- package/dist/client/facades.d.mts +21 -0
- package/dist/client/facades.mjs +142 -0
- package/dist/client/mod.d.mts +2 -0
- package/dist/client/mod.mjs +2 -0
- package/dist/client/run-handle.d.mts +66 -0
- package/dist/client/run-handle.mjs +156 -0
- package/dist/client/worker-options.d.mts +17 -0
- package/dist/client/worker-options.mjs +25 -0
- package/dist/collection.d.mts +15 -0
- package/dist/collection.mjs +33 -0
- package/dist/compiled-function.d.mts +27 -0
- package/dist/compiled-function.mjs +54 -0
- package/dist/definition.d.mts +37 -0
- package/dist/definition.mjs +83 -0
- package/dist/engine/child-workflow.d.mts +10 -0
- package/dist/engine/child-workflow.mjs +333 -0
- package/dist/engine/command-identity.d.mts +9 -0
- package/dist/engine/command-identity.mjs +14 -0
- package/dist/engine/create.d.mts +106 -0
- package/dist/engine/create.mjs +402 -0
- package/dist/engine/deterministic.d.mts +12 -0
- package/dist/engine/deterministic.mjs +102 -0
- package/dist/engine/durable-command.d.mts +10 -0
- package/dist/engine/durable-command.mjs +17 -0
- package/dist/engine/execution-api.d.mts +29 -0
- package/dist/engine/execution-api.mjs +33 -0
- package/dist/engine/execution-service.d.mts +41 -0
- package/dist/engine/execution-service.mjs +288 -0
- package/dist/engine/hooks.d.mts +56 -0
- package/dist/engine/hooks.mjs +237 -0
- package/dist/engine/inspection-api.d.mts +34 -0
- package/dist/engine/inspection-api.mjs +77 -0
- package/dist/engine/inspection.d.mts +18 -0
- package/dist/engine/inspection.mjs +149 -0
- package/dist/engine/lifecycle.d.mts +18 -0
- package/dist/engine/lifecycle.mjs +38 -0
- package/dist/engine/lock-replay.d.mts +30 -0
- package/dist/engine/lock-replay.mjs +38 -0
- package/dist/engine/lock.d.mts +21 -0
- package/dist/engine/lock.mjs +197 -0
- package/dist/engine/message.d.mts +47 -0
- package/dist/engine/message.mjs +357 -0
- package/dist/engine/mod.d.mts +4 -0
- package/dist/engine/mod.mjs +2 -0
- package/dist/engine/observation.d.mts +35 -0
- package/dist/engine/observation.mjs +201 -0
- package/dist/engine/recovery.d.mts +26 -0
- package/dist/engine/recovery.mjs +92 -0
- package/dist/engine/replay.d.mts +41 -0
- package/dist/engine/replay.mjs +122 -0
- package/dist/engine/retry-policy.d.mts +4 -0
- package/dist/engine/retry-policy.mjs +4 -0
- package/dist/engine/retry.d.mts +23 -0
- package/dist/engine/retry.mjs +75 -0
- package/dist/engine/run-data.d.mts +9 -0
- package/dist/engine/run-data.mjs +74 -0
- package/dist/engine/run-service.d.mts +38 -0
- package/dist/engine/run-service.mjs +133 -0
- package/dist/engine/run-utils.d.mts +16 -0
- package/dist/engine/run-utils.mjs +83 -0
- package/dist/engine/schedule.d.mts +25 -0
- package/dist/engine/schedule.mjs +207 -0
- package/dist/engine/serialization.d.mts +9 -0
- package/dist/engine/serialization.mjs +85 -0
- package/dist/engine/start-api.d.mts +30 -0
- package/dist/engine/start-api.mjs +98 -0
- package/dist/engine/state.d.mts +77 -0
- package/dist/engine/state.mjs +464 -0
- package/dist/engine/step-api.d.mts +9 -0
- package/dist/engine/step-api.mjs +25 -0
- package/dist/engine/stream.d.mts +55 -0
- package/dist/engine/stream.mjs +333 -0
- package/dist/engine/summary/active-events.d.mts +34 -0
- package/dist/engine/summary/active-events.mjs +69 -0
- package/dist/engine/summary/child-workflow-runs.d.mts +24 -0
- package/dist/engine/summary/child-workflow-runs.mjs +138 -0
- package/dist/engine/summary/child-workflow-waits.d.mts +21 -0
- package/dist/engine/summary/child-workflow-waits.mjs +67 -0
- package/dist/engine/summary/recovery-child-workflow-waits.d.mts +11 -0
- package/dist/engine/summary/recovery-child-workflow-waits.mjs +89 -0
- package/dist/engine/summary/recovery-due-runs.d.mts +16 -0
- package/dist/engine/summary/recovery-due-runs.mjs +52 -0
- package/dist/engine/summary/recovery-failed-runs.d.mts +14 -0
- package/dist/engine/summary/recovery-failed-runs.mjs +33 -0
- package/dist/engine/summary/recovery-lease-runs.d.mts +16 -0
- package/dist/engine/summary/recovery-lease-runs.mjs +69 -0
- package/dist/engine/summary/recovery-locks.d.mts +18 -0
- package/dist/engine/summary/recovery-locks.mjs +100 -0
- package/dist/engine/summary/recovery-message-waits.d.mts +11 -0
- package/dist/engine/summary/recovery-message-waits.mjs +74 -0
- package/dist/engine/summary/recovery-retry-runs.d.mts +17 -0
- package/dist/engine/summary/recovery-retry-runs.mjs +65 -0
- package/dist/engine/summary/recovery-run-timestamps.d.mts +13 -0
- package/dist/engine/summary/recovery-run-timestamps.mjs +18 -0
- package/dist/engine/summary/recovery-runs.d.mts +23 -0
- package/dist/engine/summary/recovery-runs.mjs +54 -0
- package/dist/engine/summary/recovery-schedules.d.mts +10 -0
- package/dist/engine/summary/recovery-schedules.mjs +46 -0
- package/dist/engine/summary/recovery-sleep-waits.d.mts +10 -0
- package/dist/engine/summary/recovery-sleep-waits.mjs +53 -0
- package/dist/engine/summary/recovery-waits.d.mts +21 -0
- package/dist/engine/summary/recovery-waits.mjs +29 -0
- package/dist/engine/summary/run-availability.d.mts +13 -0
- package/dist/engine/summary/run-availability.mjs +37 -0
- package/dist/engine/summary/run-cleanup.d.mts +14 -0
- package/dist/engine/summary/run-cleanup.mjs +44 -0
- package/dist/engine/summary/run-failure.d.mts +14 -0
- package/dist/engine/summary/run-failure.mjs +62 -0
- package/dist/engine/summary/run-hooks.d.mts +14 -0
- package/dist/engine/summary/run-hooks.mjs +63 -0
- package/dist/engine/summary/run-lease.d.mts +11 -0
- package/dist/engine/summary/run-lease.mjs +64 -0
- package/dist/engine/summary/run-message-waits.d.mts +15 -0
- package/dist/engine/summary/run-message-waits.mjs +62 -0
- package/dist/engine/summary/run-metrics.d.mts +31 -0
- package/dist/engine/summary/run-metrics.mjs +93 -0
- package/dist/engine/summary/run-retry.d.mts +9 -0
- package/dist/engine/summary/run-retry.mjs +69 -0
- package/dist/engine/summary/run-sleeps.d.mts +15 -0
- package/dist/engine/summary/run-sleeps.mjs +61 -0
- package/dist/engine/summary/run-state.d.mts +26 -0
- package/dist/engine/summary/run-state.mjs +48 -0
- package/dist/engine/summary/run-status.d.mts +16 -0
- package/dist/engine/summary/run-status.mjs +35 -0
- package/dist/engine/summary/run-streams.d.mts +14 -0
- package/dist/engine/summary/run-streams.mjs +46 -0
- package/dist/engine/summary/run-transition.d.mts +9 -0
- package/dist/engine/summary/run-transition.mjs +39 -0
- package/dist/engine/summary/run-workflow.d.mts +9 -0
- package/dist/engine/summary/run-workflow.mjs +47 -0
- package/dist/engine/summary/step-attempt-active-child-workflows.d.mts +13 -0
- package/dist/engine/summary/step-attempt-active-child-workflows.mjs +55 -0
- package/dist/engine/summary/step-attempt-active-fields.d.mts +23 -0
- package/dist/engine/summary/step-attempt-active-fields.mjs +14 -0
- package/dist/engine/summary/step-attempt-active-message-waits.d.mts +12 -0
- package/dist/engine/summary/step-attempt-active-message-waits.mjs +47 -0
- package/dist/engine/summary/step-attempt-active-sleeps.d.mts +19 -0
- package/dist/engine/summary/step-attempt-active-sleeps.mjs +44 -0
- package/dist/engine/summary/step-attempt-buckets.d.mts +22 -0
- package/dist/engine/summary/step-attempt-buckets.mjs +152 -0
- package/dist/engine/summary/step-attempt-failure.d.mts +12 -0
- package/dist/engine/summary/step-attempt-failure.mjs +61 -0
- package/dist/engine/summary/step-attempt-lifecycle.d.mts +13 -0
- package/dist/engine/summary/step-attempt-lifecycle.mjs +19 -0
- package/dist/engine/summary/step-attempts.d.mts +11 -0
- package/dist/engine/summary/step-attempts.mjs +43 -0
- package/dist/engine/summary.d.mts +32 -0
- package/dist/engine/summary.mjs +37 -0
- package/dist/engine/task.d.mts +6 -0
- package/dist/engine/task.mjs +207 -0
- package/dist/errors/base.d.mts +99 -0
- package/dist/errors/base.mjs +426 -0
- package/dist/errors/capability.d.mts +11 -0
- package/dist/errors/capability.mjs +23 -0
- package/dist/errors/definition.d.mts +21 -0
- package/dist/errors/definition.mjs +51 -0
- package/dist/errors/details.d.mts +13 -0
- package/dist/errors/details.mjs +65 -0
- package/dist/errors/failures.d.mts +81 -0
- package/dist/errors/failures.mjs +168 -0
- package/dist/errors/hook.d.mts +14 -0
- package/dist/errors/hook.mjs +30 -0
- package/dist/errors/implementation.d.mts +11 -0
- package/dist/errors/implementation.mjs +25 -0
- package/dist/errors/lock.d.mts +28 -0
- package/dist/errors/lock.mjs +70 -0
- package/dist/errors/message.d.mts +106 -0
- package/dist/errors/message.mjs +122 -0
- package/dist/errors/mod.d.mts +17 -0
- package/dist/errors/mod.mjs +17 -0
- package/dist/errors/record-view.d.mts +11 -0
- package/dist/errors/record-view.mjs +24 -0
- package/dist/errors/records.d.mts +7 -0
- package/dist/errors/records.mjs +280 -0
- package/dist/errors/run.d.mts +116 -0
- package/dist/errors/run.mjs +232 -0
- package/dist/errors/runtime.d.mts +12 -0
- package/dist/errors/runtime.mjs +24 -0
- package/dist/errors/schedule.d.mts +27 -0
- package/dist/errors/schedule.mjs +65 -0
- package/dist/errors/step.d.mts +76 -0
- package/dist/errors/step.mjs +113 -0
- package/dist/errors/stream.d.mts +24 -0
- package/dist/errors/stream.mjs +64 -0
- package/dist/esbuild.d.mts +7 -0
- package/dist/esbuild.mjs +6 -0
- package/dist/events/descriptors.d.mts +245 -0
- package/dist/events/descriptors.mjs +405 -0
- package/dist/events/mod.d.mts +3 -0
- package/dist/events/mod.mjs +2 -0
- package/dist/history/attempt-key.d.mts +6 -0
- package/dist/history/attempt-key.mjs +7 -0
- package/dist/history/mod.d.mts +71 -0
- package/dist/history/mod.mjs +195 -0
- package/dist/history/projection.d.mts +8 -0
- package/dist/history/projection.mjs +259 -0
- package/dist/history/replay-projection.d.mts +88 -0
- package/dist/history/replay-projection.mjs +369 -0
- package/dist/history/sequencer.d.mts +16 -0
- package/dist/history/sequencer.mjs +78 -0
- package/dist/history/step-attempts.d.mts +9 -0
- package/dist/history/step-attempts.mjs +50 -0
- package/dist/history/timeline.d.mts +16 -0
- package/dist/history/timeline.mjs +272 -0
- package/dist/history/validator.d.mts +9 -0
- package/dist/history/validator.mjs +110 -0
- package/dist/inspection/mod.d.mts +5 -0
- package/dist/inspection/mod.mjs +3 -0
- package/dist/message-options.d.mts +8 -0
- package/dist/message-options.mjs +25 -0
- package/dist/mod.d.mts +52 -0
- package/dist/mod.mjs +26 -0
- package/dist/otel/support.d.mts +18 -0
- package/dist/otel/support.mjs +102 -0
- package/dist/otel.d.mts +30 -0
- package/dist/otel.mjs +588 -0
- package/dist/pagination.d.mts +25 -0
- package/dist/pagination.mjs +73 -0
- package/dist/registry.d.mts +20 -0
- package/dist/registry.mjs +48 -0
- package/dist/rolldown.d.mts +7 -0
- package/dist/rolldown.mjs +6 -0
- package/dist/rollup.d.mts +7 -0
- package/dist/rollup.mjs +6 -0
- package/dist/rspack.d.mts +7 -0
- package/dist/rspack.mjs +6 -0
- package/dist/runtime.d.mts +34 -0
- package/dist/runtime.mjs +194 -0
- package/dist/step-definition.d.mts +10 -0
- package/dist/step-definition.mjs +15 -0
- package/dist/store/append-projection.d.mts +15 -0
- package/dist/store/append-projection.mjs +26 -0
- package/dist/store/capabilities.d.mts +14 -0
- package/dist/store/capabilities.mjs +42 -0
- package/dist/store/cleanup.d.mts +18 -0
- package/dist/store/cleanup.mjs +338 -0
- package/dist/store/compaction.d.mts +5 -0
- package/dist/store/compaction.mjs +8 -0
- package/dist/store/create.d.mts +52 -0
- package/dist/store/create.mjs +159 -0
- package/dist/store/event-append-project.d.mts +15 -0
- package/dist/store/event-append-project.mjs +77 -0
- package/dist/store/event-append.d.mts +37 -0
- package/dist/store/event-append.mjs +80 -0
- package/dist/store/event-history.d.mts +22 -0
- package/dist/store/event-history.mjs +75 -0
- package/dist/store/event.d.mts +10 -0
- package/dist/store/event.mjs +28 -0
- package/dist/store/idempotency.d.mts +10 -0
- package/dist/store/idempotency.mjs +167 -0
- package/dist/store/instrumentation.d.mts +7 -0
- package/dist/store/instrumentation.mjs +123 -0
- package/dist/store/lock.d.mts +8 -0
- package/dist/store/lock.mjs +28 -0
- package/dist/store/materialization.d.mts +30 -0
- package/dist/store/materialization.mjs +127 -0
- package/dist/store/message-index.d.mts +21 -0
- package/dist/store/message-index.mjs +121 -0
- package/dist/store/mod.d.mts +5 -0
- package/dist/store/mod.mjs +4 -0
- package/dist/store/query.d.mts +24 -0
- package/dist/store/query.mjs +82 -0
- package/dist/store/revision.d.mts +4 -0
- package/dist/store/revision.mjs +7 -0
- package/dist/store/run.d.mts +49 -0
- package/dist/store/run.mjs +236 -0
- package/dist/store/schedule.d.mts +12 -0
- package/dist/store/schedule.mjs +41 -0
- package/dist/store/schema.d.mts +14 -0
- package/dist/store/schema.mjs +17 -0
- package/dist/store/step-attempt.d.mts +14 -0
- package/dist/store/step-attempt.mjs +23 -0
- package/dist/store/storage-value.d.mts +5 -0
- package/dist/store/storage-value.mjs +6 -0
- package/dist/store/validation.d.mts +35 -0
- package/dist/store/validation.mjs +77 -0
- package/dist/store-keys.d.mts +49 -0
- package/dist/store-keys.mjs +63 -0
- package/dist/telemetry.d.mts +132 -0
- package/dist/telemetry.mjs +172 -0
- package/dist/temporal.d.mts +23 -0
- package/dist/temporal.mjs +59 -0
- package/dist/timer.d.mts +12 -0
- package/dist/timer.mjs +28 -0
- package/dist/types/cleanup.d.mts +112 -0
- package/dist/types/cleanup.mjs +22 -0
- package/dist/types/client.d.mts +270 -0
- package/dist/types/client.mjs +1 -0
- package/dist/types/common.d.mts +19 -0
- package/dist/types/common.mjs +15 -0
- package/dist/types/definition.d.mts +92 -0
- package/dist/types/definition.mjs +1 -0
- package/dist/types/engine-options.d.mts +76 -0
- package/dist/types/engine-options.mjs +1 -0
- package/dist/types/engine.d.mts +309 -0
- package/dist/types/engine.mjs +1 -0
- package/dist/types/error-code.d.mts +256 -0
- package/dist/types/error-code.mjs +131 -0
- package/dist/types/error.d.mts +236 -0
- package/dist/types/error.mjs +48 -0
- package/dist/types/events.d.mts +2869 -0
- package/dist/types/events.mjs +283 -0
- package/dist/types/hook.d.mts +149 -0
- package/dist/types/hook.mjs +66 -0
- package/dist/types/lock.d.mts +63 -0
- package/dist/types/lock.mjs +18 -0
- package/dist/types/message-id.d.mts +7 -0
- package/dist/types/message-id.mjs +5 -0
- package/dist/types/message.d.mts +87 -0
- package/dist/types/message.mjs +1 -0
- package/dist/types/observation.d.mts +146 -0
- package/dist/types/observation.mjs +1 -0
- package/dist/types/pagination.d.mts +32 -0
- package/dist/types/pagination.mjs +13 -0
- package/dist/types/result.d.mts +62 -0
- package/dist/types/result.mjs +1 -0
- package/dist/types/retry.d.mts +48 -0
- package/dist/types/retry.mjs +1 -0
- package/dist/types/run-id.d.mts +7 -0
- package/dist/types/run-id.mjs +5 -0
- package/dist/types/run-list.d.mts +43 -0
- package/dist/types/run-list.mjs +1 -0
- package/dist/types/run-options.d.mts +86 -0
- package/dist/types/run-options.mjs +1 -0
- package/dist/types/run.d.mts +355 -0
- package/dist/types/run.mjs +207 -0
- package/dist/types/runtime.d.mts +143 -0
- package/dist/types/runtime.mjs +21 -0
- package/dist/types/schedule.d.mts +127 -0
- package/dist/types/schedule.mjs +48 -0
- package/dist/types/step-attempts.d.mts +326 -0
- package/dist/types/step-attempts.mjs +43 -0
- package/dist/types/step-id.d.mts +7 -0
- package/dist/types/step-id.mjs +5 -0
- package/dist/types/step.d.mts +183 -0
- package/dist/types/step.mjs +5 -0
- package/dist/types/store.d.mts +254 -0
- package/dist/types/store.mjs +1 -0
- package/dist/types/stream-id.d.mts +7 -0
- package/dist/types/stream-id.mjs +5 -0
- package/dist/types/stream.d.mts +126 -0
- package/dist/types/stream.mjs +1 -0
- package/dist/types/summary.d.mts +318 -0
- package/dist/types/summary.mjs +1 -0
- package/dist/types/telemetry.d.mts +74 -0
- package/dist/types/telemetry.mjs +9 -0
- package/dist/types/timeline.d.mts +237 -0
- package/dist/types/timeline.mjs +1 -0
- package/dist/types/vite.d.mts +65 -0
- package/dist/types/vite.mjs +1 -0
- package/dist/types/worker-id.d.mts +7 -0
- package/dist/types/worker-id.mjs +5 -0
- package/dist/types/worker.d.mts +94 -0
- package/dist/types/worker.mjs +1 -0
- package/dist/utility.d.mts +49 -0
- package/dist/utility.mjs +122 -0
- package/dist/vite/ast.d.mts +23 -0
- package/dist/vite/ast.mjs +73 -0
- package/dist/vite/error.d.mts +19 -0
- package/dist/vite/error.mjs +47 -0
- package/dist/vite/generated-name.d.mts +7 -0
- package/dist/vite/generated-name.mjs +9 -0
- package/dist/vite/manifest.d.mts +7 -0
- package/dist/vite/manifest.mjs +106 -0
- package/dist/vite/plugin.d.mts +7 -0
- package/dist/vite/plugin.mjs +190 -0
- package/dist/vite/safety.d.mts +12 -0
- package/dist/vite/safety.mjs +96 -0
- package/dist/vite/source-edit.d.mts +12 -0
- package/dist/vite/source-edit.mjs +44 -0
- package/dist/vite/source-map.d.mts +11 -0
- package/dist/vite/source-map.mjs +53 -0
- package/dist/vite/source.d.mts +7 -0
- package/dist/vite/source.mjs +14 -0
- package/dist/vite/transform.d.mts +7 -0
- package/dist/vite/transform.mjs +405 -0
- package/dist/vite.d.mts +6 -0
- package/dist/vite.mjs +5 -0
- package/dist/webhook.d.mts +16 -0
- package/dist/webhook.mjs +17 -0
- package/dist/webpack.d.mts +7 -0
- package/dist/webpack.mjs +6 -0
- package/dist/worker/error.d.mts +11 -0
- package/dist/worker/error.mjs +40 -0
- package/dist/worker.d.mts +18 -0
- package/dist/worker.mjs +427 -0
- package/package.json +144 -0
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
import { WorkflowStateError } from "../errors/base.mjs";
|
|
2
|
+
import { makeWorkflowStepAttemptKey } from "../history/attempt-key.mjs";
|
|
3
|
+
import { omitUndefined } from "../collection.mjs";
|
|
4
|
+
import { getWorkflowEventStepAttemptKey } from "../events/descriptors.mjs";
|
|
5
|
+
import { applyWorkflowStepAttemptEvent } from "../history/projection.mjs";
|
|
6
|
+
import "../history/mod.mjs";
|
|
7
|
+
import { supportsWorkflowConditionalWrite } from "./capabilities.mjs";
|
|
8
|
+
import { makeStepAttemptKey } from "../store-keys.mjs";
|
|
9
|
+
import { toStorageValue } from "./storage-value.mjs";
|
|
10
|
+
import { parseStoredWorkflowStepAttemptRecord } from "./validation.mjs";
|
|
11
|
+
import { rollbackAppendedEventIfCurrent } from "./event-history.mjs";
|
|
12
|
+
import { createWorkflowStepAttempts } from "../history/step-attempts.mjs";
|
|
13
|
+
import { requireReadableStoredWorkflowRun } from "./run.mjs";
|
|
14
|
+
//#region src/store/materialization.ts
|
|
15
|
+
const MAX_MATERIALIZE_STEP_ATTEMPT_ATTEMPTS = 16;
|
|
16
|
+
const WORKFLOW_MATERIALIZED_STEP_DESCRIPTORS = {
|
|
17
|
+
run: { identityFields: [] },
|
|
18
|
+
sleep: { identityFields: ["until"] },
|
|
19
|
+
workflow: { identityFields: [
|
|
20
|
+
"childRunId",
|
|
21
|
+
"workflowName",
|
|
22
|
+
"workflowVersion",
|
|
23
|
+
"cancellation"
|
|
24
|
+
] },
|
|
25
|
+
"message-send": { identityFields: ["targetRunId", "messageId"] },
|
|
26
|
+
"message-wait": { identityFields: ["messageId"] },
|
|
27
|
+
stream: { identityFields: [] },
|
|
28
|
+
deterministic: { identityFields: [] }
|
|
29
|
+
};
|
|
30
|
+
async function materializeStepAttempt(storage, namespace, runId, event) {
|
|
31
|
+
const storageKey = getWorkflowStepAttemptMaterializationStorageKey(namespace, runId, event);
|
|
32
|
+
if (storageKey === void 0) return;
|
|
33
|
+
await materializeStepAttemptIfCurrent(storage, runId, storageKey, (current) => applyWorkflowStepAttemptEvent(runId, event, current));
|
|
34
|
+
}
|
|
35
|
+
async function materializeStepAttemptForAppendOutcome(storage, namespace, runId, event, outcome) {
|
|
36
|
+
try {
|
|
37
|
+
await materializeStepAttempt(storage, namespace, runId, event);
|
|
38
|
+
} catch (error) {
|
|
39
|
+
if (outcome.status === "appended") await rollbackAppendedEventIfCurrent(storage, namespace, runId, outcome.events, outcome.previousEventHistory);
|
|
40
|
+
throw error;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
function getWorkflowStepAttemptMaterializationStorageKey(namespace, runId, event) {
|
|
44
|
+
let attemptKey;
|
|
45
|
+
try {
|
|
46
|
+
attemptKey = getWorkflowEventStepAttemptKey(event);
|
|
47
|
+
} catch {
|
|
48
|
+
attemptKey = void 0;
|
|
49
|
+
}
|
|
50
|
+
return attemptKey === void 0 ? void 0 : makeStepAttemptKey(namespace, runId, attemptKey);
|
|
51
|
+
}
|
|
52
|
+
function createWorkflowStepAttemptMaterializationUpdate(namespace, runId, event, current) {
|
|
53
|
+
const storageKey = getWorkflowStepAttemptMaterializationStorageKey(namespace, runId, event);
|
|
54
|
+
if (storageKey === void 0) return;
|
|
55
|
+
const next = applyWorkflowStepAttemptEvent(runId, event, current);
|
|
56
|
+
return {
|
|
57
|
+
storageKey,
|
|
58
|
+
...next === void 0 ? {} : { next },
|
|
59
|
+
shouldStore: next !== void 0 && shouldStoreStepAttempt(current, next)
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
async function createWorkflowStepAttemptMaterializationStoragePlan(storage, namespace, runId, event) {
|
|
63
|
+
const storageKey = getWorkflowStepAttemptMaterializationStorageKey(namespace, runId, event);
|
|
64
|
+
if (storageKey === void 0) return { materializes: false };
|
|
65
|
+
const { raw, current } = await readStoredWorkflowStepAttempt(storage, storageKey);
|
|
66
|
+
const materialization = createWorkflowStepAttemptMaterializationUpdate(namespace, runId, event, current);
|
|
67
|
+
return {
|
|
68
|
+
materializes: true,
|
|
69
|
+
storageKey,
|
|
70
|
+
...current === void 0 ? {} : { current },
|
|
71
|
+
...materialization?.shouldStore === true && materialization.next !== void 0 ? { item: {
|
|
72
|
+
key: materialization.storageKey,
|
|
73
|
+
expected: raw,
|
|
74
|
+
value: toStorageValue(omitUndefined(materialization.next))
|
|
75
|
+
} } : {}
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
async function reconcileStepAttempts(storage, namespace, runId, events) {
|
|
79
|
+
const attempts = createWorkflowStepAttempts(runId, events);
|
|
80
|
+
await requireReadableStoredWorkflowRun(storage, namespace, runId);
|
|
81
|
+
await Promise.all(attempts.map((attempt) => materializeStepAttemptIfCurrent(storage, runId, makeStepAttemptKey(namespace, runId, makeWorkflowStepAttemptKey(attempt.stepId, attempt.attempt)), () => attempt, { repairStatusMismatch: true })));
|
|
82
|
+
return attempts;
|
|
83
|
+
}
|
|
84
|
+
async function materializeStepAttemptIfCurrent(storage, runId, storageKey, createNext, options = {}) {
|
|
85
|
+
if (!supportsWorkflowConditionalWrite(storage)) {
|
|
86
|
+
const { current } = await readStoredWorkflowStepAttempt(storage, storageKey);
|
|
87
|
+
const next = createNext(current);
|
|
88
|
+
if (next !== void 0 && shouldStoreStepAttempt(current, next, options)) await storage.set(storageKey, toStorageValue(omitUndefined(next)));
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
for (let attempt = 0; attempt < MAX_MATERIALIZE_STEP_ATTEMPT_ATTEMPTS; attempt++) {
|
|
92
|
+
const { raw, current } = await readStoredWorkflowStepAttempt(storage, storageKey);
|
|
93
|
+
const next = createNext(current);
|
|
94
|
+
if (next === void 0 || !shouldStoreStepAttempt(current, next, options)) return;
|
|
95
|
+
if (await storage.compareAndSet(storageKey, raw, toStorageValue(omitUndefined(next)))) return;
|
|
96
|
+
}
|
|
97
|
+
WorkflowStateError.failedStepAttemptMaterialization(runId, MAX_MATERIALIZE_STEP_ATTEMPT_ATTEMPTS);
|
|
98
|
+
}
|
|
99
|
+
async function readStoredWorkflowStepAttempt(storage, storageKey) {
|
|
100
|
+
const raw = await storage.get(storageKey);
|
|
101
|
+
return {
|
|
102
|
+
raw,
|
|
103
|
+
current: parseStoredWorkflowStepAttemptRecord(raw)
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
function shouldStoreStepAttempt(current, next, options = {}) {
|
|
107
|
+
if (current === void 0) return true;
|
|
108
|
+
if (!isSameStepAttemptIdentity(current, next)) return true;
|
|
109
|
+
if (current.status === "abandoned" !== (next.status === "abandoned")) return true;
|
|
110
|
+
if (options.repairStatusMismatch === true && current.status !== next.status) return true;
|
|
111
|
+
const currentTimestamp = current.finishedAt ?? current.startedAt;
|
|
112
|
+
const nextTimestamp = next.finishedAt ?? next.startedAt;
|
|
113
|
+
if (currentTimestamp === void 0) return true;
|
|
114
|
+
if (nextTimestamp === void 0) return false;
|
|
115
|
+
if (Temporal.Instant.compare(currentTimestamp, nextTimestamp) > 0) return false;
|
|
116
|
+
if (Temporal.Instant.compare(currentTimestamp, nextTimestamp) < 0) return true;
|
|
117
|
+
return (next.status === "running" ? 0 : next.status === "abandoned" || next.status === "failed" || next.status === "completed" ? 1 : 0) >= (current.status === "running" ? 0 : current.status === "abandoned" || current.status === "failed" || current.status === "completed" ? 1 : 0);
|
|
118
|
+
}
|
|
119
|
+
function isSameStepAttemptIdentity(current, next) {
|
|
120
|
+
if (!(current.runId === next.runId && current.stepId === next.stepId && current.stepName === next.stepName && current.kind === next.kind && current.attempt === next.attempt && current.count === next.count)) return false;
|
|
121
|
+
return isSameMaterializedStepAttemptFields(current, next, WORKFLOW_MATERIALIZED_STEP_DESCRIPTORS[current.kind].identityFields);
|
|
122
|
+
}
|
|
123
|
+
function isSameMaterializedStepAttemptFields(current, next, fields) {
|
|
124
|
+
return fields.every((field) => current[field] === next[field]);
|
|
125
|
+
}
|
|
126
|
+
//#endregion
|
|
127
|
+
export { createWorkflowStepAttemptMaterializationStoragePlan, isSameStepAttemptIdentity, materializeStepAttemptForAppendOutcome, materializeStepAttemptIfCurrent, reconcileStepAttempts, shouldStoreStepAttempt };
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { RunId } from "../types/run-id.mjs";
|
|
2
|
+
import "../types/run.mjs";
|
|
3
|
+
import { EventRecord } from "../types/events.mjs";
|
|
4
|
+
import { WorkflowMessageIdempotencyIndex, WorkflowStorage } from "../types/store.mjs";
|
|
5
|
+
import { WorkflowEventAppendSuccessOutcome } from "./event-append.mjs";
|
|
6
|
+
//#region src/store/message-index.d.ts
|
|
7
|
+
interface WorkflowDuplicateMessageAppendOutcome {
|
|
8
|
+
readonly status: "duplicate";
|
|
9
|
+
readonly events: readonly EventRecord[];
|
|
10
|
+
}
|
|
11
|
+
declare function duplicateMessageAppendOutcome(runId: RunId, events: readonly EventRecord[], event: EventRecord): WorkflowDuplicateMessageAppendOutcome | undefined;
|
|
12
|
+
declare function projectMessageIdempotencyIndexForAppendOutcome(storage: WorkflowStorage, namespace: string, runId: RunId, event: EventRecord, outcome: WorkflowEventAppendSuccessOutcome): Promise<readonly EventRecord[]>;
|
|
13
|
+
declare function reconcileMessageIdempotencyIndexes(storage: WorkflowStorage, namespace: string, runId: RunId, events: readonly EventRecord[]): Promise<readonly WorkflowMessageIdempotencyIndex[]>;
|
|
14
|
+
declare function repairMessageIdempotencyIndexes(storage: WorkflowStorage, namespace: string, runId: RunId): Promise<readonly WorkflowMessageIdempotencyIndex[]>;
|
|
15
|
+
declare function hasMessageIdempotencyKey(storage: WorkflowStorage, namespace: string, runId: RunId, idempotencyKey: string): Promise<boolean>;
|
|
16
|
+
declare function messageIdempotencyIndexValuesFromEvents(events: readonly EventRecord[], runId: RunId): readonly {
|
|
17
|
+
readonly idempotencyKey: string;
|
|
18
|
+
readonly value: WorkflowMessageIdempotencyIndex;
|
|
19
|
+
}[];
|
|
20
|
+
//#endregion
|
|
21
|
+
export { WorkflowDuplicateMessageAppendOutcome, duplicateMessageAppendOutcome, hasMessageIdempotencyKey, messageIdempotencyIndexValuesFromEvents, projectMessageIdempotencyIndexForAppendOutcome, reconcileMessageIdempotencyIndexes, repairMessageIdempotencyIndexes };
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import { WorkflowMessageIdempotencyConflictError } from "../errors/message.mjs";
|
|
2
|
+
import "../errors/mod.mjs";
|
|
3
|
+
import { WorkflowHistory } from "../history/mod.mjs";
|
|
4
|
+
import { supportsWorkflowConditionalWrite } from "./capabilities.mjs";
|
|
5
|
+
import { makeEventsKey, makeMessageIdempotencyKey } from "../store-keys.mjs";
|
|
6
|
+
import { isSameWorkflowStorageValue } from "./revision.mjs";
|
|
7
|
+
import { toStorageValue } from "./storage-value.mjs";
|
|
8
|
+
import { isSameMessageIdempotencyIndex } from "./validation.mjs";
|
|
9
|
+
import { getStoredEventHistory, rollbackAppendedEventIfCurrent } from "./event-history.mjs";
|
|
10
|
+
import { requireReadableStoredWorkflowRun } from "./run.mjs";
|
|
11
|
+
//#region src/store/message-index.ts
|
|
12
|
+
function duplicateMessageAppendOutcome(runId, events, event) {
|
|
13
|
+
if (!isMessageIdempotencyEvent(event)) return;
|
|
14
|
+
const existing = new WorkflowHistory(events).messageByIdempotencyKey(event.idempotencyKey);
|
|
15
|
+
if (existing === void 0) return;
|
|
16
|
+
requireDuplicateMessageEventMatches(runId, existing, event);
|
|
17
|
+
return {
|
|
18
|
+
status: "duplicate",
|
|
19
|
+
events
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
function isMessageIdempotencyEvent(event) {
|
|
23
|
+
return event.kind === "message_sent" && event.idempotencyKey !== void 0;
|
|
24
|
+
}
|
|
25
|
+
function requireDuplicateMessageEventMatches(runId, existing, event) {
|
|
26
|
+
if (existing.messageId === event.messageId && isSameWorkflowStorageValue(existing.payload, event.payload)) return;
|
|
27
|
+
WorkflowMessageIdempotencyConflictError.conflict({
|
|
28
|
+
runId,
|
|
29
|
+
messageId: existing.messageId,
|
|
30
|
+
idempotencyKey: event.idempotencyKey
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
async function indexMessageIdempotencyKey(storage, namespace, runId, event, events) {
|
|
34
|
+
if (event.kind !== "message_sent" || event.idempotencyKey === void 0) return;
|
|
35
|
+
await requireReadableStoredWorkflowRun(storage, namespace, runId);
|
|
36
|
+
const indexedEvent = indexableMessageEventByIdempotencyKey(new WorkflowHistory(events), event.idempotencyKey);
|
|
37
|
+
if (indexedEvent === void 0) return;
|
|
38
|
+
const key = makeMessageIdempotencyKey(namespace, runId, event.idempotencyKey);
|
|
39
|
+
const value = {
|
|
40
|
+
runId,
|
|
41
|
+
messageId: indexedEvent.messageId,
|
|
42
|
+
timestamp: indexedEvent.timestamp
|
|
43
|
+
};
|
|
44
|
+
if (!supportsWorkflowConditionalWrite(storage)) {
|
|
45
|
+
const current = await storage.get(key);
|
|
46
|
+
if (current !== void 0) {
|
|
47
|
+
if (isSameMessageIdempotencyIndex(current, value)) return;
|
|
48
|
+
WorkflowMessageIdempotencyConflictError.conflict({
|
|
49
|
+
runId,
|
|
50
|
+
messageId: indexedEvent.messageId,
|
|
51
|
+
idempotencyKey: event.idempotencyKey
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
await storage.set(key, toStorageValue(value));
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
if (await storage.compareAndSet(key, void 0, toStorageValue(value))) return;
|
|
58
|
+
if (isSameMessageIdempotencyIndex(await storage.get(key), value)) return;
|
|
59
|
+
WorkflowMessageIdempotencyConflictError.conflict({
|
|
60
|
+
runId,
|
|
61
|
+
messageId: indexedEvent.messageId,
|
|
62
|
+
idempotencyKey: event.idempotencyKey
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
async function projectMessageIdempotencyIndexForAppendOutcome(storage, namespace, runId, event, outcome) {
|
|
66
|
+
try {
|
|
67
|
+
await indexMessageIdempotencyKey(storage, namespace, runId, event, outcome.events);
|
|
68
|
+
} catch (error) {
|
|
69
|
+
if (outcome.status === "appended") await rollbackAppendedEventIfCurrent(storage, namespace, runId, outcome.events, outcome.previousEventHistory);
|
|
70
|
+
throw error;
|
|
71
|
+
}
|
|
72
|
+
return outcome.events;
|
|
73
|
+
}
|
|
74
|
+
async function reconcileMessageIdempotencyIndexes(storage, namespace, runId, events) {
|
|
75
|
+
await requireReadableStoredWorkflowRun(storage, namespace, runId);
|
|
76
|
+
const indexes = messageIdempotencyIndexValuesFromEvents(events, runId);
|
|
77
|
+
await Promise.all(indexes.map(async ({ idempotencyKey, value }) => {
|
|
78
|
+
await storeMessageIdempotencyIndex(storage, namespace, runId, idempotencyKey, value);
|
|
79
|
+
}));
|
|
80
|
+
return indexes.map((index) => index.value);
|
|
81
|
+
}
|
|
82
|
+
async function repairMessageIdempotencyIndexes(storage, namespace, runId) {
|
|
83
|
+
await requireReadableStoredWorkflowRun(storage, namespace, runId);
|
|
84
|
+
return await reconcileMessageIdempotencyIndexes(storage, namespace, runId, (await getStoredEventHistory(storage, makeEventsKey(namespace, runId))).events);
|
|
85
|
+
}
|
|
86
|
+
async function hasMessageIdempotencyKey(storage, namespace, runId, idempotencyKey) {
|
|
87
|
+
return await storage.get(makeMessageIdempotencyKey(namespace, runId, idempotencyKey)) !== void 0;
|
|
88
|
+
}
|
|
89
|
+
function indexableMessageEventByIdempotencyKey(history, idempotencyKey) {
|
|
90
|
+
const event = history.messageByIdempotencyKey(idempotencyKey);
|
|
91
|
+
return event !== void 0 && isIndexableMessageIdempotencyEvent(event) ? event : void 0;
|
|
92
|
+
}
|
|
93
|
+
function messageIdempotencyIndexValuesFromEvents(events, runId) {
|
|
94
|
+
return new WorkflowHistory(events).latestMessagesByIdempotencyKey().filter(isIndexableMessageIdempotencyEvent).map((event) => ({
|
|
95
|
+
idempotencyKey: event.idempotencyKey,
|
|
96
|
+
value: {
|
|
97
|
+
runId,
|
|
98
|
+
messageId: event.messageId,
|
|
99
|
+
timestamp: event.timestamp
|
|
100
|
+
}
|
|
101
|
+
}));
|
|
102
|
+
}
|
|
103
|
+
function isIndexableMessageIdempotencyEvent(event) {
|
|
104
|
+
return event.kind === "message_sent" && event.idempotencyKey !== void 0 && typeof event.messageId === "string" && event.messageId.trim() !== "";
|
|
105
|
+
}
|
|
106
|
+
async function storeMessageIdempotencyIndex(storage, namespace, runId, idempotencyKey, value) {
|
|
107
|
+
const key = makeMessageIdempotencyKey(namespace, runId, idempotencyKey);
|
|
108
|
+
const current = await storage.get(key);
|
|
109
|
+
if (isSameMessageIdempotencyIndex(current, value)) return;
|
|
110
|
+
if (!supportsWorkflowConditionalWrite(storage)) {
|
|
111
|
+
await storage.set(key, toStorageValue(value));
|
|
112
|
+
return;
|
|
113
|
+
}
|
|
114
|
+
if (!await storage.compareAndSet(key, current, toStorageValue(value))) WorkflowMessageIdempotencyConflictError.conflict({
|
|
115
|
+
runId,
|
|
116
|
+
messageId: value.messageId,
|
|
117
|
+
idempotencyKey
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
//#endregion
|
|
121
|
+
export { duplicateMessageAppendOutcome, hasMessageIdempotencyKey, messageIdempotencyIndexValuesFromEvents, projectMessageIdempotencyIndexForAppendOutcome, reconcileMessageIdempotencyIndexes, repairMessageIdempotencyIndexes };
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { CreateWorkflowStoreOptions, WorkflowClaimRunOptions, WorkflowCleanupIndexKey, WorkflowCleanupIndexValue, WorkflowCleanupRepository, WorkflowConditionalEventAppender, WorkflowEventAppender, WorkflowEventHistoryChangeWaitOptions, WorkflowEventHistoryWatcher, WorkflowEventLog, WorkflowEventReader, WorkflowExtendRunLeaseOptions, WorkflowIdempotencyIndex, WorkflowIdempotencyKeyOptions, WorkflowLockConditionalWriterRepository, WorkflowLockListerRepository, WorkflowLockReaderRepository, WorkflowLockRepository, WorkflowMessageIdempotencyIndex, WorkflowMessageIdempotencyIndexLookup, WorkflowMessageIdempotencyRepairModel, WorkflowReleaseRunLeaseOptions, WorkflowRunConditionalWriterRepository, WorkflowRunCreationRepository, WorkflowRunLeaseRepository, WorkflowRunListerRepository, WorkflowRunQueryRepository, WorkflowRunReaderRepository, WorkflowRunRepository, WorkflowRunUnconditionalWriterRepository, WorkflowRunWriterRepository, WorkflowScheduleConditionalWriterRepository, WorkflowScheduleCreationRepository, WorkflowScheduleListerRepository, WorkflowScheduleQueryRepository, WorkflowScheduleReaderRepository, WorkflowScheduleRepository, WorkflowStepAttemptLookup, WorkflowStepAttemptReadModel, WorkflowStorage, WorkflowStoreCapabilities, WorkflowStoreCapabilitiesSource, WorkflowStoreMetadata, WorkflowStoreNamespace, WorkflowStoreStorageContext } from "../types/store.mjs";
|
|
2
|
+
import { getWorkflowStoreCapabilities } from "./capabilities.mjs";
|
|
3
|
+
import { WorkflowStore, createWorkflowStore } from "./create.mjs";
|
|
4
|
+
import { workflowCleanupIndexKeySchema, workflowMessageIdempotencyIndexSchema } from "./schema.mjs";
|
|
5
|
+
export { type CreateWorkflowStoreOptions, type WorkflowClaimRunOptions, type WorkflowCleanupIndexKey, type WorkflowCleanupIndexValue, type WorkflowCleanupRepository, type WorkflowConditionalEventAppender, type WorkflowEventAppender, type WorkflowEventHistoryChangeWaitOptions, type WorkflowEventHistoryWatcher, type WorkflowEventLog, type WorkflowEventReader, type WorkflowExtendRunLeaseOptions, type WorkflowIdempotencyIndex, type WorkflowIdempotencyKeyOptions, type WorkflowLockConditionalWriterRepository, type WorkflowLockListerRepository, type WorkflowLockReaderRepository, type WorkflowLockRepository, type WorkflowMessageIdempotencyIndex, type WorkflowMessageIdempotencyIndexLookup, type WorkflowMessageIdempotencyRepairModel, type WorkflowReleaseRunLeaseOptions, type WorkflowRunConditionalWriterRepository, type WorkflowRunCreationRepository, type WorkflowRunLeaseRepository, type WorkflowRunListerRepository, type WorkflowRunQueryRepository, type WorkflowRunReaderRepository, type WorkflowRunRepository, type WorkflowRunUnconditionalWriterRepository, type WorkflowRunWriterRepository, type WorkflowScheduleConditionalWriterRepository, type WorkflowScheduleCreationRepository, type WorkflowScheduleListerRepository, type WorkflowScheduleQueryRepository, type WorkflowScheduleReaderRepository, type WorkflowScheduleRepository, type WorkflowStepAttemptLookup, type WorkflowStepAttemptReadModel, type WorkflowStorage, WorkflowStore, type WorkflowStoreCapabilities, type WorkflowStoreCapabilitiesSource, type WorkflowStoreMetadata, type WorkflowStoreNamespace, type WorkflowStoreStorageContext, createWorkflowStore, getWorkflowStoreCapabilities, workflowCleanupIndexKeySchema, workflowMessageIdempotencyIndexSchema };
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import { getWorkflowStoreCapabilities } from "./capabilities.mjs";
|
|
2
|
+
import { workflowCleanupIndexKeySchema, workflowMessageIdempotencyIndexSchema } from "./schema.mjs";
|
|
3
|
+
import { WorkflowStore, createWorkflowStore } from "./create.mjs";
|
|
4
|
+
export { WorkflowStore, createWorkflowStore, getWorkflowStoreCapabilities, workflowCleanupIndexKeySchema, workflowMessageIdempotencyIndexSchema };
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { RunId } from "../types/run-id.mjs";
|
|
2
|
+
import { ListSchedulesOptions, ScheduleRecord } from "../types/schedule.mjs";
|
|
3
|
+
import { WorkflowListRunsOptions } from "../types/run-list.mjs";
|
|
4
|
+
import { WorkflowListStepAttemptsOptions, WorkflowStepAttemptRecord } from "../types/step-attempts.mjs";
|
|
5
|
+
import { WorkflowRunRecord } from "../types/run.mjs";
|
|
6
|
+
import { WorkflowEventReader, WorkflowRunReaderRepository, WorkflowStorage } from "../types/store.mjs";
|
|
7
|
+
//#region src/store/query.d.ts
|
|
8
|
+
type WorkflowStepAttemptQueryStore = WorkflowRunReaderRepository & WorkflowEventReader;
|
|
9
|
+
declare function queryWorkflowRuns(storage: WorkflowStorage, namespace: string, options: WorkflowListRunsOptions | undefined, query: {
|
|
10
|
+
readonly label: string;
|
|
11
|
+
readonly sort: boolean;
|
|
12
|
+
}): Promise<readonly WorkflowRunRecord[]>;
|
|
13
|
+
declare function queryWorkflowStepAttempts(storage: WorkflowStorage, namespace: string, store: WorkflowStepAttemptQueryStore, runId: RunId, options: WorkflowListStepAttemptsOptions | undefined, _query: {
|
|
14
|
+
readonly label: string;
|
|
15
|
+
}): Promise<readonly WorkflowStepAttemptRecord[]>;
|
|
16
|
+
declare function querySchedules(storage: WorkflowStorage, namespace: string, options: ListSchedulesOptions | undefined, _query: {
|
|
17
|
+
readonly label: string;
|
|
18
|
+
}): Promise<readonly ScheduleRecord[]>;
|
|
19
|
+
declare function getRequiredStoreRun(store: WorkflowRunReaderRepository, runId: RunId): Promise<WorkflowRunRecord>;
|
|
20
|
+
declare function matchesWorkflowRunListOptions(run: WorkflowRunRecord, options: WorkflowListRunsOptions | undefined): boolean;
|
|
21
|
+
declare function matchesWorkflowStepAttemptListOptions(attempt: WorkflowStepAttemptRecord, options: WorkflowListStepAttemptsOptions | undefined): boolean;
|
|
22
|
+
declare function matchesScheduleListOptions(schedule: ScheduleRecord, options: ListSchedulesOptions | undefined): boolean;
|
|
23
|
+
//#endregion
|
|
24
|
+
export { getRequiredStoreRun, matchesScheduleListOptions, matchesWorkflowRunListOptions, matchesWorkflowStepAttemptListOptions, querySchedules, queryWorkflowRuns, queryWorkflowStepAttempts };
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { WorkflowRunNotFoundError } from "../errors/run.mjs";
|
|
2
|
+
import { matchesOptionalValue, uniqueBy } from "../collection.mjs";
|
|
3
|
+
import "../errors/mod.mjs";
|
|
4
|
+
import { compareOptionalTimestamps, matchesOptionalDurationRange, matchesOptionalTimestampRange, matchesTimestampRange } from "../temporal.mjs";
|
|
5
|
+
import { makeRunKeyPrefix, makeScheduleKeyPrefix } from "../store-keys.mjs";
|
|
6
|
+
import { isListableWorkflowRunRecord, isReadableScheduleRecord } from "./validation.mjs";
|
|
7
|
+
import { reconcileStepAttempts } from "./materialization.mjs";
|
|
8
|
+
//#region src/store/query.ts
|
|
9
|
+
async function queryWorkflowRuns(storage, namespace, options, query) {
|
|
10
|
+
const uniqueRuns = uniqueBy((await storage.entries({ prefix: makeRunKeyPrefix(namespace) })).map((entry) => entry.value).filter((run) => isListableWorkflowRunRecord(run, namespace)).filter((run) => matchesWorkflowRunListOptions(run, options)), (run) => run.id);
|
|
11
|
+
if (!query.sort) return uniqueRuns;
|
|
12
|
+
return [...uniqueRuns].sort(compareWorkflowRunsForQuery);
|
|
13
|
+
}
|
|
14
|
+
async function queryWorkflowStepAttempts(storage, namespace, store, runId, options, _query) {
|
|
15
|
+
await getRequiredStoreRun(store, runId);
|
|
16
|
+
return (await reconcileStepAttempts(storage, namespace, runId, await store.getEvents(runId))).filter((attempt) => matchesWorkflowStepAttemptListOptions(attempt, options));
|
|
17
|
+
}
|
|
18
|
+
async function querySchedules(storage, namespace, options, _query) {
|
|
19
|
+
return uniqueBy((await storage.entries({ prefix: makeScheduleKeyPrefix(namespace) })).map((entry) => entry.value).filter((schedule) => isReadableScheduleRecord(schedule, namespace)).filter((schedule) => matchesScheduleListOptions(schedule, options)).sort(compareSchedulesForQuery), (schedule) => schedule.id);
|
|
20
|
+
}
|
|
21
|
+
function compareWorkflowRunsForQuery(left, right) {
|
|
22
|
+
const createdOrder = compareOptionalTimestamps(left.createdAt, right.createdAt);
|
|
23
|
+
if (createdOrder !== 0) return createdOrder;
|
|
24
|
+
return left.id.localeCompare(right.id);
|
|
25
|
+
}
|
|
26
|
+
function compareSchedulesForQuery(left, right) {
|
|
27
|
+
return Temporal.Instant.compare(left.nextFireAt, right.nextFireAt) || Temporal.Instant.compare(left.createdAt, right.createdAt) || left.id.localeCompare(right.id);
|
|
28
|
+
}
|
|
29
|
+
async function getRequiredStoreRun(store, runId) {
|
|
30
|
+
const run = await store.getRun(runId);
|
|
31
|
+
if (run === void 0) WorkflowRunNotFoundError.notFound(runId);
|
|
32
|
+
return run;
|
|
33
|
+
}
|
|
34
|
+
function matchesWorkflowRunListOptions(run, options) {
|
|
35
|
+
if (options?.status !== void 0 && !matchesOptionalValue(run.status, options.status)) return false;
|
|
36
|
+
if (options?.workflowName !== void 0 && run.workflowName !== options.workflowName) return false;
|
|
37
|
+
if (options?.workflowVersion !== void 0 && run.workflowVersion !== options.workflowVersion) return false;
|
|
38
|
+
if (options?.idempotencyKey !== void 0 && run.idempotencyKey !== options.idempotencyKey) return false;
|
|
39
|
+
if (options?.workerId !== void 0 && run.workerId !== options.workerId) return false;
|
|
40
|
+
if (options?.parentRunId !== void 0 && run.parentRunId !== options.parentRunId) return false;
|
|
41
|
+
if (options?.parentStepId !== void 0 && run.parentStepId !== options.parentStepId) return false;
|
|
42
|
+
if (options?.parentStepName !== void 0 && run.parentStepName !== options.parentStepName) return false;
|
|
43
|
+
if (options?.parentStepAttempt !== void 0 && run.parentStepAttempt !== options.parentStepAttempt) return false;
|
|
44
|
+
if (options?.lastTransitionReason !== void 0 && !matchesOptionalValue(run.lastTransitionReason, options.lastTransitionReason)) return false;
|
|
45
|
+
if (options?.retryReason !== void 0 && (run.retryReason === void 0 || !matchesOptionalValue(run.retryReason, options.retryReason))) return false;
|
|
46
|
+
if (!matchesRunAttributeFilters(run, options)) return false;
|
|
47
|
+
return matchesTimestampRange(run.createdAt, options?.createdAtFrom, options?.createdAtTo) && matchesTimestampRange(run.updatedAt, options?.updatedAtFrom, options?.updatedAtTo) && matchesOptionalTimestampRange(run.finishedAt, options?.finishedAtFrom, options?.finishedAtTo) && matchesOptionalTimestampRange(run.startedAt, options?.startedAtFrom, options?.startedAtTo) && matchesOptionalTimestampRange(run.availableAt, options?.availableAtFrom, options?.availableAtTo) && matchesOptionalTimestampRange(run.leaseExpiresAt, options?.leaseExpiresAtFrom, options?.leaseExpiresAtTo) && matchesOptionalTimestampRange(run.retryAt, options?.retryAtFrom, options?.retryAtTo);
|
|
48
|
+
}
|
|
49
|
+
function matchesWorkflowStepAttemptListOptions(attempt, options) {
|
|
50
|
+
if (options?.status !== void 0 && !matchesOptionalValue(attempt.status, options.status)) return false;
|
|
51
|
+
if (options?.kind !== void 0 && !matchesOptionalValue(attempt.kind, options.kind)) return false;
|
|
52
|
+
if (options?.stepId !== void 0 && attempt.stepId !== options.stepId) return false;
|
|
53
|
+
if (options?.stepName !== void 0 && attempt.stepName !== options.stepName) return false;
|
|
54
|
+
if (options?.attempt !== void 0 && attempt.attempt !== options.attempt) return false;
|
|
55
|
+
if (options?.childRunId !== void 0 && attempt.childRunId !== options.childRunId) return false;
|
|
56
|
+
if (options?.workflowName !== void 0 && attempt.workflowName !== options.workflowName) return false;
|
|
57
|
+
if (options?.workflowVersion !== void 0 && attempt.workflowVersion !== options.workflowVersion) return false;
|
|
58
|
+
if (options?.messageId !== void 0 && attempt.messageId !== options.messageId) return false;
|
|
59
|
+
if (options?.targetRunId !== void 0 && attempt.targetRunId !== options.targetRunId) return false;
|
|
60
|
+
return matchesTimestampRange(attempt.createdAt, options?.createdAtFrom, options?.createdAtTo) && matchesTimestampRange(attempt.startedAt, options?.startedAtFrom, options?.startedAtTo) && matchesOptionalTimestampRange(attempt.finishedAt, options?.finishedAtFrom, options?.finishedAtTo) && matchesOptionalTimestampRange(attempt.until, options?.untilFrom, options?.untilTo) && matchesOptionalDurationRange(attempt.duration, options?.durationFrom, options?.durationTo) && matchesOptionalTimestampRange(attempt.timeoutAt, options?.timeoutAtFrom, options?.timeoutAtTo) && matchesOptionalTimestampRange(attempt.messageTimestamp, options?.messageTimestampFrom, options?.messageTimestampTo);
|
|
61
|
+
}
|
|
62
|
+
function matchesScheduleListOptions(schedule, options) {
|
|
63
|
+
if (options?.status !== void 0 && !matchesOptionalValue(schedule.status, options.status)) return false;
|
|
64
|
+
if (options?.workflowName !== void 0 && schedule.workflowName !== options.workflowName) return false;
|
|
65
|
+
if (options?.workflowVersion !== void 0 && schedule.workflowVersion !== options.workflowVersion) return false;
|
|
66
|
+
return true;
|
|
67
|
+
}
|
|
68
|
+
function matchesRunAttributeFilters(run, options) {
|
|
69
|
+
if (options === void 0) return true;
|
|
70
|
+
if (options.attributes !== void 0) {
|
|
71
|
+
for (const [key, value] of Object.entries(options.attributes)) if (!Object.is(run.attributes?.[key], value)) return false;
|
|
72
|
+
}
|
|
73
|
+
if (options.attributeExists !== void 0) {
|
|
74
|
+
if (!(Array.isArray(options.attributeExists) ? options.attributeExists : [options.attributeExists]).every((key) => run.attributes?.[key] !== void 0)) return false;
|
|
75
|
+
}
|
|
76
|
+
if (options.attributeMissing !== void 0) {
|
|
77
|
+
if (!(Array.isArray(options.attributeMissing) ? options.attributeMissing : [options.attributeMissing]).every((key) => run.attributes?.[key] === void 0)) return false;
|
|
78
|
+
}
|
|
79
|
+
return true;
|
|
80
|
+
}
|
|
81
|
+
//#endregion
|
|
82
|
+
export { getRequiredStoreRun, matchesScheduleListOptions, matchesWorkflowRunListOptions, matchesWorkflowStepAttemptListOptions, querySchedules, queryWorkflowRuns, queryWorkflowStepAttempts };
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { RunId } from "../types/run-id.mjs";
|
|
2
|
+
import { WorkflowPage } from "../types/pagination.mjs";
|
|
3
|
+
import { WorkflowListRunsOptions, WorkflowListRunsPageOptions } from "../types/run-list.mjs";
|
|
4
|
+
import { WorkerId } from "../types/worker-id.mjs";
|
|
5
|
+
import { WorkflowRunRecord } from "../types/run.mjs";
|
|
6
|
+
import { EventRecord } from "../types/events.mjs";
|
|
7
|
+
import { WorkflowClaimRunOptions, WorkflowStorage } from "../types/store.mjs";
|
|
8
|
+
//#region src/store/run.d.ts
|
|
9
|
+
interface WorkflowRunReader {
|
|
10
|
+
getRun(runId: RunId): Promise<WorkflowRunRecord | undefined>;
|
|
11
|
+
}
|
|
12
|
+
interface WorkflowRunClaimReader extends WorkflowRunReader {
|
|
13
|
+
listRuns(options?: WorkflowListRunsOptions): Promise<readonly WorkflowRunRecord[]>;
|
|
14
|
+
}
|
|
15
|
+
declare function updateRunIfCurrent(storage: WorkflowStorage, namespace: string, store: WorkflowRunReader, current: WorkflowRunRecord, next: WorkflowRunRecord): Promise<WorkflowRunRecord | undefined>;
|
|
16
|
+
declare function getWorkflowRun(storage: WorkflowStorage, namespace: string, runId: RunId): Promise<WorkflowRunRecord | undefined>;
|
|
17
|
+
declare function listWorkflowRuns(storage: WorkflowStorage, namespace: string, options?: WorkflowListRunsOptions): Promise<readonly WorkflowRunRecord[]>;
|
|
18
|
+
declare function listWorkflowRunsPage(storage: WorkflowStorage, namespace: string, options?: WorkflowListRunsPageOptions): Promise<WorkflowPage<WorkflowRunRecord>>;
|
|
19
|
+
declare function countWorkflowRuns(storage: WorkflowStorage, namespace: string, options?: WorkflowListRunsOptions): Promise<number>;
|
|
20
|
+
declare function updateWorkflowRun(storage: WorkflowStorage, namespace: string, store: WorkflowRunReader, run: WorkflowRunRecord): Promise<WorkflowRunRecord>;
|
|
21
|
+
declare function claimWorkflowRun(storage: WorkflowStorage, namespace: string, store: WorkflowRunClaimReader, options: WorkflowClaimRunOptions): Promise<WorkflowRunRecord | undefined>;
|
|
22
|
+
declare function extendWorkflowRunLease(storage: WorkflowStorage, namespace: string, store: WorkflowRunReader, options: {
|
|
23
|
+
readonly runId: RunId;
|
|
24
|
+
readonly workerId: string;
|
|
25
|
+
readonly attempts?: number;
|
|
26
|
+
readonly now: Temporal.Instant;
|
|
27
|
+
readonly leaseDuration: Temporal.Duration;
|
|
28
|
+
}): Promise<WorkflowRunRecord | undefined>;
|
|
29
|
+
declare function releaseWorkflowRunLease(storage: WorkflowStorage, namespace: string, store: WorkflowRunReader, options: {
|
|
30
|
+
readonly runId: RunId;
|
|
31
|
+
readonly workerId: string;
|
|
32
|
+
readonly attempts?: number;
|
|
33
|
+
readonly now: Temporal.Instant;
|
|
34
|
+
}): Promise<WorkflowRunRecord | undefined>;
|
|
35
|
+
declare function isSameReadableWorkflowRunRevision(latest: WorkflowRunRecord | undefined, current: WorkflowRunRecord, namespace: string): boolean;
|
|
36
|
+
declare function isStoredWorkflowRunRevisionCurrent(storage: WorkflowStorage, namespace: string, currentRun: WorkflowRunRecord): Promise<boolean>;
|
|
37
|
+
declare function getReadableStoredWorkflowRun(storage: WorkflowStorage, namespace: string, runId: RunId): Promise<WorkflowRunRecord | undefined>;
|
|
38
|
+
declare function requireReadableStoredWorkflowRun(storage: WorkflowStorage, namespace: string, runId: RunId): Promise<WorkflowRunRecord>;
|
|
39
|
+
declare function shouldRollbackAppendedEventForRun(namespace: string, run: WorkflowRunRecord | undefined, event: EventRecord): boolean;
|
|
40
|
+
declare function isWorkflowRunStaleForAppend(currentRun: WorkflowRunRecord, event: EventRecord): boolean;
|
|
41
|
+
declare function rollbackAppendedEventIfRunStale(storage: WorkflowStorage, namespace: string, runId: RunId, event: EventRecord, events: readonly EventRecord[]): Promise<boolean>;
|
|
42
|
+
declare function claimedRunPatch(run: WorkflowRunRecord, workerId: WorkerId, now: Temporal.Instant, leaseDuration: Temporal.Duration): WorkflowRunRecord;
|
|
43
|
+
declare function extendedRunLeasePatch(run: WorkflowRunRecord, now: Temporal.Instant, leaseDuration: Temporal.Duration): WorkflowRunRecord;
|
|
44
|
+
declare function releaseRunLeasePatch(run: WorkflowRunRecord, now: Temporal.Instant): WorkflowRunRecord;
|
|
45
|
+
declare function firstClaimableRun(store: WorkflowRunClaimReader, options: WorkflowClaimRunOptions, listOptions: WorkflowListRunsOptions): Promise<WorkflowRunRecord | undefined>;
|
|
46
|
+
declare function canClaimRun(run: WorkflowRunRecord, options: WorkflowClaimRunOptions, listOptions: WorkflowListRunsOptions): boolean;
|
|
47
|
+
declare function isStaleLeaseClaim(run: WorkflowRunRecord, now: Temporal.Instant): boolean;
|
|
48
|
+
//#endregion
|
|
49
|
+
export { canClaimRun, claimWorkflowRun, claimedRunPatch, countWorkflowRuns, extendWorkflowRunLease, extendedRunLeasePatch, firstClaimableRun, getReadableStoredWorkflowRun, getWorkflowRun, isSameReadableWorkflowRunRevision, isStaleLeaseClaim, isStoredWorkflowRunRevisionCurrent, isWorkflowRunStaleForAppend, listWorkflowRuns, listWorkflowRunsPage, releaseRunLeasePatch, releaseWorkflowRunLease, requireReadableStoredWorkflowRun, rollbackAppendedEventIfRunStale, shouldRollbackAppendedEventForRun, updateRunIfCurrent, updateWorkflowRun };
|