Class: Eye::Trigger

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

Defined Under Namespace

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

Constant Summary collapse

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

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.



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

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.



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

def message
  @message
end

#optionsObject (readonly)

Returns the value of attribute options.



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

def options
  @options
end

#processObject (readonly)

Returns the value of attribute process.



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

def process
  @process
end

Class Method Details

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



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

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



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

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



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

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



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

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



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

def self.requires
end

.validate!(options = {}) ⇒ Object



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

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

Instance Method Details

#check(transition) ⇒ Object

Raises:

  • (NotImplementedError)


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

def check(transition)
  raise NotImplementedError
end

#defer(&block) ⇒ Object



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

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

#filter_transition(trans) ⇒ Object



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

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



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

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

#logger_sub_tagObject



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

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

#logger_tagObject



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

def logger_tag
  @process.logger.prefix
end

#notify(transition, reason) ⇒ Object



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

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



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

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