Class: Rider::HostPartitionedQueue

Inherits:
Object
  • Object
show all
Defined in:
lib/rider/part_queue.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ HostPartitionedQueue

Returns a new instance of HostPartitionedQueue.



5
6
7
8
# File 'lib/rider/part_queue.rb', line 5

def initialize(name)
  @name = name
  clear
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



3
4
5
# File 'lib/rider/part_queue.rb', line 3

def name
  @name
end

Class Method Details

.unserialize(name) ⇒ Object



61
62
63
64
65
# File 'lib/rider/part_queue.rb', line 61

def self.unserialize(name)
  filename = "tmp/#{name}.q"
  return nil unless File.exist?(filename)
  YAML.load_file("tmp/#{name}.q")
end

Instance Method Details

#==(another_queue) ⇒ Object



49
50
51
52
53
# File 'lib/rider/part_queue.rb', line 49

def ==(another_queue)
  another_queue.instance_variable_get("@urls_by_host") == @urls_by_host &&
  another_queue.instance_variable_get("@hosts") == @hosts &&
  another_queue.instance_variable_get("@current_host_index") == @current_host_index
end

#clearObject



39
40
41
42
43
# File 'lib/rider/part_queue.rb', line 39

def clear
  @urls_by_host = {}
  @hosts = []
  @current_host_index = 0
end

#empty?Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/rider/part_queue.rb', line 45

def empty?
  @hosts.empty?
end

#push(url) ⇒ Object



10
11
12
13
14
15
16
# File 'lib/rider/part_queue.rb', line 10

def push(url)
  host = get_host(url)
  @hosts << host unless @hosts.include?(host)
  @urls_by_host[host] ||= []
  @urls_by_host[host] << url
  return true
end

#serializeObject



55
56
57
58
59
# File 'lib/rider/part_queue.rb', line 55

def serialize
  File.open(filename, 'w') do |file|
    file.write(self.to_yaml)
  end
end

#shiftObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/rider/part_queue.rb', line 18

def shift
  if empty?
    Rider.log.debug("Q #{name} POP nil")        
    return nil
  end
  host = @hosts[@current_host_index]
  url = @urls_by_host[host].shift
  
  if @urls_by_host[host].empty?
    @hosts.delete_at(@current_host_index) 
    @urls_by_host.delete(host)
    # no need to increment @current_host_index since we just effectively pushed every element down by one
    # by deleting from @hosts, UNLESS it was the last item in the array, in which case that index doesn't
    # exist anymore
    increment_current_host_index if @current_host_index == @hosts.length
  else
    increment_current_host_index
  end
  return url
end