Repository navigation
feat(openworkflow): export the backend API and test suite #893
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
cliftonc
wants to merge
2
commits into
openworkflowdev:main
Choose a base branch
from
cliftonc:backend-api
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| --- | ||
| title: Custom Backends | ||
| description: Implement the Backend interface to store workflow state in your own database or driver, and check it with the backend test suite | ||
| --- | ||
|
|
||
| OpenWorkflow ships with [PostgreSQL](/docs/postgres) and [SQLite](/docs/sqlite) | ||
| backends. To store workflow state somewhere else, implement the `Backend` | ||
| interface from `openworkflow/backend` and pass it to the client and worker like | ||
| any other backend: | ||
|
|
||
| ```ts | ||
| import { MyBackend } from "./my-backend"; | ||
| import { OpenWorkflow } from "openworkflow"; | ||
|
|
||
| const backend = new MyBackend(); | ||
| const ow = new OpenWorkflow({ backend }); | ||
| ``` | ||
|
|
||
| <Warning> | ||
| The backend API is experimental. It can change in a minor release before 1.0 | ||
| (for example, when a feature adds new backend methods), so pin the | ||
| `openworkflow` versions your backend supports, such as `>=0.11.0 <0.12.0`. | ||
| </Warning> | ||
|
|
||
| ## The Backend Interface | ||
|
|
||
| `openworkflow/backend` exports the `Backend` interface and the types its methods | ||
| take and return: | ||
|
|
||
| ```ts | ||
| import type { | ||
| Backend, | ||
| ClaimWorkflowRunParams, | ||
| WorkflowRun, | ||
| } from "openworkflow/backend"; | ||
|
|
||
| export class MyBackend implements Backend { | ||
| async claimWorkflowRun( | ||
| params: ClaimWorkflowRunParams, | ||
| ): Promise<WorkflowRun | null> { | ||
| // ... | ||
| } | ||
|
|
||
| // ... | ||
| } | ||
| ``` | ||
|
|
||
| A backend owns its connections and its schema. `stop()` is called when the | ||
| client or worker shuts down. | ||
|
|
||
| ## Shared Helpers | ||
|
|
||
| The built-in backends keep the rules that don't depend on storage in shared | ||
| helpers. Custom backends should use the same helpers, so they behave the same | ||
| way: | ||
|
|
||
| | Helper | Use | | ||
| | ---------------------------------- | --------------------------------------------------------------------------- | | ||
| | `computeFailedWorkflowRunUpdate` | Decide whether a failed run is retried, and when, or fails | | ||
| | `prepareWorkflowRerun` | Validate a rerun and build the new run's parameters from the source run | | ||
| | `resolveCancelWorkflowRunConflict` | Choose the result when a cancel matched no row (already canceled, or final) | | ||
| | `nextScheduleOccurrence` | Compute a schedule's next run time from its cron and timezone | | ||
| | `isTerminalStatus` | Check whether a run status is final | | ||
| | `decodeListCursor` | Decode the `after` / `before` cursor of a list call | | ||
| | `buildPaginatedResponse` | Build a list response, with cursors, from `limit + 1` fetched rows | | ||
| | `toWorkflowRunCounts` | Turn `GROUP BY "status"` rows into counts for every status | | ||
| | `requireRow` | Throw `Failed to <operation>` when a mutation matched no row | | ||
| | `wrapError` | Wrap a driver error with a message, keeping the cause | | ||
|
|
||
| ## Testing Your Backend | ||
|
|
||
| `openworkflow/testing` exports the test suite that the built-in backends run. | ||
| It requires [Vitest](https://vitest.dev): | ||
|
|
||
| ```ts | ||
| import { MyBackend } from "./my-backend"; | ||
| import { randomUUID } from "node:crypto"; | ||
| import { testBackend } from "openworkflow/testing"; | ||
|
|
||
| testBackend({ | ||
| setup: async () => new MyBackend({ namespaceId: randomUUID() }), | ||
| teardown: async (backend) => { | ||
| await backend.stop(); | ||
| }, | ||
| }); | ||
| ``` | ||
|
|
||
| Each `setup()` should return a backend on its own namespace, so tests don't see | ||
| each other's runs. Run the suite against each OpenWorkflow version you support. | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| // Backend contract and the helpers the built-in backends share, for authors of | ||
| // custom backends. Experimental: may change in a minor release before 1.0. | ||
|
|
||
| // contract | ||
| export * from "./core/backend.js"; | ||
| export type { SerializedError } from "./core/error.js"; | ||
| export type { JsonValue } from "./core/json.js"; | ||
| export type { | ||
| WorkflowSchedule, | ||
| WorkflowScheduleDefinition, | ||
| } from "./core/schedule.js"; | ||
| export type { | ||
| StepAttempt, | ||
| StepAttemptContext, | ||
| StepAttemptStatus, | ||
| StepKind, | ||
| } from "./core/step-attempt.js"; | ||
| export type { WorkflowRun, WorkflowRunStatus } from "./core/workflow-run.js"; | ||
|
|
||
| // helpers | ||
| export type { Cursor } from "./core/cursor.js"; | ||
| export { | ||
| buildPaginatedResponse, | ||
| DEFAULT_PAGINATION_PAGE_SIZE, | ||
| decodeListCursor, | ||
| } from "./core/cursor.js"; | ||
| export { requireRow, wrapError } from "./core/error.js"; | ||
| export { prepareWorkflowRerun } from "./core/rerun.js"; | ||
| export { nextScheduleOccurrence } from "./core/schedule.js"; | ||
| export { computeFailedWorkflowRunUpdate } from "./core/workflow-definition.js"; | ||
| export { | ||
| isTerminalStatus, | ||
| resolveCancelWorkflowRunConflict, | ||
| } from "./core/workflow-run.js"; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| export type { TestBackendOptions } from "./testing/backend.testsuite.js"; | ||
| export { testBackend } from "./testing/backend.testsuite.js"; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The guide says the client or worker calls the backend’s
stop(), butOpenWorkflowhas no shutdown method andWorker.stop()never callsbackend.stop(). Readers relying on this promise can leave database connections open. Say that callers must explicitly callbackend.stop()after stopping their workers.