# geofmureithi/apalis architecture

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

- Interactive diagram: https://gitdiagram.com/geofmureithi/apalis
- Repository: https://github.com/geofmureithi/apalis

## Overview

##### Project Type & Purpose
- apalis is a Rust workspace providing a modular background-job and message-processing framework. It ships as a collection of crates:
- apalis-core (the in-memory engine & abstractions)
- apalis-cron (cron-job source)
- apalis-redis (Redis backend)
- apalis-sql  (SQL backends: SQLite, Postgres, MySQL)
- It is not a single runnable service but a library/toolkit that users embed in their own async apps (Tokio, async-std, etc.) to produce/consume jobs.

##### File Structure Highlights
- packages/… holds each crate’s source (core, cron, redis, sql).
- src/layers/… inside apalis core exposes built-in Tower middleware (tracing, retry, sentry, prometheus, catch-panic).
- examples/… demonstrates real-world usage: actix-web, axum, REST API, cron pipelines, stepped tasks, different storage backends.
- Top-level Cargo.toml defines the workspace.

##### Key Architecture Concepts
- **Core Service Pipeline**: apalis-core defines a Tower Service that:
1. Polls a storage backend for new jobs
2. Passes each job through a stack of middleware layers (tracing, retry, timeout, etc.)
3. Invokes the user’s async handler
4. Reports progress and final status back to the backend
- **Pluggable Storage Backends**: apalis-redis and apalis-sql implement a Storage trait for persisting and retrieving jobs. apalis-cron acts as a job _source_, piping scheduled entries into another backend.
- **Builder Pattern**: WorkerBuilder configures name, concurrency, data, middleware toggles, storage backend, then yields a running worker.
- **Examples & Integration Points**: Users spawn producers (e.g., HTTP route, cron scheduler) that push jobs into the chosen storage. They run one or more worker processes (in the same binary or separate) to consume.

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Producers
    subgraph "Producers" 
        direction TB
        HTTP["HTTP Server (Job Producer)"]:::producer
        CronSource["Cron Source"]:::producer
    end

    %% Core Engine
    subgraph "apalis-core (Core Engine & Abstractions)" 
        direction TB
        CoreLib["apalis-core\nsrc/lib.rs"]:::core
        ServicePipeline["Service Pipeline\n(src/lib.rs & src/layers/mod.rs)"]:::core
        subgraph "Middleware Stack"
            direction TB
            Tracing["tracing"]:::core
            Retry["retry"]:::core
            Sentry["sentry"]:::core
            Prometheus["prometheus"]:::core
            CatchPanic["catch-panic"]:::core
        end
        WorkerBuilder["WorkerBuilder"]:::core
        StorageTrait["Storage Trait"]:::core
        InMemory["In-Memory Storage"]:::storage
        Poller["Poller & Controller"]:::core
        WorkerLogic["Worker Logic"]:::core
    end

    %% Storage Backends
    subgraph "Storage Backends"
        direction TB
        Redis["RedisStorage"]:::storage
        SQL["SQLStorage"]:::storage
        subgraph "SQL Engines"
            direction TB
            Postgres["Postgres"]:::storage
            MySQL["MySQL"]:::storage
            SQLite["SQLite"]:::storage
            Migrations["migrations"]:::storage
        end
    end

    %% Observability and UI
    subgraph "Observability & UI"
        direction TB
        PromObs["Prometheus"]:::obs
        SentryObs["Sentry"]:::obs
        WebUI["Web UI / REST API"]:::producer
    end

    %% Examples
    subgraph "Examples"
        direction TB
        ExActix["actix-web"]:::example
        ExAxum["axum"]:::example
        ExCron["cron pipeline"]:::example
        ExREST["REST API"]:::example
        Workspace["Cargo.toml"]:::example
    end

    %% Relationships
    HTTP -->|push| Redis
    HTTP -->|push| SQL
    HTTP -->|push| InMemory
    CronSource -->|schedule jobs| Redis
    CronSource -->|schedule jobs| SQL
    CronSource -->|schedule jobs| InMemory

    Redis -->|deliver job| ServicePipeline
    SQL -->|deliver job| ServicePipeline
    InMemory -->|deliver job| ServicePipeline

    ServicePipeline -->|invoke| WorkerLogic
    WorkerLogic -->|update status| Redis
    WorkerLogic -->|update status| SQL
    WorkerLogic -->|update status| InMemory

    ServicePipeline -->|metrics/logs| PromObs
    ServicePipeline -->|metrics/logs| SentryObs

    WebUI -->|REST calls| Redis
    WebUI -->|REST calls| SQL
    WebUI -->|REST calls| InMemory
    WebUI -->|REST calls| ServicePipeline

    %% Core wiring
    CoreLib --> ServicePipeline
    ServicePipeline --> Tracing
    ServicePipeline --> Retry
    ServicePipeline --> Sentry
    ServicePipeline --> Prometheus
    ServicePipeline --> CatchPanic
    WorkerBuilder --> ServicePipeline
    WorkerBuilder --> StorageTrait
    StorageTrait --> Redis
    StorageTrait --> SQL
    StorageTrait --> InMemory
    Poller --> ServicePipeline
    Poller --> Redis
    Poller --> SQL
    Poller --> InMemory

    %% Examples links
    ExActix --> WorkerBuilder
    ExAxum --> WorkerBuilder
    ExCron --> CronSource
    ExREST --> HTTP

    %% Click Events
    click CoreLib "https://github.com/geofmureithi/apalis/blob/main/packages/apalis-core/src/lib.rs"
    click ServicePipeline "https://github.com/geofmureithi/apalis/blob/main/packages/apalis-core/src/lib.rs"
    click ServicePipeline "https://github.com/geofmureithi/apalis/blob/main/packages/apalis-core/src/layers/mod.rs"
    click Tracing "https://github.com/geofmureithi/apalis/tree/main/src/layers/tracing"
    click Retry "https://github.com/geofmureithi/apalis/tree/main/src/layers/retry"
    click Sentry "https://github.com/geofmureithi/apalis/tree/main/src/layers/sentry"
    click Prometheus "https://github.com/geofmureithi/apalis/tree/main/src/layers/prometheus"
    click CatchPanic "https://github.com/geofmureithi/apalis/tree/main/src/layers/catch_panic"
    click WorkerBuilder "https://github.com/geofmureithi/apalis/blob/main/packages/apalis-core/src/builder.rs"
    click StorageTrait "https://github.com/geofmureithi/apalis/blob/main/packages/apalis-core/src/storage/mod.rs"
    click InMemory "https://github.com/geofmureithi/apalis/blob/main/packages/apalis-core/src/memory.rs"
    click Poller "https://github.com/geofmureithi/apalis/blob/main/packages/apalis-core/src/poller/mod.rs"
    click Poller "https://github.com/geofmureithi/apalis/blob/main/packages/apalis-core/src/poller/controller.rs"
    click WorkerLogic "https://github.com/geofmureithi/apalis/blob/main/packages/apalis-core/src/worker/mod.rs"
    click CronSource "https://github.com/geofmureithi/apalis/blob/main/packages/apalis-cron/src/lib.rs"
    click CronSource "https://github.com/geofmureithi/apalis/blob/main/packages/apalis-cron/src/pipe.rs"
    click Redis "https://github.com/geofmureithi/apalis/blob/main/packages/apalis-redis/src/storage.rs"
    click SQL "https://github.com/geofmureithi/apalis/blob/main/packages/apalis-sql/src/mysql.rs"
    click Postgres "https://github.com/geofmureithi/apalis/blob/main/packages/apalis-sql/src/postgres.rs"
    click MySQL "https://github.com/geofmureithi/apalis/blob/main/packages/apalis-sql/src/mysql.rs"
    click SQLite "https://github.com/geofmureithi/apalis/blob/main/packages/apalis-sql/src/sqlite.rs"
    click Migrations "https://github.com/geofmureithi/apalis/tree/main/packages/apalis-sql/migrations/"
    click ExActix "https://github.com/geofmureithi/apalis/blob/main/examples/actix-web/src/main.rs"
    click ExAxum "https://github.com/geofmureithi/apalis/blob/main/examples/axum/src/main.rs"
    click ExCron "https://github.com/geofmureithi/apalis/blob/main/examples/cron/src/main.rs"
    click ExREST "https://github.com/geofmureithi/apalis/blob/main/examples/rest-api/src/main.rs"
    click Workspace "https://github.com/geofmureithi/apalis/blob/main/Cargo.toml"

    %% Styles
    classDef core fill:#cce5ff,stroke:#004085
    classDef storage fill:#d4edda,stroke:#155724
    classDef producer fill:#fff3cd,stroke:#856404
    classDef obs fill:#e2e3e5,stroke:#6c757d
    classDef example fill:#f8d7da,stroke:#721c24
```
