Module: Irrc::Runner

Included in:
Irrd::Client, Whoisd::Client
Defined in:
lib/irrc/runner.rb

Instance Method Summary collapse

Instance Method Details

#run(threads) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/irrc/runner.rb', line 3

def run(threads)
  done = []

  loop do
    # Trick to avoid dead lock
    if last_thread_of?(threads) && @queue.empty?
      terminate

      # Queue guard objects notifying other threads to return results
      logger.debug "Queue #{threads - 1} guard objects"
      (threads - 1).times { @queue.push nil }

      return done
    end

    query = @queue.pop

    # Trick to avoid dead lock
    if query.nil?
      terminate
      return done
    end

    connect unless established?

    begin
      logger.info "Processing #{query.object}"
      query = process(query)
      query.success
      logger.debug "Queue new #{query.children.size} queries"
      query.children.each {|q| @queue << q }
    rescue
      logger.error "#{$!.message} when processing #{query.object} for #{query.root.object}"
      query.fail
    end

    done << query if query.root?
  end
end