Class: RWGet::Queue

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

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Queue

Returns a new instance of Queue.



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

def initialize(options = {})
  @writer = Tempfile.new("rwget-queue")
  @reader = File.open(@writer.path, "r")
  @dirty = false
end

Instance Method Details

#closeObject



28
29
30
31
# File 'lib/rwget/queue.rb', line 28

def close
  @writer.close
  @reader.close
end

#get(retrying = false) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/rwget/queue.rb', line 14

def get(retrying = false)
  sleep 0.1 if retrying
  if @dirty
    @writer.flush 
    @dirty = false
  end
  line = @reader.gets
  unless line
    return retrying ? nil : get(:retry)
  end
  key, depth = line.split("\t")
  return [key, depth.to_i]
end

#put(key, depth) ⇒ Object



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

def put(key, depth)
  @writer.puts "#{key}\t#{depth}"
  @dirty = true
end