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

  @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


171
172
173
174
175
# File 'lib/ruote/log/wait_logger.rb', line 171

def self.fp(msg)

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

Instance Method Details

#color=(c) ⇒ Object


166
167
168
169
# File 'lib/ruote/log/wait_logger.rb', line 166

def color=(c)

  @color = c
end

#dumpObject

Debug only : dumps all the seen events to $stdout


122
123
124
125
# File 'lib/ruote/log/wait_logger.rb', line 122

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.


115
116
117
118
# File 'lib/ruote/log/wait_logger.rb', line 115

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
108
109
110
# 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'

  @mutex.synchronize do

    @seen << msg
    @log << msg

    while @log.size > @log_max; @log.shift; end
    while @seen.size > @log_max; @seen.shift; end
  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.


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

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

    @mutex.synchronize { check_waiting }

    break if Thread.current['__result__']

    sleep 0.007
  end

  Thread.current['__result__']
end