Class: Opee::Env

Inherits:
Object
  • Object
show all
Defined in:
lib/opee/env.rb

Overview

The Env class hold all the global data for Opee. The global data could have been put directly under the Opee module but it seemed cleaner to keep it all together in an separate class.

Constant Summary

@@actors =

Array of active Actors. This is private and should not be modified directly.

[]
@@log =

global logger. This is private and should not be modified directly.

nil
@@finish_thread =

used to wakup calling thread when ready. This is private and should not be modified directly.

nil
@@rescuer =

Actor that responds to rescue if set. This is private and should not be modified directly. Use the rescuer() or rescuer=() methods instead.

nil

Class Method Summary (collapse)

Class Method Details

+ (Object) actor_count

Returns the number of active Actors.



57
58
59
# File 'lib/opee/env.rb', line 57

def self.actor_count()
  @@actors.size
end

+ (Object) add_actor(actor)

Adds an Actor to the Env. This is called by the Actor#initialize() method.

Parameters:

  • actor (Actor)

    actor to add



29
30
31
# File 'lib/opee/env.rb', line 29

def self.add_actor(actor)
  @@actors << actor
end

+ (true|false) busy?

Returns true of one or more Actors is either processing a request or has a request waiting to be processed on it's input queue.

Returns:

  • (true|false)

    the busy state across all Actors



194
195
196
197
# File 'lib/opee/env.rb', line 194

def self.busy?
  @@actors.each { |a| return true if a.busy? }
  false
end

+ (Object) debug(message)

Asks the logger to log a message if the current severity level is less than or equal to Logger::DEBUG.

Parameters:

  • message (String)

    string to log



91
92
93
# File 'lib/opee/env.rb', line 91

def self.debug(message)
  log(Logger::Severity::DEBUG, message)
end

+ (Object) each_actor(&blk)

Iterates over each active Actor and yields to the provided block with each Actor.

Parameters:

  • blk (Proc)

    Proc to call on each iteration



42
43
44
# File 'lib/opee/env.rb', line 42

def self.each_actor(&blk)
  @@actors.each { |a| blk.yield(a) }
end

+ (Object) error(message)

Asks the logger to log a message if the current severity level is less than or equal to Logger::ERROR.

Parameters:

  • message (String)

    string to log



112
113
114
# File 'lib/opee/env.rb', line 112

def self.error(message)
  log(Logger::Severity::ERROR, message)
end

+ (Object) fatal(message)

Asks the logger to log a message if the current severity level is less than or equal to Logger::FATAL.

Parameters:

  • message (String)

    string to log



119
120
121
# File 'lib/opee/env.rb', line 119

def self.fatal(message)
  log(Logger::Severity::FATAL, message)
end

+ (Actor|NilClass) find_actor(name)

Locates and return an Actor with the specified name. If there is more than one Actor with the name specified then the first one encountered is returned.

Parameters:

  • name (String)

    name of the Actor

Returns:

  • (Actor|NilClass)

    the Actor with the name specified or nil



51
52
53
54
# File 'lib/opee/env.rb', line 51

def self.find_actor(name)
  @@actors.each { |a| return a if name == a.name }
  nil
end

+ (Object) info(message)

Asks the logger to log a message if the current severity level is less than or equal to Logger::INFO.

Parameters:

  • message (String)

    string to log



98
99
100
# File 'lib/opee/env.rb', line 98

def self.info(message)
  log(Logger::Severity::INFO, message)
end

+ (Object) log(severity, message)

Asks the logger to log the message if the severity is high enough.

Parameters:

  • severity (Fixnum)

    one of the Logger levels

  • message (String)

    string to log



77
78
79
80
81
82
83
84
85
86
# File 'lib/opee/env.rb', line 77

def self.log(severity, message)
  @@log = Log.new() if @@log.nil?
  t = Thread.current
  if (name = t[:name]).nil?
    tid = "%d/0x%014x" % [Process.pid, Thread.current.object_id * 2]
  else
    tid = "#{Process.pid}/#{name}"
  end
  @@log.ask(:log, severity, message, tid)
end

+ (Object) log_rescue(ex)

Asks the logger to log a Exception class and message if the current severity level is less than or equal to Logger::ERROR. If the current severity is less than or equal to Logger::WARN then the Exception bactrace is also logged.

Parameters:

  • ex (Exception)

    Exception to log



141
142
143
144
145
146
147
148
149
# File 'lib/opee/env.rb', line 141

def self.log_rescue(ex)
  @@log = Log.new() if @@log.nil?
  return unless Logger::Severity::ERROR >= @@log.level
  msg = "#{ex.class}: #{ex.message}"
  if Logger::Severity::WARN >= @@log.level
    ex.backtrace.each { |line| msg << "    #{line}\n" }
  end
  log(Logger::Severity::ERROR, msg)
end

+ (Object) log_status



255
256
257
258
259
# File 'lib/opee/env.rb', line 255

def self.log_status()
  s = "\n  %20s  %5s  %5s\n" % ['Actor Name', 'Q-cnt', 'busy?']
  @@actors.each { |a| s << "  %20s  %5d  %5s\n" % [a.name, a.queue_count(), a.busy?()] }
  info(s)
end

+ (Log) logger

Returns the current Log Actor. If the current logger is nil then a new Log Actor is created and returned.

Returns:

  • (Log)

    the current logger



154
155
156
157
# File 'lib/opee/env.rb', line 154

def self.logger()
  @@log = Log.new() if @@log.nil?
  @@log
end

+ (Object) logger=(log_actor)

Sets the logger to the provided Log Actor. If the current logger is not nil then the current logger is closed first.

Parameters:

  • log_actor (Log)

    Log Actor to use as the logger

Raises:

  • (TypeError)

    raised if the log_actor is not a Log Actor



163
164
165
166
167
168
# File 'lib/opee/env.rb', line 163

def self.logger=(log_actor)
  raise TypeError.new("can't convert #{log_actor.class} into a Opee::Log") unless log_actor.is_a?(Log)
  @@log.close() unless @@log.nil?
  @@log = log_actor
  @@log
end

+ (Fixnum) queue_count

Returns the sum of all the requests in all the Actor's queues.

Returns:

  • (Fixnum)

    total number of items waiting to be processed



185
186
187
188
189
# File 'lib/opee/env.rb', line 185

def self.queue_count()
  cnt = 0
  @@actors.each { |a| cnt += a.queue_count() }
  cnt
end

+ (Object) remove_actor(actor)

Removes an Actor from the Env. This is called by the Actor#close() method.

Parameters:

  • actor (Actor)

    actor to remove



35
36
37
# File 'lib/opee/env.rb', line 35

def self.remove_actor(actor)
  @@actors.delete(actor)
end

+ (Object) rescue(ex)

The log_rescue() method is called and then If a rescuer is set then it's rescue() method is called.

Parameters:

  • ex (Exception)

    Exception to handle



126
127
128
129
130
131
132
133
134
# File 'lib/opee/env.rb', line 126

def self.rescue(ex)
  begin
    log_rescue(ex)
    @@rescuer.rescue(ex) unless @@rescuer.nil?
  rescue Exception => e
    puts "*** #{e.class}: #{e.message}"
    e.backtrace.each { |line| puts "   " + line }
  end
end

+ (Actor) rescuer

Returns the current rescuer Actor. The rescuer us the Actor that is sent an Exception if an Exception is raised by an Actor.

Returns:

  • (Actor)

    the current rescuer



173
174
175
# File 'lib/opee/env.rb', line 173

def self.rescuer()
  @@rescuer
end

+ (Object) rescuer=(actor)

Sets the rescuer to the provided Actor.

Parameters:

  • actor (Actor)

    Actor to use as the rescuer and responds to :rescue



179
180
181
# File 'lib/opee/env.rb', line 179

def self.rescuer=(actor)
  @@rescuer = actor
end

+ (Object) shutdown

Closes all Actors and resets the logger to nil.



62
63
64
65
66
67
68
69
70
71
72
# File 'lib/opee/env.rb', line 62

def self.shutdown()
  until @@actors.empty?
    a = @@actors.pop()
    begin
      a.close()
    rescue Exception => e
      puts "*** shutdown error #{e.class}: #{e.message}"
    end
  end
  @@log = nil
end

+ (Object) start

Calls the start() method on all Actors.



205
206
207
208
# File 'lib/opee/env.rb', line 205

def self.start()
  @@finish_thread = nil
  @@actors.each { |a| a.start() }
end

+ (Object) stop

Calls the stop() method on all Actors.



200
201
202
# File 'lib/opee/env.rb', line 200

def self.stop()
  @@actors.each { |a| a.stop() }
end

+ (Object) wait_close

Waits until all Actors are no longer busy and then closes all Actors.



241
242
243
244
245
246
247
248
249
250
251
252
253
# File 'lib/opee/env.rb', line 241

def self.wait_close()
  while 0 < queue_count()
    wait_finish()
    stop()
    break if 0 == queue_count()
    start()
  end
  @@log = nil
  until @@actors.empty?
    a = @@actors.pop()
    a.close()
  end
end

+ (Object) wait_finish

Waits for all Actors to complete processing. The method only returns when all Actors indicate they are no longer busy.



212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/opee/env.rb', line 212

def self.wait_finish()
  next_time = Time.now + 5.0
  @@finish_thread = Thread.current
  @@actors.each { |a| a.wakeup() }
  while busy?
    sleep(0.2) # actors should wake up when queue is empty
    if !@@log.nil? && Logger::DEBUG >= @@log.severity
      now = Time.now
      if next_time <= now
        log_status()
        next_time = now + 5.0
      end
    end
  end
end

+ (Object) wake_finish

Wakes up the calling thread when an Actor is finished. It is called by the Actor and should not be called by any other code.



230
231
232
233
234
235
236
237
238
# File 'lib/opee/env.rb', line 230

def self.wake_finish()
  unless @@finish_thread.nil?
    # if thread has already exited the an exception will be raised. Ignore it.
    begin
      @@finish_thread.wakeup() 
    rescue
    end
  end
end

+ (Object) warn(message)

Asks the logger to log a message if the current severity level is less than or equal to Logger::WARN.

Parameters:

  • message (String)

    string to log



105
106
107
# File 'lib/opee/env.rb', line 105

def self.warn(message)
  log(Logger::Severity::WARN, message)
end