# sidekiq-cron/sidekiq-cron architecture

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

- Interactive diagram: https://gitdiagram.com/sidekiq-cron/sidekiq-cron
- Repository: https://github.com/sidekiq-cron/sidekiq-cron

## Overview

##### Project Type and Purpose
- This is a Ruby gem designed as an add-on to Sidekiq. Its main goal is to schedule cron-based jobs by running alongside Sidekiq workers.
- The README details how it interprets cron expressions (including second-precision and natural language via Fugit), manages job lifecycles (enable, disable, enqueue, etc.), supports namespacing of jobs, and integrates with both Sidekiq Worker and ActiveJob.
- Recognize it is not a full-stack web application; rather, it’s a library enhancement that plugs into an existing background processing setup.

##### File Structure
- Notice the “lib” directory holds the core logic. For example, “lib/sidekiq-cron.rb” and its subdirectories (like “lib/sidekiq/cron/”) hold components such as the Poller (responsible for checking and enqueuing jobs), Launcher, Schedule Loader, and Web UI extension code.
- The “test” directory, including integration and unit tests, validates the functionality.
- Docker support is present (via “docker/Dockerfile” and “docker/docker-compose.yml”) to enable running tests and possibly local environments, indicating a containerized development/testing aspect.
- The presence of configuration files (like Gemfile, Rakefile, and CI workflows under “.github”) shows automated testing/build pipelines.

##### Components and Relationships to Capture
- Main Sidekiq-Cron Components:
– Cron Poller: A dedicated thread (referenced in “lib/sidekiq/cron/poller.rb”) that periodically (default 30 seconds, configurable) checks which jobs need to be enqueued.
– Schedule Loader: This module (in “lib/sidekiq/cron/schedule_loader.rb”) reads job definitions from a YAML file (or a hash/array) and applies them to the Sidekiq queue system.
– Job Management: Components handling creation/destruction, enabling/disabling, and execution of jobs (jobs are defined with a cron expression, class name, namespace, etc.). Files like “job.rb” and “launcher.rb” are critical here.
– Namespacing and Localization: The system supports multiple namespaces—track how the jobs are grouped and identified. Localization files in “lib/sidekiq/cron/locales/” highlight additional features.
– Web UI Integration: Code in “lib/sidekiq/cron/web.rb” and “lib/sidekiq/cron/web_extension.rb” integrates cron job control into the Sidekiq Web interface.
- External/Supporting Components:
– Sidekiq Worker/ActiveJob: Though managed outside this gem, Sidekiq and optionally ActiveJob are the runtime executors of enqueued jobs.
– Redis: Implicitly vital, since Sidekiq (and thus Sidekiq-Cron) relies on Redis (using sorted sets) to ensure jobs are only enqueued once.
– Docker and CI Pipeline: Illustrate how the gem is containerized for testing/build purposes (docker-compose, GitHub Actions from “.github/workflows/ci.yml”).

##### Architectural Patterns and Design Principles
- Separation of Concerns is evident: the scheduling logic (Poller, Schedule Loader) is decoupled from the job execution (Sidekiq Workers/ActiveJob).
- Threaded design: The gem spawns its own thread to poll and schedule jobs alongside the normal Sidekiq process.
- Use of configuration and extensibility: The gem allows users to override default behaviors (polling interval, schedule file, etc.) via a configure block.
- Integration via mixins and monkeypatching: Notice “include Sidekiq::Worker” in job classes, and the extension of Sidekiq’s Web UI to add cron functionalities.

##### Elements
- Labels for each component:
- “Sidekiq Cron Poller”
- “Cron Schedule Loader”
- “Job Management/Launcher”
- “Namespacing Module & Localization Files”
- “Web UI Extension”
- “Sidekiq Worker/ActiveJob Executor”
- “Redis (persistent store for queues)”
- “Docker/CI Environment (for tests/build)”
- The polling cycle: Cron Poller periodically checks the Schedule Loader, which in turn interacts with the Job Management component.
- Data flow: From user-specified configuration (YAML file, in the case of scheduled jobs) to creation and updating in Redis.
- Interaction: Web UI invoking functions in the Job Management module.
- For internal modules (core gem libraries) vs. external integrations (Sidekiq, Redis, Docker CI).

##### Detailed and Essential Architectural Elements
- Dynamic nature of job scheduling (i.e., differentiating between “schedule” and “dynamic” jobs).
- Configuration customization (polling interval, namespace definitions, reschedule grace period) as factors influencing the scheduling behavior.
- Optionally, illustrate test and deployment flows using Docker and CI workflows.

## Diagram (Mermaid)

```mermaid
graph TD
    subgraph "Sidekiq Cron Core"
        G("Core Gem Bootstrap/Integration"):::internal
        A("User Configuration (YAML/dynamic)"):::internal
        B("Cron Schedule Loader"):::internal
        C("Job Management/Launcher"):::internal
        D("Sidekiq Cron Poller"):::internal
        E1("Namespacing Module"):::internal
        E2("Localization Files"):::internal
        F1("Web UI Integration"):::internal
        F2("Web UI Extension"):::internal
    end

    subgraph "External Services"
        H("Sidekiq Worker/ActiveJob Executor"):::external
        I("Redis (Queue Store)"):::external
    end

    subgraph "Deployment & CI"
        J1("Docker Environment"):::deployment
        J2("CI Pipeline"):::deployment
    end

    %% Bootstrap initializes core modules
    G -->|"init"| B
    G -->|"init"| C
    G -->|"init"| D
    G -->|"init"| E1
    G -->|"init"| E2
    G -->|"init"| F1
    G -->|"init"| F2

    %% Configuration flow
    A -->|"provides_config"| B

    %% Scheduling and job triggering flow
    B -->|"loads_schedule"| C
    D -->|"polls"| B
    D -->|"triggers"| C

    %% Web UI integration
    F1 -->|"invokes"| C
    F2 -->|"invokes"| C

    %% Job enqueuing and execution
    C -->|"enqueues"| I
    I -->|"processes"| H

    %% Deployment interactions
    G -->|"tested_in"| J1
    G -->|"tested_in"| J2

    %% Click Events
    click D "https://github.com/sidekiq-cron/sidekiq-cron/blob/master/lib/sidekiq/cron/poller.rb"
    click B "https://github.com/sidekiq-cron/sidekiq-cron/blob/master/lib/sidekiq/cron/schedule_loader.rb"
    click C "https://github.com/sidekiq-cron/sidekiq-cron/blob/master/lib/sidekiq/cron/launcher.rb"
    click C "https://github.com/sidekiq-cron/sidekiq-cron/blob/master/lib/sidekiq/cron/job.rb"
    click E1 "https://github.com/sidekiq-cron/sidekiq-cron/blob/master/lib/sidekiq/cron/namespace.rb"
    click E2 "https://github.com/sidekiq-cron/sidekiq-cron/tree/master/lib/sidekiq/cron/locales/"
    click F1 "https://github.com/sidekiq-cron/sidekiq-cron/blob/master/lib/sidekiq/cron/web.rb"
    click F2 "https://github.com/sidekiq-cron/sidekiq-cron/blob/master/lib/sidekiq/cron/web_extension.rb"
    click G "https://github.com/sidekiq-cron/sidekiq-cron/blob/master/lib/sidekiq-cron.rb"
    click J1 "https://github.com/sidekiq-cron/sidekiq-cron/tree/master/docker/"
    click J2 "https://github.com/sidekiq-cron/sidekiq-cron/blob/master/.github/workflows/ci.yml"

    %% Styles
    classDef internal fill:#BEE3F8,stroke:#2C5282,stroke-width:2px,color:#000;
    classDef external fill:#FBD38D,stroke:#C05621,stroke-width:2px,color:#000;
    classDef deployment fill:#C6F6D5,stroke:#2F855A,stroke-width:2px,color:#000;
```
