Method: Curl.execute

Defined in:
lib/rhack/curl/global.rb

.execute(raise_errors = false) ⇒ Object Also known as: run



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
42
43
44
# File 'lib/rhack/curl/global.rb', line 5

def execute(raise_errors=false)
  if @@carier_thread and s = @@carier_thread.status
    L.log "Carier Thread allready started and has status #{s}"
  else
    if s = status(raise_errors)
      L.warn s
    end
    L.log(@@carier_thread ? "Resetting Carier thread" : "Setting Carier thread up")
    @@carier_thread = thread {
      error = nil
      begin
        yield if block_given?
      rescue => error
        nil
      end
      loop {
        begin
          # with true argument (idle) it would break only if there is no requests to perform
          # and still carier thread is joined
          break unless @@carier.perform true
          L.log "All requests have been performed; idling..."
        rescue => error
          L.log "Catched #{error.class.name} in Carier Thread"
          break
          # but ruby mystically crashes if next sequence occur:
          # Multi performs and can't see any requests so entering idle mode
          # we add some requests and multi load them
          # one of requests' callbacks raises error in *main* thread
          # so we can't allow any raises here, instead, keep them in 'wait' section
        end
      } unless error
      L.log "Nothing to perform; recalling..."
      error
    }
    # until main thread has sleep a bit, $CarierThread will have status "run", 
    # no matter whether it's idling or performing requests
    sleep 0.001
    true
  end
end