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.current, 'node-1')
registry.dispatched?('daily_report', Time.current) # => true

Instance Method Summary collapse

Methods inherited from Registry

#dispatched?

Constructor Details

#initializeMemoryEngine

Initialize a new in-memory registry.



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

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.



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

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:

  • the cron job key

  • when the job was scheduled to fire

Returns:

  • dispatch record or nil if not found



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

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:

  • the cron job key

  • when the job was scheduled to fire

  • identifier for the dispatching node

  • (defaults to: 'dispatched')

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

Returns:

  • the stored dispatch record



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

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.current,
      node_id: node_id,
      status: status
    }
  end
end

#sizeInteger

Get the number of stored dispatch records.

Returns:

  • number of dispatch records



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

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