Class: Eye::Trigger

Inherits:
Object show all
Includes:
Dsl::Validation
Defined in:
lib/eye/trigger.rb

Direct Known Subclasses

Custom, Flapping, StopChilds, Transition

Defined Under Namespace

Classes: Custom, Flapping, StopChilds, Transition

Constant Summary collapse

TYPES =

ex: { :type => :flapping, :times => 2, :within => 30.seconds}

{:flapping => 'Flapping', :transition => 'Transition', :stop_childs => 'StopChilds'}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Dsl::Validation

included

Constructor Details

#initialize(process, options = {}) ⇒ Trigger

Returns a new instance of Trigger.



45
46
47
48
49
50
51
# File 'lib/eye/trigger.rb', line 45

def initialize(process, options = {})
  @options = options
  @process = process
  @full_name = @process.full_name if @process

  debug "add #{options}"
end

Instance Attribute Details

#messageObject (readonly)

Returns the value of attribute message.



12
13
14
# File 'lib/eye/trigger.rb', line 12

def message
  @message
end

#optionsObject (readonly)

Returns the value of attribute options.



12
13
14
# File 'lib/eye/trigger.rb', line 12

def options
  @options
end

#processObject (readonly)

Returns the value of attribute process.



12
13
14
# File 'lib/eye/trigger.rb', line 12

def process
  @process
end

Class Method Details

.create(process, options = {}) ⇒ Object



33
34
35
36
37
38
39
# File 'lib/eye/trigger.rb', line 33

def self.create(process, options = {})
  get_class(options[:type]).new(process, options)

rescue Exception, Timeout::Error => ex
  log_ex(ex)
  nil
end

.depends_onObject



107
108
# File 'lib/eye/trigger.rb', line 107

def self.depends_on
end

.get_class(type) ⇒ Object



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

def self.get_class(type)
  klass = eval("Eye::Trigger::#{TYPES[type]}") rescue nil
  raise "Unknown trigger #{type}" unless klass
  if deps = klass.depends_on
    Array(deps).each { |d| require d }
  end
  klass
end

.name_and_class(type) ⇒ Object



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

def self.name_and_class(type)
  type = type.to_sym
  return {:name => type, :type => type} if TYPES[type]

  if type =~ /\A(.*?)_?[0-9]+\z/
    ctype = $1.to_sym
    return {:name => type, :type => ctype} if TYPES[ctype]
  end
end

.register(base) ⇒ Object



100
101
102
103
104
105
# File 'lib/eye/trigger.rb', line 100

def self.register(base)
  name = base.to_s.gsub('Eye::Trigger::', '')
  type = name.underscore.to_sym
  Eye::Trigger::TYPES[type] = name
  Eye::Trigger.const_set(name, base)
end

.validate!(options = {}) ⇒ Object



41
42
43
# File 'lib/eye/trigger.rb', line 41

def self.validate!(options = {})
  get_class(options[:type]).validate(options)
end

Instance Method Details

#check(transition) ⇒ Object



88
89
90
# File 'lib/eye/trigger.rb', line 88

def check(transition)
  raise 'realize me'
end

#defer(&block) ⇒ Object



96
97
98
# File 'lib/eye/trigger.rb', line 96

def defer(&block)
  Celluloid::Future.new(&block).value
end

#filter_transition(trans) ⇒ Object



80
81
82
83
84
85
86
# File 'lib/eye/trigger.rb', line 80

def filter_transition(trans)
  return true unless to || from || event

  compare_state(trans.to_name, to) &&
    compare_state(trans.from_name, from) &&
    compare_state(trans.event, event)
end

#inspectObject



53
54
55
# File 'lib/eye/trigger.rb', line 53

def inspect
  "<#{self.class} @process='#{@full_name}' @options=#{@options}>"
end

#logger_sub_tagObject



61
62
63
# File 'lib/eye/trigger.rb', line 61

def logger_sub_tag
  "trigger(#{@options[:type]})"
end

#logger_tagObject



57
58
59
# File 'lib/eye/trigger.rb', line 57

def logger_tag
  @process.logger.prefix
end

#notify(transition, reason) ⇒ Object



65
66
67
68
69
70
71
72
73
74
# File 'lib/eye/trigger.rb', line 65

def notify(transition, reason)
  debug "check (:#{transition.event}) :#{transition.from} => :#{transition.to}"
  @reason = reason
  @transition = transition

  check(transition) if filter_transition(transition)

rescue Exception, Timeout::Error => ex
  log_ex(ex)
end

#run_in_process_context(p) ⇒ Object



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

def run_in_process_context(p)
  process.instance_exec(&p) if process.alive?
end