Class: DispatchRider::QueueServices::FileSystem::Queue

Inherits:
Object
  • Object
show all
Defined in:
lib/dispatch-rider/queue_services/file_system/queue.rb

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Queue

Returns a new instance of Queue.



8
9
10
11
# File 'lib/dispatch-rider/queue_services/file_system/queue.rb', line 8

def initialize(path)
  FileUtils.mkdir_p(path)
  @path = path
end

Instance Method Details

#add(item) ⇒ Object



13
14
15
16
17
# File 'lib/dispatch-rider/queue_services/file_system/queue.rb', line 13

def add(item)
  name_base = "#{@path}/#{Time.now.to_f}"
  File.write("#{name_base}.inprogress", item)
  FileUtils.mv("#{name_base}.inprogress", "#{name_base}.ready")
end

#popObject



19
20
21
22
23
24
25
26
# File 'lib/dispatch-rider/queue_services/file_system/queue.rb', line 19

def pop
  file_path = next_item(10)
  return nil unless file_path

  file_path_inflight = file_path.gsub(/\.ready$/, '.inflight')
  FileUtils.mv(file_path, file_path_inflight)
  File.new(file_path_inflight)
end

#put_back(item) ⇒ Object



28
29
30
31
# File 'lib/dispatch-rider/queue_services/file_system/queue.rb', line 28

def put_back(item)
  add(item)
  remove(item)
end

#remove(item) ⇒ Object



33
34
35
36
# File 'lib/dispatch-rider/queue_services/file_system/queue.rb', line 33

def remove(item)
  item.close
  File.unlink(item.path)
end