Skip to content

Rivet.Durable Objects reference

Source: packages/rivet/src/WorkflowEngine.ts Kind: Layer · Provides: WorkflowEngine

Effect’s WorkflowEngine on Rivet Durable Objects. Provide it to the workflows’ Layer (Workflows.layer(...)) in every runtime that starts or reads runs, and export that Layer from main so the pool’s nodes run it.

Every execution is one instance of the Durable Object class name, named after its workflow and execution id, with its journal in that instance’s SQLite. Its sleeps, signals and retries are durable callbacks (Rivet.DurableObjectCallbacks), so a run that waits uses no compute until it wakes. Built in a caller, the Layer deploys the class to pool and reaches executions through the control plane’s gateway; on a node of the pool, the same Layer hosts the class.

A run replays its workflow from the top: finished activities return their stored results, and the first unfinished one runs and is stored. Runs of one execution never overlap, and a run that fails or is cut short is retried.

layerWorkflowEngine: Running workflows on Rivet

Section titled “layerWorkflowEngine: Running workflows on Rivet”
import * as Docker from "@alchemy.run/docker";
import * as Rivet from "@alchemy.run/rivet";
import * as RivetDocker from "@alchemy.run/rivet/Docker";
import * as Workflows from "alchemy/Workflows";
import * as Effect from "effect/Effect";
import * as HttpServerResponse from "effect/http/HttpServerResponse";
import * as Layer from "effect/Layer";
import * as Schema from "effect/Schema";
import { Activity, Workflow } from "effect/workflow";
const ControlPlane = RivetDocker.SwarmControlPlane("Rivet");
const Default = RivetDocker.SwarmPool("Default").pipe(Layer.provide(ControlPlane));
export const SendWelcome = Workflow.make("SendWelcome", {
payload: { userId: Schema.String },
idempotencyKey: ({ userId }) => userId,
});
// The pool's nodes load this export to run the workflows.
export const WorkflowsLive = Workflows.layer(
SendWelcome,
Effect.fn(function* ({ userId }) {
yield* Activity.make({ name: "send", execute: Effect.log(`welcome, ${userId}`) });
}),
).pipe(
Layer.provideMerge(
Rivet.layerWorkflowEngine({
name: "Workflows",
main: import.meta.url,
handler: "WorkflowsLive",
pool: Default,
}),
),
);
export default class Api extends Docker.Service<Api>()(
"Api",
Effect.gen(function* () {
const cluster = yield* Rivet.Cluster;
return {
main: import.meta.url,
port: 3000,
networks: cluster.network?.kind === "docker" ? [{ name: cluster.network.name }] : [],
};
}).pipe(Effect.provide(ControlPlane)),
Effect.gen(function* () {
const welcome = yield* Workflows.RunWorkflow(SendWelcome);
return {
fetch: Effect.gen(function* () {
const run = yield* welcome.start({ userId: "u-1" });
return HttpServerResponse.text(run.executionId);
}),
};
}).pipe(Effect.provide(WorkflowsLive)),
) {}