Class: DispatchRider::QueueServices::FileSystem::Queue
- Inherits:
-
Object
- Object
- DispatchRider::QueueServices::FileSystem::Queue
- Defined in:
- lib/dispatch-rider/queue_services/file_system/queue.rb
Instance Method Summary collapse
- #add(item) ⇒ Object
-
#initialize(path) ⇒ Queue
constructor
A new instance of Queue.
- #pop ⇒ Object
- #put_back(item) ⇒ Object
- #remove(item) ⇒ Object
- #size ⇒ Object
Constructor Details
#initialize(path) ⇒ Queue
Returns a new instance of Queue.
6 7 8 9 |
# File 'lib/dispatch-rider/queue_services/file_system/queue.rb', line 6 def initialize(path) FileUtils.mkdir_p(path) @path = path end |
Instance Method Details
#add(item) ⇒ Object
11 12 13 14 15 |
# File 'lib/dispatch-rider/queue_services/file_system/queue.rb', line 11 def add(item) name_base = "#{@path}/#{Time.now.to_f}" File.open("#{name_base}.inprogress", "w"){ |f| f.write(item) } FileUtils.mv("#{name_base}.inprogress", "#{name_base}.ready") end |
#pop ⇒ Object
17 18 19 20 21 22 23 |
# File 'lib/dispatch-rider/queue_services/file_system/queue.rb', line 17 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
25 26 27 28 |
# File 'lib/dispatch-rider/queue_services/file_system/queue.rb', line 25 def put_back(item) add(item) remove(item) end |
#remove(item) ⇒ Object
30 31 32 33 |
# File 'lib/dispatch-rider/queue_services/file_system/queue.rb', line 30 def remove(item) item.close File.unlink(item.path) end |
#size ⇒ Object
35 36 37 |
# File 'lib/dispatch-rider/queue_services/file_system/queue.rb', line 35 def size file_paths.size end |