Module: StatusLib

Defined in:
lib/status_lib.rb,
lib/status_lib/version.rb

Defined Under Namespace

Classes: ApiDownError, Cache, Config, NullStatusApi, ServiceDownError, StatusApi, StatusInfo

Constant Summary collapse

VERSION =
"0.0.2"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.serverObject (readonly)

Returns the value of attribute server.



23
24
25
# File 'lib/status_lib.rb', line 23

def server
  @server
end

Class Method Details

.configObject

Read configuration settings

The values that are set to #configure are available here.



92
93
94
# File 'lib/status_lib.rb', line 92

def config
  @config ||= Config.new
end

.configure(settings) ⇒ Object

Configure the gem

Params:

+settings+:: the configuration settings.  A hash that may contain:
  +:server+:: the URL of the status page service
  +:cache_time+:: time to cache status reponses from the API
  +:api_call_timeout+:: maximum time to wait for a call to status API
  +:down_duration+:: default time to switch a service down for
  +:block_timeout+:: default time that a `with_circuit_breaker` can take


84
85
86
# File 'lib/status_lib.rb', line 84

def configure(settings)
  config.update(settings)
end

.switch(name, status, options = {}) ⇒ Object

Sets the status of a service to up or down

Params:

+name+:: the name of the service to update
+status+:: the desired status; either :up or :down
+options+:: additional options.  can contain:
  +:for+:: the time to keep the service down, in seconds


42
43
44
# File 'lib/status_lib.rb', line 42

def switch(name, status, options={})
  status_info.switch(name, status, options)
end

.up?(name) ⇒ Boolean

Checks whether a particular service is up or down.

Params:

+name+:: the name of the service to check

Returns:

  • (Boolean)


30
31
32
# File 'lib/status_lib.rb', line 30

def up?(name)
  status_info.up?(name)
end

.with_circuit_breaker(name, options = {}) ⇒ Object

Helper method for guarding a block of code with a curcuit breaker.

If the code in the block raises an exception or exceeds its timeout, then the associated service status will be set to down for the specified interval, and a StatusLib::ServiceDownError exception will be raised.

If the service is already makred down then the block won’t be evaluated at all, and StatusLib::ServiceDownError will be raised immediately.

+name+:: the name of the service to update
+options+:: additional options.  can contain:
  +:timeout+:: upper limit on how long the block can take to run
  +:down_for+:: the time to keep the service down, in seconds


60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/status_lib.rb', line 60

def with_circuit_breaker(name, options={})
  raise ServiceDownError unless up?(name)

  Timeout::timeout( determine_timeout(options) ) do
    yield
  end
rescue Timeout::Error => e
  handle_exception(e, name, options)
  raise e
rescue => e
  handle_exception(e, name, options)
  raise ServiceDownError.new(e)
end