Class: Requeus::Queue

Inherits:
Object
  • Object
show all
Defined in:
lib/requeus/queue.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(conf) ⇒ Queue

Returns a new instance of Queue.



3
4
5
6
7
8
# File 'lib/requeus/queue.rb', line 3

def initialize conf
  @name = conf['name']
  @endpoint = conf['endpoint']
  @workers_count = conf['workers'].to_i
  @interval = conf['interval'].to_f
end

Instance Attribute Details

#endpointObject (readonly)

Returns the value of attribute endpoint.



10
11
12
# File 'lib/requeus/queue.rb', line 10

def endpoint
  @endpoint
end

#nameObject (readonly)

Returns the value of attribute name.



10
11
12
# File 'lib/requeus/queue.rb', line 10

def name
  @name
end

Instance Method Details

#start_workersObject



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
45
46
47
48
49
50
51
52
# File 'lib/requeus/queue.rb', line 13

def start_workers
  workers_queue = SizedQueue.new(@workers_count)

  @workers_count.times.map do
    Thread.new do
      loop do
        begin
          server, queue, handle, request = workers_queue.pop

          if request.do_request queue.endpoint
            server.confirm queue.name, handle
            request.delete_files
          end
        rescue Exception => e
          puts e
          puts e.backtrace.join("\n")
        end
      end
    end
  end

  Requeus::Impl.instance.server_sequence.map do |s|
    Thread.new(s) do |server|
      loop do
        begin
          requests = server.get @name, workers_queue.num_waiting

          if requests.empty?
            sleep server.interval * @interval
          else
            requests.each {|handle, request| workers_queue << [server, self, handle, Requeus::Request.from_json(request)]}
          end
        rescue Exception => e
          puts e
          puts e.backtrace.join("\n")
        end
      end
    end
  end
end