Class: FreeMessageQueue::FilePersistentQueue

Inherits:
BaseQueue
  • Object
show all
Defined in:
lib/fmq/queues/file_persistent.rb

Overview

This a FIFO queue that stores messages in the file system

queue_manager = FreeMessageQueue::QueueManager.new(true) do
  setup_queue "/mail_box/threez", FreeMessageQueue::FilePersistentQueue do |q|
    q.folder = "./tmp/mail_box/threez"
    q.max_messages = 10000
  end
end

NOTE the put method is not implemented in this queue. It is a poll only queue.

Constant Summary

Constants inherited from BaseQueue

BaseQueue::INFINITE

Instance Attribute Summary

Attributes inherited from BaseQueue

#bytes, #manager, #max_messages, #max_size, #size

Instance Method Summary collapse

Methods inherited from BaseQueue

#empty?, #initialize

Constructor Details

This class inherits a constructor from FreeMessageQueue::BaseQueue

Instance Method Details

#clearObject

remove all items from the queue



68
69
70
71
72
# File 'lib/fmq/queues/file_persistent.rb', line 68

def clear
  FileUtils.rm all_messages
  @size = 0
  @bytes = 0
end

#folder=(path) ⇒ Object

CONFIGURATION OPTION sets the path to the folder that holds all messages, this will create the folder if it doesn’t exist



62
63
64
65
# File 'lib/fmq/queues/file_persistent.rb', line 62

def folder=(path)
  FileUtils.mkdir_p path unless File.exist? path
  @folder_path = path
end

#pollObject

Return the



35
36
37
38
39
40
41
42
43
# File 'lib/fmq/queues/file_persistent.rb', line 35

def poll()
  check_folder_name
  messages = all_messages.sort!
  return nil if messages.size == 0
  
  msg_bin = File.open(messages.first, "rb") { |f| f.read }
  FileUtils.rm messages.first
  remove_message(Marshal.load(msg_bin))
end

#put(message) ⇒ Object

add one message to the queue (will be saved in file system)



46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/fmq/queues/file_persistent.rb', line 46

def put(message)
  check_folder_name
  return false if message.nil?
  
  add_message(message) # check constraints and update stats
  
  msg_bin = Marshal.dump(message)
  File.open(@folder_path + "/#{Time.now.to_f}.msg", "wb") do |f|
    f.write msg_bin
  end
  return true
end