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



190
191
192
193
# File 'lib/opee/env.rb', line 190

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



87
88
89
# File 'lib/opee/env.rb', line 87

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



108
109
110
# File 'lib/opee/env.rb', line 108

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



115
116
117
# File 'lib/opee/env.rb', line 115

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



94
95
96
# File 'lib/opee/env.rb', line 94

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



73
74
75
76
77
78
79
80
81
82
# File 'lib/opee/env.rb', line 73

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



137
138
139
140
141
142
143
144
145
# File 'lib/opee/env.rb', line 137

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



245
246
247
248
249
# File 'lib/opee/env.rb', line 245

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



150
151
152
153
# File 'lib/opee/env.rb', line 150

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



159
160
161
162
163
164
# File 'lib/opee/env.rb', line 159

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



181
182
183
184
185
# File 'lib/opee/env.rb', line 181

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



122
123
124
125
126
127
128
129
130
# File 'lib/opee/env.rb', line 122

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



169
170
171
# File 'lib/opee/env.rb', line 169

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



175
176
177
# File 'lib/opee/env.rb', line 175

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
# File 'lib/opee/env.rb', line 62

def self.shutdown()
  until @@actors.empty?
    a = @@actors.pop()
    a.close()
  end
  @@log = nil
end

+ (Object) start

Calls the start() method on all Actors.



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

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

+ (Object) stop

Calls the stop() method on all Actors.



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

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

+ (Object) wait_close

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



231
232
233
234
235
236
237
238
239
240
241
242
243
# File 'lib/opee/env.rb', line 231

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.



208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/opee/env.rb', line 208

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 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.



226
227
228
# File 'lib/opee/env.rb', line 226

def self.wake_finish()
  @@finish_thread.wakeup() unless @@finish_thread.nil?
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



101
102
103
# File 'lib/opee/env.rb', line 101

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