Class: Ruote::WaitLogger

Inherits:
Object
  • Object
show all
Defined in:
lib/ruote/log/wait_logger.rb,
lib/ruote/log/fancy_printing.rb

Overview

The logic behind Ruote::Dashboard#wait_for is implemented here.

This logger keeps track of the last 56 events. This number can be tweaked via the ‘wait_logger_max’ storage option (ruote.rubyforge.org/configuration.html)

One doesn’t play directly with this class. It’s available only via the Ruote::Dashboard#wait_for and Ruote::Dashboard#noisy=

To access the log of processed msgs, look at history services, not at this wait_logger.

options (storage initialization options)

wait_logger_max(Integer)

defaults to 77, max number of recent records to keep track of

wait_logger_timeout(Integer)

defaults to 60 (seconds), #wait_for times out after how many seconds?

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context) ⇒ WaitLogger

Returns a new instance of WaitLogger.



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/ruote/log/wait_logger.rb', line 75

def initialize(context)

  @context = context

  @seen = []
  @log = []
  @waiting = []

  @count = -1
  @color = 33
  @noisy = false

  @log_max = context['wait_logger_max'] || 77
  @timeout = context['wait_logger_timeout'] || 60 # in seconds

  @check_mutex = Mutex.new
end

Instance Attribute Details

#logObject (readonly)

Returns the value of attribute log.



63
64
65
# File 'lib/ruote/log/wait_logger.rb', line 63

def log
  @log
end

#noisyObject

When set to true, this logger will spit out the ruote activity happening in this Ruby’s runtime ruote worker (if any) to $stdout.



68
69
70
# File 'lib/ruote/log/wait_logger.rb', line 68

def noisy
  @noisy
end

#seenObject (readonly)

Returns the value of attribute seen.



62
63
64
# File 'lib/ruote/log/wait_logger.rb', line 62

def seen
  @seen
end

#timeoutObject

The timeout for #wait_for. Defaults to 60 (seconds). When set to number inferior or equal to zero, no timeout will be enforced.



73
74
75
# File 'lib/ruote/log/wait_logger.rb', line 73

def timeout
  @timeout
end

Class Method Details

.fp(msg) ⇒ Object



168
169
170
171
172
# File 'lib/ruote/log/wait_logger.rb', line 168

def self.fp(msg)

  @logger ||= TestLogger.new(nil)
  puts @logger.send(:fancy_print, msg)
end

Instance Method Details

#color=(c) ⇒ Object



163
164
165
166
# File 'lib/ruote/log/wait_logger.rb', line 163

def color=(c)

  @color = c
end

#dumpObject

Debug only : dumps all the seen events to $stdout



119
120
121
122
# File 'lib/ruote/log/wait_logger.rb', line 119

def dump

  @seen.collect { |msg| fancy_print(msg) }.join("\n")
end

#fancy_logObject

Returns an array of the latest msgs, but fancy-printed. The oldest first.



112
113
114
115
# File 'lib/ruote/log/wait_logger.rb', line 112

def fancy_log

  @log.collect { |msg| fancy_print(msg) }
end

#on_msg(msg) ⇒ Object

The context will call this method for each msg sucessfully processed by the worker.



96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/ruote/log/wait_logger.rb', line 96

def on_msg(msg)

  puts(fancy_print(msg, @noisy)) if @noisy

  return if msg['action'] == 'noop'

  @seen << msg
  @log << msg

  while @log.size > @log_max; @log.shift; end
  while @seen.size > @log_max; @seen.shift; end
end

#wait_for(interests, opts = {}) ⇒ Object

Blocks until one or more interests are satisfied.

interests must be an array of interests. Please refer to Dashboard#wait_for documentation for allowed values of each interest.

If multiple interests are given, wait_for blocks until all of the interests are satisfied.

wait_for may only be used by one thread at a time. If one thread calls wait_for and later another thread calls wait_for while the first thread is waiting, the first thread’s interests are lost and the first thread will never wake up.



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/ruote/log/wait_logger.rb', line 137

def wait_for(interests, opts={})

  @waiting << [ Thread.current, interests ]

  Thread.current['__result__'] = nil
  start = Time.now

  to = opts[:timeout] || @timeout
  to = nil if to.nil? || to <= 0

  loop do

    raise(
      Ruote::LoggerTimeout.new(interests, to)
    ) if to && (Time.now - start) > to

    @check_mutex.synchronize { check_waiting }

    break if Thread.current['__result__']

    sleep 0.007
  end

  Thread.current['__result__']
end