PareshRadadiya/workflow-engine

A lightweight, in-memory workflow engine built with NestJS that executes tasks with dependencies, retries, timeouts, and parallel execution.

โ˜… 0Forks 0TypeScriptGitHub โ†—Compare

README

๐Ÿš€ Workflow Engine

A lightweight, in-memory workflow engine built with NestJS that executes tasks with dependencies, retries, timeouts, and parallel execution.

Features

  • โœ… Task dependencies and parallel execution
  • โœ… Configurable retries with exponential backoff
  • โœ… Task timeouts and error handling
  • โœ… Lifecycle events and logging
  • โœ… REST API with Swagger documentation
  • โœ… Decorator-based task definition (@TaskStep)
  • โœ… Comprehensive test coverage

Quick Start

# Install dependencies
npm install

# Start development server
npm run start:dev

# Run tests
npm test

API Endpoints

Method Endpoint Description
POST /workflow/run-simple Execute simple workflow
POST /workflow/run-dependencies Execute workflow with dependencies
POST /workflow/run-parallel Execute parallel workflow
POST /workflow/run-retry Execute workflow with retries
POST /workflow/run-decorated Execute decorated workflow

API Documentation: http://localhost:3000/api

Usage Examples

Basic Workflow

import { WorkflowEngineService } from './workflow/workflow-engine.service';

const workflow = [
  {
    id: 'fetchData',
    handler: async () => ({ users: [{ id: 1, name: 'John' }] }),
    retries: 2,
    timeoutMs: 1000,
  },
  {
    id: 'processData',
    dependencies: ['fetchData'],
    handler: async () => ({ processed: true }),
  },
];

const result = await workflowEngine.run(workflow);

Using Decorators

import { Injectable } from '@nestjs/common';
import { TaskStep } from './workflow/task.decorator';

@Injectable()
export class DataTasksService {
  @TaskStep({ id: 'fetchData', retries: 2 })
  async fetchData() {
    return { users: [{ id: 1, name: 'John' }] };
  }

  @TaskStep({ id: 'processData', dependencies: ['fetchData'] })
  async processData() {
    return { processed: true };
  }
}

Project Structure

src/workflow/
โ”œโ”€โ”€ workflow-engine.service.ts      # Core execution engine
โ”œโ”€โ”€ workflow-validator.service.ts   # Task validation
โ”œโ”€โ”€ retry-handler.service.ts        # Retry logic
โ”œโ”€โ”€ task-extractor.service.ts       # Decorator extraction
โ”œโ”€โ”€ workflow.controller.ts          # REST API
โ”œโ”€โ”€ interceptors/                   # Response formatting
โ”œโ”€โ”€ utils/                          # Utility functions
โ””โ”€โ”€ *.spec.ts                       # Tests

License

MIT

Contributors

PareshRadadiya

Issues