allanhvam / simple-workflows

Workflows as code in TypeScript
MIT License
0 stars 0 forks source link

simple-workflows

Simple repeatable workflows and activities as code.

Goals:

History/state can be serialized to:

Inspired by Azure Durable Function and Temporal.

Activities

All activities is just normal functions that return promises, but must be idempotent and both args and return value must be serializable.

Example activity:

export async function greet(name: string): Promise<string> {
    return `Hello, ${name}!`;
}

Workflows

Workflows is build by activities. When activities is used in workflows, they must be passed to the proxyActivities function:

import * as activities from '../activities';
import { proxyActivities } from "simple-workflows";

const { greet } = proxyActivities(activities, {});

export async function greetWorkflow(name: string): Promise<string> {
    return await greet(name);
}

Getting started

import { WorkflowWorker } from "simple-workflows";

const worker = WorkflowWorker.getInstance();

const handle = await worker.start(greetWorkflow, {
    args: ["debug"],
    workflowId: "debug",
});

console.log(`Started workflow ${handle.workflowId}`);

let result = await handle.result();

Custom serialization

By default standard JSON serialization is used, the serialization can be customized by setting serializer on the store eg.:

import superjson from 'superjson';

const worker = WorkflowWorker.getInstance();
const store = new DurableFunctionsWorkflowHistoryStore({
  connectionString: "UseDevelopmentStorage=true",
  serializer: superjson,
});
worker.store = store;

Limitations