Class: Eye::Trigger

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

Defined Under Namespace

Classes: CheckDependency, Custom, Flapping, StartingGuard, StopChildren, Transition, WaitDependency

Constant Summary collapse

TYPES =
{:flapping => 'Flapping', :transition => 'Transition', :stop_children => 'StopChildren',
  :wait_dependency => 'WaitDependency', :check_dependency => 'CheckDependency', :starting_guard => 'StartingGuard'
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Dsl::Validation

included

Constructor Details

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


48
49
50
51
52
53
54
# File 'lib/eye/trigger.rb', line 48

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


15
16
17
# File 'lib/eye/trigger.rb', line 15

def message
  @message
end

#optionsObject (readonly)

Returns the value of attribute options


15
16
17
# File 'lib/eye/trigger.rb', line 15

def options
  @options
end

#processObject (readonly)

Returns the value of attribute process


15
16
17
# File 'lib/eye/trigger.rb', line 15

def process
  @process
end

Class Method Details

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


36
37
38
39
40
41
42
# File 'lib/eye/trigger.rb', line 36

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

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

.get_class(type) ⇒ Object


27
28
29
30
31
32
33
34
# File 'lib/eye/trigger.rb', line 27

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

.name_and_class(type) ⇒ Object


17
18
19
20
21
22
23
24
25
# File 'lib/eye/trigger.rb', line 17

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


118
119
120
121
122
123
# File 'lib/eye/trigger.rb', line 118

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

.requiresObject


125
126
# File 'lib/eye/trigger.rb', line 125

def self.requires
end

.validate!(options = {}) ⇒ Object


44
45
46
# File 'lib/eye/trigger.rb', line 44

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

Instance Method Details

#check(transition) ⇒ Object

Raises:

  • (NotImplementedError)

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

def check(transition)
  raise NotImplementedError
end

#defer(&block) ⇒ Object


114
115
116
# File 'lib/eye/trigger.rb', line 114

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

#exec_proc(name = :do) ⇒ Object


103
104
105
106
107
108
109
110
111
112
# File 'lib/eye/trigger.rb', line 103

def exec_proc(name = :do)
  act = @options[name]
  if act
    res = instance_exec(&@options[name]) if act.is_a?(Proc)
    res = send(act, process) if act.is_a?(Symbol)
    res
  else
    true
  end
end

#filter_transition(trans) ⇒ Object


87
88
89
90
91
92
93
# File 'lib/eye/trigger.rb', line 87

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


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

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

#logger_sub_tagObject


64
65
66
# File 'lib/eye/trigger.rb', line 64

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

#logger_tagObject


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

def logger_tag
  @process.logger.prefix
end

#notify(transition, reason) ⇒ Object


68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/eye/trigger.rb', line 68

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
  if ex.class == Eye::Process::StateError
    raise ex
  else
    log_ex(ex)
  end
end

#run_in_process_context(p) ⇒ Object


99
100
101
# File 'lib/eye/trigger.rb', line 99

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