# tuannh982/simple-workflow-go architecture

> Architecture diagram of the GitHub repository tuannh982/simple-workflow-go, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2024-12-29). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/tuannh982/simple-workflow-go
- Repository: https://github.com/tuannh982/simple-workflow-go

## Overview

- Client → Backend (Schedule workflows)
- Workers → Backend (Poll for tasks)
- Backend → Workers (Deliver tasks)

- Activity Results → Backend
- Workflow State → Backend
- Debug Information → Client

##### Key Subsystems (Group related components)

a. Execution Context
- Workflow Execution Context
- Activity Execution Context
- Context Propagators

b. Task Management
- Task Executors
- Task Processors
- Worker Threads

##### Important Elements

a. Data Converter Layer
- JSON data conversion between components

b. Promise/Future Pattern Implementation
- Activity Promises
- Timer Promises

c. Backend Storage Structure
- Events Table
- Tasks Table
- Workflow State Table

##### Specific Implementation Details

Notes or callouts for:
- Workflow Variable System (SetVar/GetVar)
- Activity Registration
- Error Handling Flow
- Retry Mechanisms

##### Architectural Patterns to Highlight

- Worker Pool Pattern
- Promise/Future Pattern
- Event Sourcing (History Events)
- Repository Pattern (PSQL Implementation)

**[Client Layer]**
- ↓
**[Worker Layer]**
- ↓
**[Core Runtime]**
- ↓
**[Backend Layer]**

Special Considerations:

##### How state is maintained and persisted

- How workflows are scheduled and executed
- How activities are distributed and processed
- How state is maintained and recovered
- How debugging and monitoring work

This will provide a clear understanding of the system's architecture while maintaining the right level of abstraction for a principal software engineer to understand both the high-level design and the important implementation details.

## Diagram (Mermaid)

```mermaid
graph TB
    %% Styles and classes
    classDef client fill:#b3d1ff,stroke:#333,stroke-width:2px
    classDef worker fill:#90EE90,stroke:#333,stroke-width:2px
    classDef backend fill:#FFA500,stroke:#333,stroke-width:2px
    classDef core fill:#DDA0DD,stroke:#333,stroke-width:2px
    classDef debug stroke-dasharray: 5 5
    classDef interface fill:#f9f,stroke:#333,stroke-width:1px

    %% Client Layer
    subgraph ClientLayer["Client Layer"]
        WC["Workflow Client"]
        WD["Workflow Debugger"]:::debug
        WRA["Workflow Result Awaiter"]
    end

    %% Worker Layer
    subgraph WorkerLayer["Worker Layer"]
        AW["Activity Workers"]
        WW["Workflow Workers"]
        WR["Worker Registry"]
    end

    %% Core Engine
    subgraph CoreEngine["Core Engine"]
        WR_["Workflow Runtime"]
        AE["Activity Executor"]
        TP["Task Processor"]
        DC["Data Converter"]
    end

    %% Backend Layer
    subgraph BackendLayer["Backend Layer"]
        PS["PSQL Storage"]
        TM["Task Management"]
        EH["Event History"]
        WS["Workflow State"]
    end

    %% Core Interfaces
    AI["Activity Interface"]:::interface
    WI["Workflow Interface"]:::interface
    BI["Backend Interface"]:::interface

    %% Relationships
    WC --> WR_
    WD -.-> WS
    WRA --> EH
    
    AW --> AE
    WW --> WR_
    WR --> TP
    
    WR_ --> DC
    AE --> TP
    TP --> TM
    
    TM --> PS
    EH --> PS
    WS --> PS

    AI --> AW
    WI --> WW
    BI --> PS

    %% Click events
    click WC "https://github.com/tuannh982/simple-workflow-go/blob/main/pkg/api/client/client.go"
    click WD "https://github.com/tuannh982/simple-workflow-go/blob/main/pkg/api/debug/workflow_debugger.go"
    click AW "https://github.com/tuannh982/simple-workflow-go/blob/main/pkg/worker/activity_worker/activity_worker.go"
    click WW "https://github.com/tuannh982/simple-workflow-go/blob/main/pkg/worker/workflow_worker/workflow_worker.go"
    click WR "https://github.com/tuannh982/simple-workflow-go/tree/main/pkg/registry"
    click PS "https://github.com/tuannh982/simple-workflow-go/blob/main/pkg/backend/psql/backend.go"
    click TM "https://github.com/tuannh982/simple-workflow-go/blob/main/pkg/backend/psql/persistent/task.go"
    click EH "https://github.com/tuannh982/simple-workflow-go/blob/main/pkg/backend/psql/persistent/history_event.go"
    click WS "https://github.com/tuannh982/simple-workflow-go/blob/main/pkg/backend/psql/persistent/workflow.go"
    click WR_ "https://github.com/tuannh982/simple-workflow-go/blob/main/internal/workflow/workflow_runtime.go"
    click AE "https://github.com/tuannh982/simple-workflow-go/blob/main/internal/activity/activity_task_executor.go"
    click TP "https://github.com/tuannh982/simple-workflow-go/blob/main/internal/activity/activity_task_processor.go"
    click DC "https://github.com/tuannh982/simple-workflow-go/blob/main/pkg/dataconverter/data_converter.go"
    click AI "https://github.com/tuannh982/simple-workflow-go/blob/main/pkg/types/activity.go"
    click WI "https://github.com/tuannh982/simple-workflow-go/blob/main/pkg/types/workflow.go"
    click BI "https://github.com/tuannh982/simple-workflow-go/blob/main/pkg/backend/backend.go"

    %% Legend
    subgraph Legend
        CL["Client Component"]:::client
        WL["Worker Component"]:::worker
        BL["Backend Component"]:::backend
        COL["Core Component"]:::core
        IL["Interface"]:::interface
        DL["Debug Component"]:::debug
    end

    %% Class assignments
    class WC,WRA client
    class AW,WW,WR worker
    class PS,TM,EH,WS backend
    class WR_,AE,TP,DC core
```
