Skip to content

sam-goodwin/mini-workflow

Repository files navigation

mini-workflow

This is a mini Temporal-like workflow engine for TypeScript implemented with pluggable backends for AWS (CloudFlare coming soon).

Caution

The code was build in a day and is not fully tested. The e2e example works, but there may be bugs.

Example

Usage

SST Config

const engine = new WorkflowEngine("WorkflowEngine", {
  worker: "example/index.execute",
  orchestrator: "example/index.orchestrate",
});

App Code

import { workflow } from "mini-workflow";

// a workflow is implemented as a simple function accepting context and arbitrary parameters
export const uploadObjectWorkflow = workflow(
  "uploadObjectWorkflow",
  async (ctx, key: string, data: string) => {
    // ctx.sleep will sleep (pause the workflow) for 1 second
    await ctx.sleep(1);

    // ctx.task will asynchronously (and durably, with at-least-once delivery) run the provided function in the worker Lambda Function
    await ctx.task(async () => {
      await s3Client.send(
        new PutObjectCommand({
          Bucket: bucketName,
          Key: key,
          Body: data,
        })
      );
    });

    const response = await ctx.task(async () => {
      const response = await s3Client.send(
        new GetObjectCommand({
          Bucket: bucketName,
          Key: key,
        })
      );

      return response.Body?.transformToString() ?? "";
    });

    return response;
  }
);

Debug

Replay events in a Javascript Debug Terminal to step-through debug a workflow that ran in AWS (or is still running).

npx tsx ./src/cli.ts replay \
  --main ./example/workflow.ts \
  --bucket-name <bucket-name>  \
  --execution-id <execution-id>
replay.mp4

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published