Class: ScoutAgent::Order

Inherits:
Object
  • Object
show all
Defined in:
lib/scout_agent/order.rb,
lib/scout_agent/order/check_in_order.rb,
lib/scout_agent/order/snapshot_order.rb

Overview

This object represents an order that can be understood over XMPP.

Each subclass can can set a MATCH_RE constant with a regular expression for orders it can handle or override the match?() class method, analyze the orders with code, and return true or false if it can handle it.

The first subclass that matches will have it’s execute() method called to process the order.

Direct Known Subclasses

CheckInOrder, SnapshotOrder

Defined Under Namespace

Classes: CheckInOrder, SnapshotOrder

Constant Summary collapse

ORDERS_DIR =

The directory Order subclasses are loaded from.

LIB_DIR + "order"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(message, match_details) ⇒ Order

Creates an instance of this Order type for execution. The Order is passed the matched message and the match_details.



88
89
90
91
# File 'lib/scout_agent/order.rb', line 88

def initialize(message, match_details)
  @message       = message
  @match_details = match_details
end

Instance Attribute Details

#match_detailsObject (readonly)

Any details returned from the match. This will be an Array of Regexp captures by default.



99
100
101
# File 'lib/scout_agent/order.rb', line 99

def match_details
  @match_details
end

#messageObject (readonly)

The message matched from XMPP.



94
95
96
# File 'lib/scout_agent/order.rb', line 94

def message
  @message
end

Class Method Details

.can_handle?(message, modified_body = nil) ⇒ Boolean

Finds the first subclass that can handle message, optionally with the modified_body. This method will return nil if a match cannot be made.

Returns:

  • (Boolean)


54
55
56
57
58
59
60
61
# File 'lib/scout_agent/order.rb', line 54

def self.can_handle?(message, modified_body = nil)
  subclasses.each { |order|
    if match_details = order.match?(message, modified_body)
      return order.new(message, match_details)
    end
  }
  nil
end

.inherited(order) ⇒ Object

Hooks into Ruby’s Class loading process so we can notice subclasses as they are loaded.



38
39
40
# File 'lib/scout_agent/order.rb', line 38

def self.inherited(order)
  subclasses << order
end

.load_allObject

Load all subclasses found in ORDERS_DIR and return them.



43
44
45
46
47
48
# File 'lib/scout_agent/order.rb', line 43

def self.load_all
  ORDERS_DIR.each_entry do |order|
    require ORDERS_DIR + order unless order.basename.to_s[0] == ?.
  end
  subclasses
end

.logObject

Returns whatever log has been set by or log=() or a bit bucket log.



25
26
27
# File 'lib/scout_agent/order.rb', line 25

def self.log
  @log ||= WireTap.new(nil)
end

.log=(log) ⇒ Object

Set the log() used by all Order objects.



20
21
22
# File 'lib/scout_agent/order.rb', line 20

def self.log=(log)
  @log = log
end

.master_agentObject

Returns an IDCard for the master agent that Order objects can use to signal it.



80
81
82
# File 'lib/scout_agent/order.rb', line 80

def self.master_agent
  @master_agent ||= IDCard.new(:master)
end

.match?(message, modified_body = nil) ⇒ Boolean

Returns true if this type of object matches message, optionally with the modified_body.

The default implementation looks for a MATCH_RE constant and checks it against modified_body or message.body. Subclasses are free to override this for more complex behavior though.

Returns:

  • (Boolean)


71
72
73
74
# File 'lib/scout_agent/order.rb', line 71

def self.match?(message, modified_body = nil)
  const_defined?(:MATCH_RE) and
  const_get(:MATCH_RE).match(modified_body || message.body)
end

.subclassesObject

Returns an Array of all loaded subclasses.



30
31
32
# File 'lib/scout_agent/order.rb', line 30

def self.subclasses
  @subclasses ||= Array.new
end

Instance Method Details

#logObject

A shortcut for the class method of the same name.



102
103
104
# File 'lib/scout_agent/order.rb', line 102

def log
  self.class.log
end

#notify_masterObject

This helper can be used to send an ALRM signal to the master agent which triggers it to wake up and go to work. This is handy for making it notice changes faster.



111
112
113
114
115
116
# File 'lib/scout_agent/order.rb', line 111

def notify_master
  self.class.master_agent.signal("ALRM")
rescue Exception  # unable to signal process
  # do nothing:  process will catch up when it restarts
  log.warn("Unable to signal master process.")
end