Class: Sensu::Extension::Base
- Inherits:
-
Object
- Object
- Sensu::Extension::Base
- Defined in:
- lib/sensu/extension.rb
Instance Attribute Summary collapse
-
#logger ⇒ Array
Logger provided by Sensu.
-
#settings ⇒ Array
Settings hash provided by Sensu.
Class Method Summary collapse
-
.descendants ⇒ Array<object>
Determine classes that have inherited this class, used by the extension loader.
Instance Method Summary collapse
-
#[](key) ⇒ Object
Retrieve the definition object corresponding to a key, acting like a Hash object.
-
#definition ⇒ Object
Override this method to change the extension’s definition, a hash.
-
#description ⇒ Object
Override this method to set the extension’s description.
-
#has_key?(key) ⇒ TrueClass, FalseClass
Check to see if the definition has a key.
-
#initialize ⇒ Base
constructor
Initialize the extension, call post_init() when the eventmachine reactor starts up, stop() when it stops.
-
#name ⇒ Object
Override this method to set the extension’s name.
-
#post_init ⇒ Object
Override this method to do something immediately after the eventmachine reactor is started.
-
#run(data = nil, &callback) ⇒ Object
Override this method to do something when the extension is run, you must yield or call the callback with two parameters, an output string and exit code.
-
#safe_run(data = nil, &callback) ⇒ Object
Run the extension with a few safeties.
-
#stop ⇒ Object
Override this method to do something when the eventmachine reactor stops, such as connection or file cleanup.
Constructor Details
#initialize ⇒ Base
Initialize the extension, call post_init() when the eventmachine reactor starts up, stop() when it stops.
19 20 21 22 23 24 25 26 |
# File 'lib/sensu/extension.rb', line 19 def initialize EM.next_tick do post_init end EM.add_shutdown_hook do stop end end |
Instance Attribute Details
#logger ⇒ Array
Returns logger provided by Sensu.
11 12 13 |
# File 'lib/sensu/extension.rb', line 11 def logger @logger end |
#settings ⇒ Array
Returns settings hash provided by Sensu.
15 16 17 |
# File 'lib/sensu/extension.rb', line 15 def settings @settings end |
Class Method Details
.descendants ⇒ Array<object>
Determine classes that have inherited this class, used by the extension loader. Do not override this method!
109 110 111 112 113 |
# File 'lib/sensu/extension.rb', line 109 def self.descendants ObjectSpace.each_object(Class).select do |klass| klass < self end end |
Instance Method Details
#[](key) ⇒ Object
Retrieve the definition object corresponding to a key, acting like a Hash object. Do not override this method!
77 78 79 |
# File 'lib/sensu/extension.rb', line 77 def [](key) definition[key.to_sym] end |
#definition ⇒ Object
Override this method to change the extension’s definition, a hash. You probably don’t need to touch this. The hash must contain :type (“extension”) and :name.
41 42 43 44 45 46 |
# File 'lib/sensu/extension.rb', line 41 def definition { :type => "extension", :name => name } end |
#description ⇒ Object
Override this method to set the extension’s description.
34 35 36 |
# File 'lib/sensu/extension.rb', line 34 def description "extension description (change me)" end |
#has_key?(key) ⇒ TrueClass, FalseClass
Check to see if the definition has a key. Do not override this method!
86 87 88 |
# File 'lib/sensu/extension.rb', line 86 def has_key?(key) definition.has_key?(key.to_sym) end |
#name ⇒ Object
Override this method to set the extension’s name.
29 30 31 |
# File 'lib/sensu/extension.rb', line 29 def name "base" end |
#post_init ⇒ Object
Override this method to do something immediately after the eventmachine reactor is started. This method is great for setting up connections etc.
51 52 53 |
# File 'lib/sensu/extension.rb', line 51 def post_init true end |
#run(data = nil, &callback) ⇒ Object
Override this method to do something when the extension is run, you must yield or call the callback with two parameters, an output string and exit code.
62 63 64 |
# File 'lib/sensu/extension.rb', line 62 def run(data=nil, &callback) callback.call("noop", 0) end |
#safe_run(data = nil, &callback) ⇒ Object
Run the extension with a few safeties. This method wraps run() with a begin;rescue, and duplicates data before passing it to ensure the extension doesn’t mutate the original. Do not override this method!
97 98 99 100 101 102 103 |
# File 'lib/sensu/extension.rb', line 97 def safe_run(data=nil, &callback) begin data ? run(data.dup, &callback) : run(&callback) rescue => error callback.call(error.to_s, 2) end end |
#stop ⇒ Object
Override this method to do something when the eventmachine reactor stops, such as connection or file cleanup.
68 69 70 |
# File 'lib/sensu/extension.rb', line 68 def stop true end |