Class: Kaal::Dispatch::MemoryEngine

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

Overview

In-memory dispatch registry using a Hash for storage.

Stores dispatch records in memory. Suitable for development, testing, or single-node deployments where persistence is not required.

Examples:

Usage

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

Instance Method Summary collapse

Methods inherited from Registry

#dispatched?

Constructor Details

#initializeMemoryEngine

Initialize a new in-memory registry.



24
25
26
27
28
# File 'lib/kaal/dispatch/memory_engine.rb', line 24

def initialize
  super
  @dispatches = {}
  @mutex = Mutex.new
end

Instance Method Details

#clearvoid

This method returns an undefined value.

Clear all stored dispatch records. Useful for testing.



69
70
71
72
73
# File 'lib/kaal/dispatch/memory_engine.rb', line 69

def clear
  @mutex.synchronize do
    @dispatches.clear
  end
end

#find_dispatch(key, fire_time) ⇒ Hash?

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:

  • (Hash, nil)

    dispatch record or nil if not found



57
58
59
60
61
62
# File 'lib/kaal/dispatch/memory_engine.rb', line 57

def find_dispatch(key, fire_time)
  @mutex.synchronize do
    storage_key = build_key(key, fire_time)
    @dispatches[storage_key]
  end
end

#log_dispatch(key, fire_time, node_id, status = 'dispatched') ⇒ Hash

Log a dispatch attempt in memory.

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:

  • (Hash)

    the stored dispatch record



38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/kaal/dispatch/memory_engine.rb', line 38

def log_dispatch(key, fire_time, node_id, status = 'dispatched')
  @mutex.synchronize do
    storage_key = build_key(key, fire_time)
    @dispatches[storage_key] = {
      key: key,
      fire_time: fire_time,
      dispatched_at: Time.now.utc,
      node_id: node_id,
      status: status
    }
  end
end

#sizeInteger

Get the number of stored dispatch records.

Returns:

  • (Integer)

    number of dispatch records



79
80
81
82
83
# File 'lib/kaal/dispatch/memory_engine.rb', line 79

def size
  @mutex.synchronize do
    @dispatches.size
  end
end