Class: RServiceBus2::StateStorageDir
- Inherits:
-
Object
- Object
- RServiceBus2::StateStorageDir
- Defined in:
- lib/rservicebus2/state_storage/dir.rb
Overview
State Storage on the file system
Instance Method Summary collapse
- #begin ⇒ Object
- #commit ⇒ Object
- #get(handler) ⇒ Object
- #get_path(handler) ⇒ Object
-
#initialize(uri) ⇒ StateStorageDir
constructor
A new instance of StateStorageDir.
- #load(path) ⇒ Object
Constructor Details
#initialize(uri) ⇒ StateStorageDir
Returns a new instance of StateStorageDir.
4 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 |
# File 'lib/rservicebus2/state_storage/dir.rb', line 4 def initialize(uri) @state_dir = uri.path inputdir = 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
#begin ⇒ Object
34 35 36 |
# File 'lib/rservicebus2/state_storage/dir.rb', line 34 def begin @list = [] end |
#commit ⇒ Object
46 47 48 49 50 |
# File 'lib/rservicebus2/state_storage/dir.rb', line 46 def commit @list.each do |e| File.open(e['path'], 'w') { |f| f.write(YAML.dump(e['hash'])) } end end |
#get(handler) ⇒ Object
38 39 40 41 42 43 44 |
# File 'lib/rservicebus2/state_storage/dir.rb', line 38 def get(handler) path = get_path(handler) hash = load(path) @list << Hash['path', path, 'hash', hash] hash end |
#get_path(handler) ⇒ Object
52 53 54 |
# File 'lib/rservicebus2/state_storage/dir.rb', line 52 def get_path(handler) "#{@state_dir}/#{handler.class.name}" end |
#load(path) ⇒ Object
56 57 58 59 60 61 62 63 64 |
# File 'lib/rservicebus2/state_storage/dir.rb', line 56 def load(path) return {} unless File.exist?(path) content = IO.read(path) return {} if content == '' YAML.load(content) end |