Class: RServiceBus2::SagaStorageDir

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

Overview

Saga Storage Dir

Instance Method Summary collapse

Constructor Details

#initialize(uri) ⇒ SagaStorageDir

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
# File 'lib/rservicebus2/saga_storage/dir.rb', line 5

def initialize(uri)
  @saga_dir = uri.path

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

Instance Method Details

#beginObject

Start



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

def begin
  @list = []
  @deleted = []
end

#commitObject

Finish



56
57
58
59
60
61
62
63
# File 'lib/rservicebus2/saga_storage/dir.rb', line 56

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

#delete(correlation_id) ⇒ Object



68
69
70
# File 'lib/rservicebus2/saga_storage/dir.rb', line 68

def delete(correlation_id)
  @deleted << correlation_id
end

#get(correlation_id) ⇒ Object

Get



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

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

  data
end

#get_path(correlation_id) ⇒ Object

Detail Functions



73
74
75
# File 'lib/rservicebus2/saga_storage/dir.rb', line 73

def get_path(correlation_id)
  "#{@saga_dir}/saga-#{correlation_id}"
end

#load(path) ⇒ Object



77
78
79
80
81
82
83
84
85
# File 'lib/rservicebus2/saga_storage/dir.rb', line 77

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

  content = IO.read(path)

  return {} if content == ''

  YAML.load(content)
end

#rollbackObject



65
66
# File 'lib/rservicebus2/saga_storage/dir.rb', line 65

def rollback
end

#set(data) ⇒ Object

Set



41
42
43
44
# File 'lib/rservicebus2/saga_storage/dir.rb', line 41

def set(data)
  path = get_path(data.correlation_id)
  @list << Hash['path', path, 'data', data]
end