Class: Eye::Trigger

Inherits:
Object
  • Object
show all
Extended by:
Dsl::Validation
Defined in:
lib/eye/trigger.rb

Direct Known Subclasses

Custom, Flapping, State

Defined Under Namespace

Classes: Custom, Flapping, State

Constant Summary collapse

TYPES =

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

{:flapping => "Flapping", :state => "State"}

Instance Attribute Summary collapse

Attributes included from Dsl::Validation

#defaults, #should_bes, #validates, #variants

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Dsl::Validation

inherited, param, validate

Constructor Details

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

Returns a new instance of Trigger.



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

def initialize(process, options = {})
  @options = options
  @process = process

  debug "add #{options}"
end

Instance Attribute Details

#messageObject (readonly)

Returns the value of attribute message.



9
10
11
# File 'lib/eye/trigger.rb', line 9

def message
  @message
end

#optionsObject (readonly)

Returns the value of attribute options.



9
10
11
# File 'lib/eye/trigger.rb', line 9

def options
  @options
end

#processObject (readonly)

Returns the value of attribute process.



9
10
11
# File 'lib/eye/trigger.rb', line 9

def process
  @process
end

Class Method Details

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



29
30
31
# File 'lib/eye/trigger.rb', line 29

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

.get_class(type) ⇒ Object



23
24
25
26
27
# File 'lib/eye/trigger.rb', line 23

def self.get_class(type)
  klass = eval("Eye::Trigger::#{TYPES[type]}") rescue nil
  raise "Unknown trigger #{type}" unless klass
  klass
end

.name_and_class(type) ⇒ Object



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

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



76
77
78
79
80
81
82
# File 'lib/eye/trigger.rb', line 76

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

.validate!(options = {}) ⇒ Object



33
34
35
# File 'lib/eye/trigger.rb', line 33

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

Instance Method Details

#check(transition) ⇒ Object



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

def check(transition)
  raise "realize me"
end

#defer(&block) ⇒ Object



72
73
74
# File 'lib/eye/trigger.rb', line 72

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

#inspectObject



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

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

#logger_sub_tagObject



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

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

#logger_tagObject



48
49
50
# File 'lib/eye/trigger.rb', line 48

def logger_tag
  @process.logger.prefix
end

#notify(transition) ⇒ Object



56
57
58
59
60
61
62
# File 'lib/eye/trigger.rb', line 56

def notify(transition)
  debug "check"
  @transition = transition
  check(transition)
rescue => ex
  warn "failed #{ex.message} #{ex.backtrace}"
end

#run_in_process_context(p) ⇒ Object



68
69
70
# File 'lib/eye/trigger.rb', line 68

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