Module: WatcherAction

Defined in:
lib/watcher_action.rb,
lib/watcher_action/send_mail.rb,
lib/watcher_action/log_action.rb,
lib/watcher_action/meta_action.rb,
lib/watcher_action/kill_process.rb

Overview

Each action object contains the action that will be taken when a watched condition is triggered.

Each action must respond to the #execute(event) method, which will execute the action.

Actions should not log the executing to the log file, unless there is an unexpected error in the execution of the action itself. If the user wants to log to the log file, the logger action should be used. (All actions may use the log for debug-level information in all places.)

Defined Under Namespace

Classes: KillProcess, LogAction, MetaAction, SendMail

Class Method Summary collapse

Class Method Details

.has_action?(name) ⇒ Boolean

Checks if the given action exists

Returns:

  • (Boolean)


34
35
36
# File 'lib/watcher_action.rb', line 34

def has_action?(name)
  registered_actions[name.to_sym] != nil
end

.is_action?(name, action) ⇒ Boolean

Checks if the given action is registered with that name

Returns:

  • (Boolean)


39
40
41
# File 'lib/watcher_action.rb', line 39

def is_action?(name, action)
  registered_actions[name.to_sym] == action
end

.register(name, config_options) ⇒ Object

creates a new action of the given type. The Action object itself will not be made public, instead call the #run_action method to execute it

Raises:

  • (ArgumentError)


15
16
17
18
19
20
21
22
# File 'lib/watcher_action.rb', line 15

def register(name, config_options)
  assit_kind_of(String, name)
  raise(ArgumentError, "Illegal options.") unless(config_options.is_a?(Hash))
  type = config_options.get_value(:type, false)
  type = WatchDogger.camelize(type)
  registered_actions[name.to_sym] = WatcherAction.const_get(type).new(config_options)
  dog_log.debug('Action Handler') { "Registered action '#{name}' of type #{type}"}
end

.run_action(name, event) ⇒ Object

Runs the named action. Returns true if the action ran without exception.



25
26
27
28
29
30
31
# File 'lib/watcher_action.rb', line 25

def run_action(name, event)
  registered_actions[name.to_sym].execute(event)
  true
rescue Exception => e
  dog_log.error('Action Handler') { "Could not execute #{name}: #{e.message} (Registered actions: #{registered_actions.keys.join(', ')})" }
  false
end