Module: Postbox::Worker

Extended by:
Worker
Included in:
Worker
Defined in:
lib/postbox/worker.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#worker_threadObject

Returns the value of attribute worker_thread.



5
6
7
# File 'lib/postbox/worker.rb', line 5

def worker_thread
  @worker_thread
end

Instance Method Details

#initObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/postbox/worker.rb', line 15

def init
  Thread.new do
    loop do           
      if Postbox::Stack.empty?
        Thread.main.wakeup if Postbox::IS_IRB
        sleep
      else
        unless process_item(Postbox::Stack.shift)
          sleep 0.5
        end
      end
    end
  end
end

#process_item(item) ⇒ Object



30
31
32
33
34
35
36
37
# File 'lib/postbox/worker.rb', line 30

def process_item(item)
  unless next_attempt_allowed?(item) && (attempts_exceeded?(item) || HTTP::post_item(item))
    Postbox::Stack << item
    return false
  end           
  
  true
end

#wakeupObject



7
8
9
10
11
12
13
# File 'lib/postbox/worker.rb', line 7

def wakeup
   if !@worker_thread.nil? && @worker_thread.alive?
    @worker_thread.wakeup
  else
    @worker_thread = init
  end
end