Module: Emissary

Defined in:
lib/emissary/agent.rb,
lib/emissary.rb,
lib/emissary/daemon.rb,
lib/emissary/errors.rb,
lib/emissary/logger.rb,
lib/emissary/server.rb,
lib/emissary/message.rb,
lib/emissary/identity.rb,
lib/emissary/operator.rb,
lib/emissary/agent/gem.rb,
lib/emissary/agent/file.rb,
lib/emissary/agent/ping.rb,
lib/emissary/agent/test.rb,
lib/emissary/gem_helper.rb,
lib/emissary/agent/error.rb,
lib/emissary/agent/mysql.rb,
lib/emissary/agent/proxy.rb,
lib/emissary/agent/stats.rb,
lib/emissary/identity/ec2.rb,
lib/emissary/identity/unix.rb,
lib/emissary/operator/amqp.rb,
lib/emissary/agent/emissary.rb,
lib/emissary/agent/rabbitmq.rb

Overview

Copyright 2010 The New York Times

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Defined Under Namespace

Modules: OperatorStatistics, ServerController Classes: Agent, Daemon, Error, GemHelper, Identity, Logger, Message, NetworkEncapsulatedError, Operator, Server, TrackingError

Constant Summary collapse

LIBPATH =

:stopdoc:

Pathname.new(__FILE__).dirname.realpath
PATH =
LIBPATH.dirname
VERSION =
::YAML.load(File.read(PATH + 'VERSION.yml')).values.join '.'
EXTERNALS_BASE =
File.join('emissary')
EXTERNAL_IDENTITIES =
File.join(EXTERNALS_BASE, 'identities').to_s
EXTERNAL_AGENTS =
File.join(EXTERNALS_BASE, 'agents').to_s
EXTERNAL_OPERATORS =
File.join(EXTERNALS_BASE, 'operator').to_s
DEFAULT_EXCHANGE =
:direct
DAEMON_RECHECK_INTERVAL =
10
IP_CHECK_DOMAIN =
'checkip.dyndns.org'
IP_CHECK_URL =
"http://#{IP_CHECK_DOMAIN}"
@@pid =
nil

Class Method Summary collapse

Class Method Details

.call(handler, config, *args) ⇒ Object

Sets up a line of communication



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/emissary.rb', line 156

def call handler, config, *args
  unless handler.is_a? Class or handler.is_a? Module
    klass_name = 'Emissary::Operator::' + handler.to_s.upcase
    begin
      require_klass klass_name
    rescue LoadError => e
      begin
        require_klass Emissary::EXTERNAL_OPERATORS + '::' + handler.to_s.upcase
      #rescue LoadError
        # raise the original exception if we still haven't found the file
      #  raise e
      end
    end
    handler = klass_const klass_name
  end
  
  klass = klass_from_handler(Operator, handler, config)
  
  operator = klass.new(config, *args)
  operator.validate_config!
  block_given? and yield operator
  operator
end

.dispatch(message, config, *args) ⇒ Object

Dispatches a message to the agent specified by the message



182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'lib/emissary.rb', line 182

def dispatch message, config, *args
  unless message.is_a? Emissary::Message
    raise ArgumentError, "message is not an Emissary::Message" 
  end
  
  begin
    agent_type = 'Emissary::Agent::' + message.agent.to_s.capitalize
    begin
      require_klass agent_type 
    rescue LoadError => e
      begin
        require_klass Emissary::EXTERNAL_AGENTS + '::' + message.agent.to_s.capitalize
      rescue LoadError
        # raise the original exception if we still haven't found the file
        raise e
      end
    end
  rescue Exception => e
    Emissary.logger.error "Dispatcher Error: #{e.class.name}: #{e.message}\n\t#{e.backtrace.join("\n\t")}"
    message = message.error(e)
    agent_type = 'Emissary::Agent::Error'
  end
  
  handler = klass_const agent_type, true
  
  klass = klass_from_handler(Agent, handler, message, config, *args)
  
  Emissary.logger.debug " [--] Dispatching message to: #{agent_type}"
  agent = klass.new(message, config, *args)
  block_given? and yield agent
  agent
end

.generate_uuidObject



146
147
148
# File 'lib/emissary.rb', line 146

def generate_uuid
  UUID.generate
end

.identityObject



150
151
152
# File 'lib/emissary.rb', line 150

def identity
  @@identity ||= Emissary::Identity.instance
end

.klass_const(class_name, autoload = false) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/emissary.rb', line 115

def klass_const(class_name, autoload = false)
  return class_name if class_name.is_a? Class or class_name.is_a? Module
  
  klass = Object
  
  class_name.split('::').each do |c|
    begin
      klass = klass.const_get(c.to_sym)
    rescue NameError => e 
      if autoload
        require_klass( (klass == Object ? c : "#{klass.to_s}::#{c}") )
        redo
      end
    end
  end
  
  klass.to_s == class_name ? klass : nil
end

.klass_from_handler(klass = nil, handler = nil, *args) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/emissary.rb', line 79

def klass_from_handler(klass = nil, handler = nil, *args)
  klass = if handler and handler.is_a? Class and not handler.nil?
    raise ArgumentError, "must provide module or subclass of #{klass.name}" unless klass >= handler
    handler
  elsif handler.is_a? Module
    resource_name = "RESOURCE_#{handler.to_s.upcase.split('::').pop}"
    begin
      klass.const_get(resource_name)
    rescue NameError
      klass.const_set(resource_name, Class.new(klass) { include handler } )
    end
  elsif klass.nil?
    raise ArgumentError, "klass must be a valid class constant for #{name}#klass_from_handler"
  elsif handler.nil?
    raise ArgumentError, "handler must be a valid class or module"
  else
    klass
  end
  
  arity = klass.instance_method(:initialize).arity
  expected = arity >= 0 ? arity : -(arity + 1)
  if (arity >= 0 and args.size != expected) or (arity < 0 and args.size < expected)
    raise ArgumentError, "wrong number of arguments for #{klass}#initialize (#{args.size} for #{expected})"
  end
  
  klass
end

.klass_loaded?(class_name) ⇒ Boolean

Returns:

  • (Boolean)


107
108
109
110
111
112
113
# File 'lib/emissary.rb', line 107

def klass_loaded?(class_name)
  begin
    !!klass_const(class_name)
  rescue NameError
    false
  end
end

.libpath(*args) ⇒ Object

Returns the library path for the module. If any arguments are given, they will be joined to the end of the libray path using File.join.



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

def libpath( *args )
  args.empty? ? LIBPATH.to_s : File.join(LIBPATH.to_s, args.flatten)
end

.load_klass(class_name) ⇒ Object



134
135
136
# File 'lib/emissary.rb', line 134

def load_klass(class_name)
  load libpath(*class_name.downcase.split(/::/)) + '.rb'
end

.loggerObject



142
143
144
# File 'lib/emissary.rb', line 142

def logger
  Emissary::Logger.instance
end

.path(*args) ⇒ Object

Returns the path for the module. If any arguments are given, they will be joined to the end of the path using File.join.



71
72
73
# File 'lib/emissary.rb', line 71

def path( *args )
  args.empty? ? PATH.to_s : File.join(PATH.to_s, args.flatten)
end

.PIDObject



49
50
51
# File 'lib/emissary.rb', line 49

def PID
  @@pid
end

.require_klass(class_name) ⇒ Object



138
139
140
# File 'lib/emissary.rb', line 138

def require_klass(class_name)
  require libpath(*class_name.downcase.split(/::/))
end

.sublib_path(*args) ⇒ Object



75
76
77
# File 'lib/emissary.rb', line 75

def sublib_path( *args )
  args.empty? ? PATH.to_s : File.join(LIBPATH.to_s, 'emissary', args.flatten)
end

.versionObject

Returns the version string for the library.



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

def version
  VERSION
end