Class: RServiceBus2::SagaLoader

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

Overview

Given a directory, this class is responsible loading Sagas

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host, saga_manager) ⇒ SagaLoader

Constructor

Parameters:

  • host (RServiceBus2::Host)

    instance

  • appResources (Hash)

    As hash where k is the name of a resource, and v is the resource



12
13
14
15
16
# File 'lib/rservicebus2/saga_loader.rb', line 12

def initialize(host, saga_manager)
  @host = host
  @saga_manager = saga_manager
  @list_of_loaded_paths = {}
end

Instance Attribute Details

#saga_listObject (readonly)

Returns the value of attribute saga_list.



6
7
8
# File 'lib/rservicebus2/saga_loader.rb', line 6

def saga_list
  @saga_list
end

Instance Method Details

#get_list_of_files_for_dir(path) ⇒ Array

This method is overloaded for unit tests

Parameters:

  • path (String)

    directory to check

Returns:

  • (Array)

    a list of paths to files found in the given path



87
88
89
90
91
92
93
# File 'lib/rservicebus2/saga_loader.rb', line 87

def get_list_of_files_for_dir(path)
  list = Dir["#{path}/*"]

  RServiceBus2.rlog "SagaLoader.getListOfFilesForDir. path: #{path}, list: #{list}"

  list
end

#get_require_path(file_path) ⇒ Object

Cleans the given path to ensure it can be used for as a parameter for the

require statement.

Parameters:

  • file_path (String)

    the path to be cleaned



21
22
23
24
25
26
27
# File 'lib/rservicebus2/saga_loader.rb', line 21

def get_require_path(file_path)
  file_path = "./#{file_path}" unless file_path.start_with?('/')

  return file_path.sub('.rb', '') if File.exist?(file_path)

  abort("Filepath, #{file_path}, given for Saga require doesn't exist")
end

#get_saga_name(file_path) ⇒ Object

Extract the top level dir or file name as it is the msg name

Parameters:

  • file_path (String)

    path to check - this can be a directory or file



97
98
99
100
101
102
103
104
# File 'lib/rservicebus2/saga_loader.rb', line 97

def get_saga_name(file_path)
  base_name = File.basename(file_path)
  ext_name = File.extname(base_name)

  saga_name = base_name.sub(ext_name, '')

  "saga_#{saga_name}".gsub(/(?<=_|^)(\w)/) { Regexp.last_match(1).upcase }.gsub(/(?:_)(\w)/, '\1')
end

#load_saga(file_path, saga_name) ⇒ Object

rubocop:disable Metrics/MethodLength



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/rservicebus2/saga_loader.rb', line 61

def load_saga(file_path, saga_name)
  if @list_of_loaded_paths.key?(file_path)
    RServiceBus2.log "Not reloading, #{file_path}"
    return
  end

  RServiceBus2.rlog "file_path: #{file_path}"
  RServiceBus2.rlog "saga_name: #{saga_name}"

  saga = load_saga_from_file(saga_name, file_path)
  RServiceBus2.log "Loaded Saga: #{saga_name}"

  @saga_manager.register_saga(saga)

  @list_of_loaded_paths[file_path] = 1
rescue StandardError => e
  puts "Exception loading saga from file: #{file_path}"
  puts e.message
  puts e.backtrace[0]
  abort
end

#load_saga_from_file(saga_name, file_path) ⇒ Object

rubocop:disable Metrics/MethodLength



39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/rservicebus2/saga_loader.rb', line 39

def load_saga_from_file(saga_name, file_path)
  require_path = get_require_path(file_path)

  require require_path
  begin
    saga = Object.const_get(saga_name)
  rescue StandardError => e
    puts "Expected class name: #{saga_name}, not found after require: #{require_path}" \
         "**** Check in #{file_path} that the class is named: #{saga_name}" \
         '( In case its not that )'
    raise e
  end
  saga
end

#load_sagas_from_path(base_dir) ⇒ Object

Entry point for loading Sagas

Parameters:

  • base_dir (String)

    directory to check - should not have trailing slash



108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/rservicebus2/saga_loader.rb', line 108

def load_sagas_from_path(base_dir)
  RServiceBus2.rlog "SagaLoader.loadSagasFromPath. base_dir: #{base_dir}"

  get_list_of_files_for_dir(base_dir).each do |file_path|
    unless file_path.end_with?('.')
      saga_name = get_saga_name(file_path)
      load_saga(file_path, saga_name)
    end
  end

  self
end