lib/ruote/log/test_logger.rb in ruote-2.1.10 vs lib/ruote/log/test_logger.rb in ruote-2.1.11

- old
+ new

@@ -20,17 +20,19 @@ # THE SOFTWARE. # # Made in Japan. #++ -#require 'ruote/util/tree' +require 'ruote/log/pretty' module Ruote class TestLogger + include PrettyLogging + attr_reader :seen attr_reader :log attr_accessor :noisy @@ -50,39 +52,47 @@ # end @seen = [] @log = [] - @waiting = nil + @waiting = [] @count = -1 @color = 33 @noisy = false - - # NOTE - # in case of troubles, why not have the wait_for has an event ? end def notify (msg) - #@context.storage.put(event.merge('type' => 'archived_msgs')) - puts(pretty_print(msg)) if @noisy @seen << msg @log << msg check_waiting end + # Blocks until one or more interests are satisfied. + # + # interests must be an array of interests. Please refer to + # Engine#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. + # def wait_for (interests) - @waiting = [ Thread.current, interests ] + @waiting << [ Thread.current, interests ] check_waiting - Thread.stop if @waiting + Thread.stop if @waiting.find { |w| w.first == Thread.current } # and when this thread gets woken up, go on and return __result__ Thread.current['__result__'] end @@ -107,43 +117,56 @@ protected def check_waiting - return unless @waiting + return if @waiting.size < 1 while msg = @seen.shift - - break if check_msg(msg) + check_msg(msg) end end def check_msg (msg) - if check_interest(msg) + wakeup = [] - thread = @waiting.first - @waiting = nil - thread['__result__'] = msg - thread.wakeup + @waiting.each do |thread, interests| - true - else + wakeup << thread if matches(interests, msg) + end - false + @waiting.delete_if { |t, i| i.size < 1 } + + wakeup.each do |thread| + + thread['__result__'] = msg + thread.wakeup end end FINAL_ACTIONS = %w[ terminated ceased error_intercepted ] - def check_interest (msg) + # Checks whether message msg matches any of interests being waited for. + # + # Some interests look for actions on particular workflows (e.g., + # waiting for some workflow to finish). Other interests are not + # attached to any particular workflow (e.g., :inactive waits until + # the engine finishes processing all active and pending workflows) + # but are still satisfied when actions happen on workflows (e.g., + # the last workflow being run finishes). + # + # Returns true if all interests being waited for have been satisfied, + # false otherwise. + # + def matches (interests, msg) action = msg['action'] - @waiting.last.each do |interest| + interests.each do |interest| - satisfied = if interest == :inactive + satisfied = if interest == :inactive (FINAL_ACTIONS.include?(action) && @context.worker.inactive?) elsif interest == :empty @@ -153,125 +176,27 @@ (action == 'dispatch' && msg['participant_name'] == interest.to_s) elsif interest.is_a?(Fixnum) - @waiting[-1] = @waiting[-1] - [ interest ] + interests.delete(interest) + if (interest > 1) - @waiting[-1] << (interest - 1) + interests << (interest - 1) false else true end else # wfid (FINAL_ACTIONS.include?(action) && msg['wfid'] == interest) end - @waiting[-1] = @waiting[-1] - [ interest ] if satisfied + interests.delete(interest) if satisfied end - @waiting.last.size < 1 - end - - # <ESC>[{attr1};...;{attrn}m - # - # 0 Reset all attributes - # 1 Bright - # 2 Dim - # 4 Underscore - # 5 Blink - # 7 Reverse - # 8 Hidden - # - # Foreground Colours - # 30 Black - # 31 Red - # 32 Green - # 33 Yellow - # 34 Blue - # 35 Magenta - # 36 Cyan - # 37 White - # - # Background Colours - # 40 Black - # 41 Red - # 42 Green - # 43 Yellow - # 44 Blue - # 45 Magenta - # 46 Cyan - # 47 White - - def color (mod, s, clear=false) - - return s if Ruote::WIN - return s unless STDOUT.tty? - - "[#{mod}m#{s}#{clear ? '' : "[#{@color}m"}" - end - - def pretty_print (msg) - - @count += 1 - @count = 0 if @count > 9 - - ei = self.object_id.to_s[-2..-1] - - fei = msg['fei'] - depth = fei ? fei['expid'].split('_').size : 0 - - i = fei ? - [ fei['wfid'], fei['sub_wfid'], fei['expid'] ].join(' ') : - msg['wfid'] - - rest = msg.dup - %w[ - _id put_at _rev - type action - fei wfid variables - ].each { |k| rest.delete(k) } - - if v = rest['parent_id'] - rest['parent_id'] = Ruote.to_storage_id(v) - end - if v = rest.delete('workitem') - rest[:wi] = [ - v['fei'] ? Ruote.to_storage_id(v['fei']) : nil, - v['fields'].size ] - end - - { 'tree' => :t, 'parent_id' => :pi }.each do |k0, k1| - if v = rest.delete(k0) - rest[k1] = v - end - end - - action = msg['action'][0, 2] - action = case msg['action'] - when 'receive' then 'rc' - when 'dispatched' then 'dd' - when 'dispatch_cancel' then 'dc' - else action - end - action = case action - when 'la' then color('4;32', action) - when 'te' then color('4;31', action) - when 'ce' then color('31', action) - when 'ca' then color('31', action) - when 'rc' then color('4;33', action) - when 'di' then color('4;33', action) - when 'dd' then color('4;33', action) - when 'dc' then color('4;31', action) - else action - end - - color( - @color, - "#{@count} #{ei} #{' ' * depth}#{action} * #{i} #{rest.inspect}", - true) + interests.size < 1 end end end