# madflojo/tasks architecture

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

- Interactive diagram: https://gitdiagram.com/madflojo/tasks
- Repository: https://github.com/madflojo/tasks

## Overview

##### Main Components

a) Scheduler Core
- Central scheduler instance (created via tasks.New())
- Task management system
- Internal timing mechanism (using time.AfterFunc())

b) Task Management
- Task queue/storage
- Task execution engine
- Error handling pipeline

c) Goroutine Management
- Concurrent execution system
- Goroutine lifecycle handling

- [Public Interface] -> [Scheduler Core] -> [Task Execution]
- > [Task Management] -> [Goroutine Pool]
- > [Error Handling Pipeline]

##### Specific Elements

a) Interface Components (Top Layer):
- () method
- Stop() method
- Task struct definition
- Error handling interface

b) Scheduler Core (Middle Layer):
- Task scheduler mechanism
- Time management system
- Task state tracking

c) Execution Layer (Bottom Layer):
- Goroutine pool
- Task execution engine
- Error propagation system

##### Important Flows to Highlight

a) Task Lifecycle:
1. Task Creation/Registration
2. Scheduling
3. Execution
4. Error Handling
5. Cleanup (for RunOnce tasks)

b) Concurrent Execution:
- How multiple tasks run independently
- Goroutine management

##### Technical Details

a) Key Interfaces:
- TaskFunc func() error
- ErrFunc func(error)

b) Important Configuration Options:
- Interval (time.Duration)
- StartAfter (time.Time)
- RunOnce (bool)

## Diagram (Mermaid)

```mermaid
%%{init: {
  'theme': 'base',
  'themeVariables': {
    'primaryColor': '#2b2b2b',
    'primaryTextColor': '#fff',
    'primaryBorderColor': '#7C0000',
    'lineColor': '#F8B229',
    'secondaryColor': '#006100',
    'tertiaryColor': '#0000ff'
  }
}}%%

flowchart TB
    classDef interface fill:#2b2b2b,stroke:#333,stroke-width:2px,color:#fff
    classDef core fill:#006100,stroke:#333,stroke-width:2px,color:#fff
    classDef execution fill:#F8B229,stroke:#333,stroke-width:2px,color:#000
    classDef error fill:#7C0000,stroke:#333,stroke-width:2px,color:#fff
    classDef storage fill:#666,stroke:#333,stroke-width:2px,color:#fff

    subgraph Public_Interface["Public Interface Layer"]
        API[("Public API")]:::interface
        TaskStruct["Task Struct"]:::interface
        ErrorInterface["Error Interface"]:::interface
    end

    subgraph Core_Layer["Core Scheduler Layer"]
        Scheduler["Scheduler Core"]:::core
        TaskMgmt["Task Management"]:::core
        TimeSystem["Time Management"]:::core
        StateTracker["State Tracking"]:::storage
    end

    subgraph Execution_Layer["Execution Layer"]
        GoroutinePool["Goroutine Pool"]:::execution
        TaskExec["Task Execution Engine"]:::execution
        ErrorHandler["Error Handler"]:::error
    end

    API --> Scheduler
    API --> TaskMgmt
    TaskStruct --> TaskMgmt
    ErrorInterface --> ErrorHandler
    
    Scheduler --> TimeSystem
    Scheduler --> StateTracker
    TaskMgmt --> GoroutinePool
    TaskMgmt --> TaskExec
    
    GoroutinePool --> ErrorHandler
    TaskExec --> ErrorHandler
    TimeSystem --> TaskExec
    
    %% Click events for component mapping
    click Scheduler "https://github.com/madflojo/tasks/blob/main/tasks.go"
    click TaskMgmt "https://github.com/madflojo/tasks/blob/main/tasks.go"
    click GoroutinePool "https://github.com/madflojo/tasks/blob/main/tasks_benchmarks_test.go"
    click TaskExec "https://github.com/madflojo/tasks/blob/main/tasks_test.go"

    %% Legend
    subgraph Legend
        I["Interface Component"]:::interface
        C["Core Component"]:::core
        E["Execution Component"]:::execution
        ER["Error Handling"]:::error
        S["Storage/State"]:::storage
    end
```
