Class: RServiceBus2::StateStorageDir

Inherits:
Object
  • Object
show all
Defined in:
lib/rservicebus2/state_storage/dir.rb

Overview

State Storage on the file system

Instance Method Summary collapse

Constructor Details

#initialize(uri) ⇒ StateStorageDir

rubocop:disable Metrics/AbcSize,Metrics/MethodLength



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/rservicebus2/state_storage/dir.rb', line 5

def initialize(uri)
  @state_dir = uri.path

  Dir.new(@state_dir)
  unless File.writable?(@state_dir)
    puts "***** Directory is not writable, #{@state_dir}."
    puts "***** Make the directory, #{@state_dir}, writable and try
          again."
    puts '***** Or, set the State Directory explicitly by,
          STATE_URI=<dir://path/to/state>'
    abort
  end
rescue Errno::ENOENT
  puts "***** Directory does not exist, #{@state_dir}."
  puts "***** Create the directory, #{@state_dir}, and try again."
  puts "***** eg, mkdir #{@state_dir}"
  puts '***** Or, set the State Directory explicitly by,
        STATE_URI=<dir://path/to/state>'
  abort
rescue Errno::ENOTDIR
  puts "***** The specified path does not point to a directory,
        #{@state_dir}."
  puts "***** Either repoint path to a directory, or remove,
        #{@state_dir}, and create it as a directory."
  puts "***** eg, rm #{@state_dir} && mkdir #{@state_dir}"
  puts '***** Or, set the State Directory explicitly by,
        STATE_URI=<dir://path/to/state>'
  abort
end

Instance Method Details

#beginObject



35
36
37
# File 'lib/rservicebus2/state_storage/dir.rb', line 35

def begin
  @list = []
end

#commitObject



47
48
49
50
51
# File 'lib/rservicebus2/state_storage/dir.rb', line 47

def commit
  @list.each do |e|
    File.open(e['path'], 'w') { |f| f.write(YAML.dump(e['hash'])) }
  end
end

#get(handler) ⇒ Object



39
40
41
42
43
44
45
# File 'lib/rservicebus2/state_storage/dir.rb', line 39

def get(handler)
  path = get_path(handler)
  hash = load(path)
  @list << Hash['path', path, 'hash', hash]

  hash
end

#get_path(handler) ⇒ Object



53
54
55
# File 'lib/rservicebus2/state_storage/dir.rb', line 53

def get_path(handler)
  "#{@state_dir}/#{handler.class.name}"
end

#load(path) ⇒ Object



57
58
59
60
61
62
63
64
65
# File 'lib/rservicebus2/state_storage/dir.rb', line 57

def load(path)
  return {} unless File.exist?(path)

  content = IO.read(path)

  return {} if content == ''

  YAML.load(content)
end