Class: ReliableMsg::MessageStore::Disk

Inherits:
Base
  • Object
show all
Defined in:
lib/reliable-msg/message-store.rb

Overview

:nodoc:

Constant Summary collapse

TYPE =
self.name.split('::').last.downcase
DEFAULT_PATH =

Default path where index and messages are stored.

'queues'
MAX_OPEN_FILES =

Maximum number of open files.

20
DEFAULT_CONFIG =
{
    "type"=>TYPE,
    "path"=>DEFAULT_PATH
}
ERROR_PATH_NOT_DIR =

:nodoc:

"The path %s is not a directory"
ERROR_FAILED_WRITE_MASTER =

:nodoc:

"Cannot write to master index file %s"

Constants inherited from Base

Base::ERROR_INVALID_MESSAGE_STORE

Instance Method Summary collapse

Methods inherited from Base

configure, #get_headers, #get_last, #get_message, #transaction

Constructor Details

#initialize(config, logger) ⇒ Disk

Returns a new instance of Disk.



226
227
228
229
230
231
232
233
234
235
236
# File 'lib/reliable-msg/message-store.rb', line 226

def initialize config, logger
    super logger
    @fsync = config['fsync']
    # file_map maps messages (by ID) to files. The value is a two-item array: the file
    # name and, if opened, the File object. file_free keeps a list of all currently
    # unused files, using the same two-item arrays.
    @file_map = {}
    @file_free = []
    # Make sure the path points to the queue directory, and the master index is writeable.
    @path = File.expand_path(config['path'] || DEFAULT_PATH)
end

Instance Method Details

#activateObject

Raises:

  • (RuntimeError)


260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
# File 'lib/reliable-msg/message-store.rb', line 260

def activate
    super
    Dir.mkdir @path unless File.exist?(@path)
    raise RuntimeError, format(ERROR_PATH_NOT_DIR, @path) unless File.directory?(@path)
    index = "#{@path}/master.idx"
    if File.exist? index
        raise RuntimeError, format(ERROR_FAILED_WRITE_MASTER, index) unless File.writable?(index)
        @file = File.open index, "r+"
        @file.flock File::LOCK_EX
        @file.binmode # Things break if you forget binmode on Windows.
        load_index
    else
        @file = File.open index, "w"
        @file.flock File::LOCK_EX
        @file.binmode # Things break if you forget binmode on Windows.
        @last_block = @last_block_end = 8
        # Save. This just prevents us from starting with an empty file, and to
        # enable load_index().
        update [], [], []
    end
end

#configurationObject



255
256
257
# File 'lib/reliable-msg/message-store.rb', line 255

def configuration
    { "type"=>TYPE, "path"=>@path }
end

#deactivateObject



283
284
285
286
287
288
289
290
291
292
293
294
# File 'lib/reliable-msg/message-store.rb', line 283

def deactivate
    @file.flock File::LOCK_UN
    @file.close
    @file_map.each_pair do |id, map|
        map[1].close if map[1]
    end
    @file_free.each do |map|
        map[1].close if map[1]
    end
    @file_map = @file_free = nil
    super
end

#setupObject



244
245
246
247
248
249
250
251
252
# File 'lib/reliable-msg/message-store.rb', line 244

def setup
    if File.exist?(@path)
        raise RuntimeError, format(ERROR_PATH_NOT_DIR, @path) unless File.directory?(@path)
        false
    else
        Dir.mkdir @path
        true
    end
end

#typeObject



239
240
241
# File 'lib/reliable-msg/message-store.rb', line 239

def type
    TYPE
end