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:

File.expand_path(File.dirname(File.expand_path(__FILE__))) + File::SEPARATOR
PATH =
File.dirname(LIBPATH) + File::SEPARATOR
VERSION =
::YAML.load(File.read(File.join(PATH, 'VERSION.yml'))).values.join '.'
EXTERNALS_BASE =
File.join(File::SEPARATOR, 'opt')
EXTERNAL_IDENTITIES =
File.join(EXTERNALS_BASE, 'emissary', 'identities')
EXTERNAL_AGENTS =
File.join(EXTERNALS_BASE, 'emissary', 'agents')
EXTERNAL_OPERATORS =
File.join(EXTERNALS_BASE, 'emissary', 'operators')
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



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

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



179
180
181
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
# File 'lib/emissary.rb', line 179

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



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

def generate_uuid
  UUID.generate
end

.identityObject



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

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

.klass_const(class_name, autoload = false) ⇒ Object



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

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



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

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)


104
105
106
107
108
109
110
# File 'lib/emissary.rb', line 104

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.



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

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

.load_klass(class_name) ⇒ Object



131
132
133
# File 'lib/emissary.rb', line 131

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

.loggerObject



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

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.



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

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

.PIDObject



46
47
48
# File 'lib/emissary.rb', line 46

def PID
  @@pid
end

.require_klass(class_name) ⇒ Object



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

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

.sublib_path(*args) ⇒ Object



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

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

.versionObject

Returns the version string for the library.



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

def version
  VERSION
end