@convex-dev/workflow
v0.1.2
Published
Convex component for durably executing workflows.
Downloads
127
Readme
Convex Workflow (Beta)
Have you ever wanted to sleep for 7 days within a Convex function? Find yourself in callback hell chaining together function calls through queues? Sick of manual state management and scheduling in long-lived workflows? Convex workflows might just be what you're looking for.
import { WorkflowManager } from "@convex-dev/workflow";
import { components } from "./_generated/server";
export const workflow = new WorkflowManager(components.workflow);
export const exampleWorkflow = workflow.define({
args: {
storageId: v.id("_storage"),
},
handler: async (step, args) => {
const transcription = await step.runAction(
internal.index.computeTranscription,
{ storageId: args.storageId },
);
// Sleep for a month after computing the transcription.
await step.sleep(30 * 24 * 60 * 60 * 1000);
const embedding = await step.runAction(internal.index.computeEmbedding, {
transcription,
});
console.log(embedding);
},
});
This component adds durably executed workflows to Convex. Combine Convex queries, mutations, and actions into long-lived workflows, and the system will always fully execute a workflow to completion.
This component is currently in beta and may have some rough edges. Open a GitHub issue with any feedback or bugs you find.
Installation
First, add @convex-dev/workflow
to your Convex project:
npm install @convex-dev/workflow
Then, install the component within your convex/convex.config.ts
file:
// convex/convex.config.ts
import workflow from "@convex-dev/workflow/convex.config.js";
import { defineApp } from "convex/server";
const app = defineApp();
app.use(workflow);
export default app;
Finally, create a workflow manager within your convex/
folder, and point it
to the installed component:
// convex/index.ts
import { WorkflowManager } from "@convex-dev/workflow";
import { components } from "./_generated/server";
export const workflow = new WorkflowManager(components.workflow);
Usage
The first step is to define a workflow using workflow.define()
. This function
is designed to feel like a Convex action but with a few restrictions:
- The workflow can optionally declare an argument validator.
- The workflow runs in the background, so it can't return a value.
- The workflow must be deterministic, so it should implement most of its logic
by calling out to other Convex functions. We will be lifting some of these
restrictions over time by implementing
Math.random()
,Date.now()
, andfetch
within our workflow environment.
export const exampleWorkflow = workflow.define({
args: { name: v.string() },
handler: async (step, args) => {
const queryResult = await step.runQuery(
internal.example.exampleQuery,
args,
);
const actionResult = await step.runAction(
internal.example.exampleAction,
args,
);
console.log(queryResult, actionResult);
},
});
export const exampleQuery = internalQuery({
args: { name: v.string() },
handler: async (ctx, args) => {
return `The query says... Hi ${args.name}!`;
},
});
export const exampleAction = internalAction({
args: { name: v.string() },
handler: async (ctx, args) => {
return `The action says... Hi ${args.name}!`;
},
});
Once you've defined a workflow, you can start it from a mutation or action
using workflow.start()
.
export const kickoffWorkflow = mutation({
handler: async (ctx) => {
const workflowId = await workflow.start(
ctx,
internal.example.exampleWorkflow,
{
name: "James",
},
);
},
});
The workflow.start()
method returns a WorkflowId
, which can then be used for querying
a workflow's status.
export const kickoffWorkflow = action({
handler: async (ctx) => {
const workflowId = await workflow.start(
ctx,
internal.example.exampleWorkflow,
{
name: "James",
},
);
await new Promise((resolve) => setTimeout(resolve, 1000));
const status = await workflow.status(ctx, workflowId);
console.log("Workflow status after 1s", status);
},
});
You can also cancel a workflow with workflow.cancel()
, halting the workflow's execution immmediately. In-progress calls to step.runAction()
, however, only have best-effort cancelation.
export const kickoffWorkflow = action({
handler: async (ctx) => {
const workflowId = await workflow.start(
ctx,
internal.example.exampleWorkflow,
{
name: "James",
},
);
await new Promise((resolve) => setTimeout(resolve, 1000));
// Cancel the workflow after 1 second.
await workflow.cancel(ctx, workflowId);
},
});
After a workflow has completed, you can clean up its storage with workflow.cleanup()
.
Completed workflows are not automatically cleaned up by the system.
export const kickoffWorkflow = action({
handler: async (ctx) => {
const workflowId = await workflow.start(
ctx,
internal.example.exampleWorkflow,
{
name: "James",
},
);
try {
while (true) {
const status = await workflow.status(ctx, workflowId);
if (status.type === "inProgress") {
await new Promise((resolve) => setTimeout(resolve, 1000));
continue;
}
console.log("Workflow completed with status:", status);
break;
}
} finally {
await workflow.cleanup(ctx, workflowId);
}
},
});
Limitations
Convex workflows is a beta product currently under active development. Here are a few limitations to keep in mind:
- Steps can only take in and return a total of 1 MiB of data within a single workflow execution. If you run into journal size limits, you can work around this by storing results within your worker functions and then passing IDs around within the the workflow.
console.log()
isn't currently captured, so you may see duplicate log lines within your Convex dashboard.- We currently do not collect backtraces from within function calls from workflows.
- If you need to use side effects like
fetch
,Math.random()
, orDate.now()
, you'll need to define a separate Convex action, perform the side effects there, and then call that action from the workflow withstep.runAction()
.
🧑🏫 What is Convex?
Convex is a hosted backend platform with a
built-in database that lets you write your
database schema and
server functions in
TypeScript. Server-side database
queries automatically
cache and
subscribe to data, powering a
realtime useQuery
hook in our
React client. There are also clients for
Python,
Rust,
ReactNative, and
Node, as well as a straightforward
HTTP API.
The database supports NoSQL-style documents with opt-in schema validation, relationships and custom indexes (including on fields in nested objects).
The
query
and
mutation
server functions have transactional,
low latency access to the database and leverage our
v8
runtime with
determinism guardrails
to provide the strongest ACID guarantees on the market:
immediate consistency,
serializable isolation, and
automatic conflict resolution via
optimistic multi-version concurrency control (OCC / MVCC).
The action
server functions have
access to external APIs and enable other side-effects and non-determinism in
either our
optimized v8
runtime or a more
flexible node
runtime.
Functions can run in the background via scheduling and cron jobs.
Development is cloud-first, with hot reloads for server function editing via the CLI, preview deployments, logging and exception reporting integrations, There is a dashboard UI to browse and edit data, edit environment variables, view logs, run server functions, and more.
There are built-in features for reactive pagination, file storage, reactive text search, vector search, https endpoints (for webhooks), snapshot import/export, streaming import/export, and runtime validation for function arguments and database data.
Everything scales automatically, and it’s free to start.