# glamboyosa/swig architecture

> Architecture diagram of the GitHub repository glamboyosa/swig, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-08-19). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/glamboyosa/swig
- Repository: https://github.com/glamboyosa/swig

## Overview

#### Purpose and Entry Points
Swig is an embeddable Go library for PostgreSQL-backed background jobs, not a standalone service. Applications construct a client with `swig.NewSwig`, register worker implementations, start queue processors, and enqueue typed jobs. Core public API and orchestration are concentrated in `swig.go`; runnable integration patterns are in `examples/pgx/main.go` and `examples/sql/main.go`.

#### Client Lifecycle and Queue Orchestration
The Swig client owns initialization, queue configuration, processing startup, graceful stop, and database cleanup. Queue configurations define independent worker pools for default and priority queues, including concurrency limits. `swig.go` is the lifecycle boundary; `workers/workers.go` supplies the worker execution/registry layer.

#### Worker Registry and Job Dispatch
Application workers provide a stable job name, JSON-serializable arguments, and `Process(context.Context) error`. A registry maps persisted job kinds back to registered Go worker types so payloads can be deserialized and dispatched safely. This boundary separates user-defined work from queue infrastructure: `workers/workers.go`, `swig.go`.

#### Enqueueing and Transactional Boundary
Jobs may be inserted individually or in batches, either directly or through a caller-owned database transaction. This allows application writes and job creation to commit or roll back atomically. The public enqueue APIs reside in `swig.go`; driver transaction abstractions and SQL-facing contracts are in `drivers/driver.go` and `drivers/sql.go`.

#### PostgreSQL Persistence, Concurrency, and Recovery
PostgreSQL is the required durable store and coordination system. Processing uses row locking with `SKIP LOCK` to distribute jobs across workers/processes without duplicate claims; advisory locks provide leader election. Persisted job state supports scheduling, priorities, retries, errors, and status transitions. Database-specific implementation is behind `drivers/driver.go`, with concrete implementations in `drivers/pgx.go` and `drivers/sql.go`.

#### Driver Abstraction and External Dependencies
Swig supports two PostgreSQL access runtimes: native `pgx` pools (recommended, including LISTEN/NOTIFY support) and Go `database/sql` (with the external `lib/pq` driver for notifications). The driver interface isolates client/worker orchestration from connection, transaction, notification, and batch-insert details: `drivers/driver.go`, `drivers/pgx.go`, `drivers/sql.go`. Jobs use UUID utilities from `pkg/uuid.go`.

#### Documentation Site
A separate Next.js/TypeScript documentation application publishes library guidance and API content; it is not part of job execution. Its docs routing and MDX source pipeline are rooted at `swig-docs/src/app/docs/[[...slug]]/page.tsx`, `swig-docs/src/lib/source.ts`, and `swig-docs/content/docs`.

Main entry points: [`swig.go`](https://github.com/glamboyosa/swig/blob/main/swig.go) (Swig Client & API), [`drivers/driver.go`](https://github.com/glamboyosa/swig/blob/main/drivers/driver.go) (Driver Contract), [`workers/workers.go`](https://github.com/glamboyosa/swig/blob/main/workers/workers.go) (Worker Registry & Dispatch), [`drivers/pgx.go`](https://github.com/glamboyosa/swig/blob/main/drivers/pgx.go) (pgx Driver)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_library["Go Job Library"]
  node_swig_client["Swig Client &amp; API<br/>lifecycle API<br/>[swig.go]"]
  node_queue_pools["Queue Worker Pools<br/>orchestration<br/>[swig.go]"]
  node_worker_registry["Worker Registry &amp; Dispatch<br/>execution layer<br/>[workers.go]"]
  node_driver_contract{{"Driver Contract<br/>database abstraction<br/>[driver.go]"}}
  node_sql_contract["SQL Transaction Contract<br/>SQL-facing contract<br/>[sql.go]"]
  node_pgx_driver["pgx Driver<br/>PostgreSQL runtime<br/>[pgx.go]"]
  node_database_sql_driver["database/sql Driver<br/>PostgreSQL runtime<br/>[sql.go]"]
  node_job_state["Job State &amp; Recovery<br/>persistence model<br/>[driver.go]"]
  node_pgx_example["pgx Integration Example<br/>example application<br/>[main.go]"]
  node_sql_example["SQL Integration Example<br/>example application<br/>[main.go]"]
end

subgraph group_docs["Documentation Site"]
  node_docs_route["Docs Route<br/>Next.js route<br/>[page.tsx]"]
  node_docs_source["MDX Source Loader<br/>content pipeline<br/>[source.ts]"]
  node_docs_content["MDX Documentation<br/>content"]
end

node_application["Host Application<br/>consumer"]
node_application_workers["Application Workers<br/>user extension"]
node_postgres[("PostgreSQL<br/>durable store")]

node_application -->|"constructs and controls"| node_swig_client
node_application -->|"implements"| node_application_workers
node_application_workers -->|"registered as job kinds"| node_worker_registry
node_swig_client -->|"registers and dispatches through"| node_worker_registry
node_swig_client -->|"starts and stops"| node_queue_pools
node_queue_pools -->|"claims and executes jobs"| node_worker_registry
node_swig_client -->|"enqueues and manages through"| node_driver_contract
node_queue_pools -->|"coordinates processing through"| node_driver_contract
node_sql_contract -->|"provides transaction semantics"| node_driver_contract
node_driver_contract -->|"implemented by"| node_pgx_driver
node_driver_contract -->|"implemented by"| node_database_sql_driver
node_pgx_driver -->|"persists and coordinates"| node_postgres
node_database_sql_driver -->|"persists and coordinates"| node_postgres
node_driver_contract -->|"reads and transitions"| node_job_state
node_job_state -->|"stored in"| node_postgres
node_docs_route -->|"loads pages from"| node_docs_source
node_docs_source -->|"parses"| node_docs_content
node_pgx_example -.->|"demonstrates"| node_swig_client
node_sql_example -.->|"demonstrates"| node_swig_client

click node_swig_client "https://github.com/glamboyosa/swig/blob/main/swig.go"
click node_queue_pools "https://github.com/glamboyosa/swig/blob/main/swig.go"
click node_worker_registry "https://github.com/glamboyosa/swig/blob/main/workers/workers.go"
click node_driver_contract "https://github.com/glamboyosa/swig/blob/main/drivers/driver.go"
click node_sql_contract "https://github.com/glamboyosa/swig/blob/main/drivers/sql.go"
click node_pgx_driver "https://github.com/glamboyosa/swig/blob/main/drivers/pgx.go"
click node_database_sql_driver "https://github.com/glamboyosa/swig/blob/main/drivers/sql.go"
click node_job_state "https://github.com/glamboyosa/swig/blob/main/drivers/driver.go"
click node_docs_route "https://github.com/glamboyosa/swig/blob/main/swig-docs/src/app/docs/%5B%5B...slug%5D%5D/page.tsx"
click node_docs_source "https://github.com/glamboyosa/swig/blob/main/swig-docs/src/lib/source.ts"
click node_docs_content "https://github.com/glamboyosa/swig/tree/main/swig-docs/content/docs"
click node_pgx_example "https://github.com/glamboyosa/swig/blob/main/examples/pgx/main.go"
click node_sql_example "https://github.com/glamboyosa/swig/blob/main/examples/sql/main.go"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_swig_client,node_queue_pools,node_worker_registry,node_driver_contract,node_sql_contract,node_pgx_driver,node_database_sql_driver,node_job_state,node_pgx_example,node_sql_example toneBlue
class node_docs_route,node_docs_source,node_docs_content toneAmber
class node_application,node_application_workers,node_postgres toneNeutral
```

## Components

### Go Job Library

Embeddable PostgreSQL job processing runtime.

- [Swig Client & API](https://github.com/glamboyosa/swig/blob/main/swig.go): lifecycle API · Owns initialization, queues, processing lifecycle, and enqueue APIs. · `swig.go`
- [Queue Worker Pools](https://github.com/glamboyosa/swig/blob/main/swig.go): orchestration · Runs independent default and priority queue pools with configured concurrency. · `swig.go`
- [Worker Registry & Dispatch](https://github.com/glamboyosa/swig/blob/main/workers/workers.go): execution layer · Maps persisted job kinds to Go workers and dispatches decoded payloads. · `workers/workers.go`
- [Driver Contract](https://github.com/glamboyosa/swig/blob/main/drivers/driver.go): database abstraction · Isolates transaction, notification, claim, and batch persistence operations. · `drivers/driver.go`
- [SQL Transaction Contract](https://github.com/glamboyosa/swig/blob/main/drivers/sql.go): SQL-facing contract · Supports caller-owned transactional enqueueing. · `drivers/sql.go`
- [pgx Driver](https://github.com/glamboyosa/swig/blob/main/drivers/pgx.go): PostgreSQL runtime · Native pool implementation with LISTEN/NOTIFY support. · `drivers/pgx.go`
- [database/sql Driver](https://github.com/glamboyosa/swig/blob/main/drivers/sql.go): PostgreSQL runtime · SQL implementation using lib/pq notifications. · `drivers/sql.go`
- [Job State & Recovery](https://github.com/glamboyosa/swig/blob/main/drivers/driver.go): persistence model · Tracks schedules, priorities, retries, errors, and status transitions. · `drivers/driver.go`
- [pgx Integration Example](https://github.com/glamboyosa/swig/blob/main/examples/pgx/main.go): example application · `examples/pgx/main.go`
- [SQL Integration Example](https://github.com/glamboyosa/swig/blob/main/examples/sql/main.go): example application · `examples/sql/main.go`

### Documentation Site

Separate Next.js documentation application.

- [Docs Route](https://github.com/glamboyosa/swig/blob/main/swig-docs/src/app/docs/%5B%5B...slug%5D%5D/page.tsx): Next.js route · Resolves documentation pages by slug. · `swig-docs/src/app/docs/[[...slug]]/page.tsx`
- [MDX Source Loader](https://github.com/glamboyosa/swig/blob/main/swig-docs/src/lib/source.ts): content pipeline · Loads and structures documentation content. · `swig-docs/src/lib/source.ts`
- [MDX Documentation](https://github.com/glamboyosa/swig/tree/main/swig-docs/content/docs): content · `swig-docs/content/docs`

### Other components

- **Host Application**: consumer · Constructs Swig, registers workers, and enqueues jobs.
- **Application Workers**: user extension · Provide job names, JSON arguments, and Process implementations.
- **PostgreSQL**: durable store · Stores job state and coordinates claims and leadership.

## Connections

- Host Application → Swig Client & API: constructs and controls
- Host Application → Application Workers: implements
- Application Workers → Worker Registry & Dispatch: registered as job kinds
- Swig Client & API → Worker Registry & Dispatch: registers and dispatches through
- Swig Client & API → Queue Worker Pools: starts and stops
- Queue Worker Pools → Worker Registry & Dispatch: claims and executes jobs
- Swig Client & API → Driver Contract: enqueues and manages through
- Queue Worker Pools → Driver Contract: coordinates processing through
- SQL Transaction Contract → Driver Contract: provides transaction semantics
- Driver Contract → pgx Driver: implemented by
- Driver Contract → database/sql Driver: implemented by
- pgx Driver → PostgreSQL: persists and coordinates
- database/sql Driver → PostgreSQL: persists and coordinates
- Driver Contract → Job State & Recovery: reads and transitions
- Job State & Recovery → PostgreSQL: stored in
- Docs Route → MDX Source Loader: loads pages from
- MDX Source Loader → MDX Documentation: parses
- pgx Integration Example → Swig Client & API: demonstrates
- SQL Integration Example → Swig Client & API: demonstrates
