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
}

Constants inherited from Base

Base::ERROR_INVALID_MESSAGE_STORE

Instance Method Summary collapse

Methods inherited from Base

configure, #select, #transaction

Constructor Details

#initialize(config, logger) ⇒ Disk

Returns a new instance of Disk.



188
189
190
191
192
193
194
195
196
197
198
# File 'lib/reliable-msg/message-store.rb', line 188

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)


218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
# File 'lib/reliable-msg/message-store.rb', line 218

def activate
    super
    Dir.mkdir @path unless File.exist?(@path)
    raise RuntimeError, "The path '#{@path}' is not a directory" unless File.directory?(@path)
    index = "#{@path}/master.idx"
    if File.exist? index
        raise RuntimeError, "Cannot write to master index file '#{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



214
215
216
# File 'lib/reliable-msg/message-store.rb', line 214

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

#deactivateObject



240
241
242
243
244
245
246
247
248
249
250
# File 'lib/reliable-msg/message-store.rb', line 240

def deactivate
    @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



204
205
206
207
208
209
210
211
212
# File 'lib/reliable-msg/message-store.rb', line 204

def setup
    if File.exist?(@path)
        raise RuntimeError, "The path '#{@path}' is not a directory" unless File.directory?(@path)
        false
    else
        Dir.mkdir @path
        true
    end
end

#typeObject



200
201
202
# File 'lib/reliable-msg/message-store.rb', line 200

def type
    TYPE
end