Class: Sensu::API::Process

Inherits:
Object
  • Object
show all
Includes:
Daemon
Defined in:
lib/sensu/api/process.rb

Constant Summary

Constants included from Utilities

Utilities::EVAL_PREFIX

Instance Attribute Summary

Attributes included from Daemon

#settings, #start_time

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Daemon

#initialize, #load_extensions, #load_settings, #log_notices, #pause, #print_settings!, #resume, #setup_logger, #setup_process, #setup_redis, #setup_signal_traps, #setup_spawn, #setup_transport, #validate_settings!

Methods included from Utilities

#attributes_match?, #check_subdued?, #deep_dup, #deep_merge, #determine_check_cron_time, #eval_attribute_value, #find_attribute_value, #in_time_window?, #in_time_windows?, #object_substitute_tokens, #process_cpu_times, #process_eval_string, #random_uuid, #redact_sensitive, #retry_until_true, #substitute_tokens, #system_address, #system_hostname, #testing?

Class Method Details

.run(options = {}) ⇒ Object

Create an instance of the Sensu API process, setup the Redis and Transport connections, start the API HTTP server, set up API process signal traps (for stopping), within the EventMachine event loop.

Parameters:

  • options (Hash) (defaults to: {})


15
16
17
18
19
20
21
22
23
# File 'lib/sensu/api/process.rb', line 15

def self.run(options={})
  api = self.new(options)
  EM::run do
    api.setup_redis
    api.setup_transport
    api.start
    api.setup_signal_traps
  end
end

.test(options = {}) ⇒ Object

Create an instance of the Sensu API with initialized connections for running test specs.

Parameters:

  • options (Hash) (defaults to: {})


68
69
70
71
72
73
74
75
76
# File 'lib/sensu/api/process.rb', line 68

def self.test(options={})
  api = self.new(options)
  api.setup_redis do
    api.setup_transport do
      api.start
      yield
    end
  end
end

Instance Method Details

#startObject

Start the Sensu API HTTP server. This method sets the service state to ‘:running`.



45
46
47
48
49
50
51
# File 'lib/sensu/api/process.rb', line 45

def start
  api = @settings[:api] || {}
  bind = api[:bind] || "0.0.0.0"
  port = api[:port] || 4567
  start_http_server(bind, port)
  super
end

#start_http_server(bind, port) ⇒ Object

Start the API HTTP server. This method sets ‘@http_server`.

Parameters:

  • bind (String)

    address to listen on.

  • port (Integer)

    to listen on.



29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/sensu/api/process.rb', line 29

def start_http_server(bind, port)
  @logger.info("api listening", {
    :protocol => "http",
    :bind => bind,
    :port => port
  })
  @http_server = EM::start_server(bind, port, HTTPHandler) do |handler|
    handler.logger = @logger
    handler.settings = @settings
    handler.redis = @redis
    handler.transport = @transport
  end
end

#stopObject

Stop the Sensu API process. This method stops the HTTP server, closes the Redis and transport connections, sets the service state to ‘:stopped`, and stops the EventMachine event loop.



56
57
58
59
60
61
62
# File 'lib/sensu/api/process.rb', line 56

def stop
  @logger.warn("stopping")
  EM::stop_server(@http_server)
  @redis.close if @redis
  @transport.close if @transport
  super
end