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



10
11
12
13
14
# File 'lib/rservicebus2/saga_loader.rb', line 10

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.



4
5
6
# File 'lib/rservicebus2/saga_loader.rb', line 4

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



83
84
85
86
87
88
89
90
# File 'lib/rservicebus2/saga_loader.rb', line 83

def get_list_of_files_for_dir(path)
  list = Dir[path + '/*']

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

  list
end

#get_require_path(filePath) ⇒ 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



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

def get_require_path(filePath)
  file_path = './' + file_path unless file_path.start_with?('/')

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

  abort('Filepath, ' + filePath + ", 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



94
95
96
97
98
99
100
101
# File 'lib/rservicebus2/saga_loader.rb', line 94

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}"
end

#load_saga(file_path, saga_name) ⇒ Object

Wrapper function

Parameters:

  • filePath (String)
  • sagaName (String)


56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/rservicebus2/saga_loader.rb', line 56

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

  begin
    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
end

#load_saga_from_file(saga_name, file_path) ⇒ RServiceBus2::Saga

Instantiate the saga named in sagaName from the file name in filePath Exceptions will be raised if encountered when loading sagas. This is a load time activity, so sagas should load correctly. As much information as possible is returned to enable the saga to be fixed, or configuration corrected.

Parameters:

  • sagaName (String)

    name of the saga to instantiate

  • filePath (String)

    the path to the file to be loaded

Returns:

  • (RServiceBus2::Saga)

    the loader



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/rservicebus2/saga_loader.rb', line 35

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
    puts '**** Check in ' + file_path + ' that the class is named : ' +
      saga_name
    puts '( 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



105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/rservicebus2/saga_loader.rb', line 105

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 filePath.end_with?('.')
      saga_name = get_saga_name(file_path)
      load_saga(file_path, saga_name)
    end
  end

  self
end