Class: Kaal::Dispatch::DatabaseEngine

Inherits:
Registry
  • Object
show all
Defined in:
lib/kaal/dispatch/database_engine.rb

Overview

Database-backed dispatch registry using ActiveRecord.

Stores dispatch records in the database using the CronDispatch model. Provides persistent, queryable audit logs across all nodes.

Examples:

Usage

registry = Kaal::Dispatch::DatabaseEngine.new
registry.log_dispatch('daily_report', Time.current, 'node-1')
registry.dispatched?('daily_report', Time.current) # => true

Instance Method Summary collapse

Methods inherited from Registry

#dispatched?

Instance Method Details

#cleanup(recovery_window: 86_400) ⇒ Integer

Delete old dispatch records older than the specified time.

This cleanup prevents unbounded database growth by removing records that are older than the recovery window, making them irrelevant for future recovery operations.

Parameters:

  • recovery_window (Integer) (defaults to: 86_400)

    seconds to keep records for (e.g., 86400 for 24h)

Returns:

  • (Integer)

    number of records deleted



88
89
90
91
# File 'lib/kaal/dispatch/database_engine.rb', line 88

def cleanup(recovery_window: 86_400)
  cutoff_time = Time.current - recovery_window
  ::Kaal::CronDispatch.where('fire_time < ?', cutoff_time).delete_all
end

#find_by_key(key) ⇒ ActiveRecord::Relation

Find all dispatch records for a specific job key.

Parameters:

  • key (String)

    the cron job key

Returns:

  • (ActiveRecord::Relation)

    collection of dispatch records



57
58
59
# File 'lib/kaal/dispatch/database_engine.rb', line 57

def find_by_key(key)
  ::Kaal::CronDispatch.where(key: key).order(fire_time: :desc)
end

#find_by_node(node_id) ⇒ ActiveRecord::Relation

Find all dispatch records by node ID.

Parameters:

  • node_id (String)

    the node identifier

Returns:

  • (ActiveRecord::Relation)

    collection of dispatch records



66
67
68
# File 'lib/kaal/dispatch/database_engine.rb', line 66

def find_by_node(node_id)
  ::Kaal::CronDispatch.where(node_id: node_id).order(fire_time: :desc)
end

#find_by_status(status) ⇒ ActiveRecord::Relation

Find all dispatch records with a specific status.

Parameters:

  • status (String)

    the dispatch status

Returns:

  • (ActiveRecord::Relation)

    collection of dispatch records



75
76
77
# File 'lib/kaal/dispatch/database_engine.rb', line 75

def find_by_status(status)
  ::Kaal::CronDispatch.where(status: status).order(fire_time: :desc)
end

#find_dispatch(key, fire_time) ⇒ Kaal::CronDispatch?

Find a dispatch record for a specific job and fire time.

Parameters:

  • key (String)

    the cron job key

  • fire_time (Time)

    when the job was scheduled to fire

Returns:



48
49
50
# File 'lib/kaal/dispatch/database_engine.rb', line 48

def find_dispatch(key, fire_time)
  ::Kaal::CronDispatch.find_by(key: key, fire_time: fire_time)
end

#log_dispatch(key, fire_time, node_id, status = 'dispatched') ⇒ Kaal::CronDispatch

Log a dispatch attempt in the database.

Parameters:

  • key (String)

    the cron job key

  • fire_time (Time)

    when the job was scheduled to fire

  • node_id (String)

    identifier for the dispatching node

  • status (String) (defaults to: 'dispatched')

    dispatch status (‘dispatched’, ‘failed’, etc.)

Returns:

Raises:

  • (ActiveRecord::RecordInvalid)

    if the record is invalid



32
33
34
35
36
37
38
39
40
# File 'lib/kaal/dispatch/database_engine.rb', line 32

def log_dispatch(key, fire_time, node_id, status = 'dispatched')
  ::Kaal::CronDispatch.create!(
    key: key,
    fire_time: fire_time,
    dispatched_at: Time.current,
    node_id: node_id,
    status: status
  )
end