Class: HeartbeatRails::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/heartbeat_rails/configuration.rb

Constant Summary collapse

DEFAULT_MOUNTPOINT =

Default mountpoint for engine

'/heartbeat'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data = {}) ⇒ Configuration

Returns a new instance of Configuration.



13
14
15
16
17
18
19
20
# File 'lib/heartbeat_rails/configuration.rb', line 13

def initialize(data={})
  @data = {}
  @monitors = {}
  @mountpoint = DEFAULT_MOUNTPOINT
  @mount_mode = :append
  update!(data)
  load_default_monitors
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/heartbeat_rails/configuration.rb', line 36

def method_missing(sym, *args)
  if sym.to_s =~ /(.+)=$/
    self[$1] = args.first
  else
    self[sym]
  end
end

Instance Attribute Details

#monitorsObject

Returns the value of attribute monitors.



11
12
13
# File 'lib/heartbeat_rails/configuration.rb', line 11

def monitors
  @monitors
end

#mountpointObject (readonly)

used in railtie to read where to mount the engine



9
10
11
# File 'lib/heartbeat_rails/configuration.rb', line 9

def mountpoint
  @mountpoint
end

Instance Method Details

#[](key) ⇒ Object



28
29
30
# File 'lib/heartbeat_rails/configuration.rb', line 28

def [](key)
  @data[key.to_sym]
end

#[]=(key, value) ⇒ Object



32
33
34
# File 'lib/heartbeat_rails/configuration.rb', line 32

def []=(key, value)
  @data[key.to_sym] = value
end

#append_route!Object



61
62
63
# File 'lib/heartbeat_rails/configuration.rb', line 61

def append_route!
  @mount_mode = :append
end

#automount?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/heartbeat_rails/configuration.rb', line 53

def automount?
  !!@mount_mode
end

#dont_mount!Object



69
70
71
# File 'lib/heartbeat_rails/configuration.rb', line 69

def dont_mount!
  @mount_mode = nil
end

#load_default_monitorsObject



73
74
75
76
77
78
79
80
81
# File 'lib/heartbeat_rails/configuration.rb', line 73

def load_default_monitors
  if defined?(ActiveRecord::Base)
    monitor('database') do
      # Delegate activeness check to actual AR implementation
      # in MySQL it would call mysql.ping in postgres it fallback to SELECT 1
      ActiveRecord::Base.connection.active?
    end
  end
end

#monitor(label, &block) ⇒ Object



44
45
46
# File 'lib/heartbeat_rails/configuration.rb', line 44

def monitor(label, &block)
  @monitors[label.to_s] = block
end

#mount_at(mountpoint) ⇒ Object

Configure where the engine should be mounted



49
50
51
# File 'lib/heartbeat_rails/configuration.rb', line 49

def mount_at(mountpoint)
  @mountpoint = mountpoint
end

#prepend_route!Object



65
66
67
# File 'lib/heartbeat_rails/configuration.rb', line 65

def prepend_route!
  @mount_mode = :prepend
end

#route_methodObject



57
58
59
# File 'lib/heartbeat_rails/configuration.rb', line 57

def route_method
  @mount_mode
end

#status_checkObject



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/heartbeat_rails/configuration.rb', line 83

def status_check
  responses = {}
  begin
    @monitors.each do |label, mon|
      time = ::Benchmark.realtime do
        responses["#{label}_response"] = (mon.call).to_s
      end
      responses["#{label}_time"] = "#{time.inspect} seconds"
    end
    return :ok, responses
  rescue => e
    responses['error_message'] = e.message
    return :error, responses
  end
end

#update!(data) ⇒ Object



22
23
24
25
26
# File 'lib/heartbeat_rails/configuration.rb', line 22

def update!(data)
  data.each do |key, value|
    self[key] = value
  end
end