Skip to content

Coordinator Loop (Safe Scheduler) #3

@niteshpurohit

Description

@niteshpurohit

Feature Request

Description

Implement the coordinator loop that calculates due fire times and dispatches work safely across multiple nodes.

Use Case

Guarantees each scheduled tick enqueues once even when multiple Ruby processes are running.

Proposed Solution

  • Add RailsCrons.start! to spawn a loop thread using tick_interval.
  • Implement RailsCrons.stop! and graceful shutdown on TERM/INT.
  • On each tick:
    • For each registered cron, compute due fire times within [now - window_lookback, now + window_lookahead].
    • For each due time, try to obtain a distributed lease (see Lock Adapters). If acquired, dispatch.
  • Expose RailsCrons.tick! for manual single-tick execution (handy for tests/Rake tasks).

Alternatives Considered

Outsourcing to an external scheduler — rejected to keep the library self-contained yet agnostic to job systems.

Additional Context

The loop must be re-entrant and safe to call in tests; avoid global state that isn't resettable.

Checklist

  • I have searched for a similar feature request and didn't find any duplicates.
  • I have included all the required information.
  • I have tagged the issue with the label "enhancement".

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    Projects

    Status

    Ready

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions