Class: RServiceBus::SagaLoader
- Inherits:
-
Object
- Object
- RServiceBus::SagaLoader
- Defined in:
- lib/rservicebus/SagaLoader.rb
Overview
Given a directory, this class is responsible loading Sagas
Instance Attribute Summary collapse
-
#sagaList ⇒ Object
readonly
Returns the value of attribute sagaList.
Instance Method Summary collapse
-
#getListOfFilesForDir(path) ⇒ Array
This method is overloaded for unit tests.
-
#getRequirePath(filePath) ⇒ Object
Cleans the given path to ensure it can be used for as a parameter for the require statement.
-
#getSagaName(filePath) ⇒ Object
Extract the top level dir or file name as it is the msg name.
-
#initialize(host, sagaManager) ⇒ SagaLoader
constructor
Constructor.
-
#loadSaga(filePath, sagaName) ⇒ Object
Wrapper function.
-
#loadSagaFromFile(sagaName, filePath) ⇒ RServiceBus::Saga
Instantiate the saga named in sagaName from the file name in filePath Exceptions will be raised if encountered when loading sagas.
-
#loadSagasFromPath(baseDir) ⇒ Object
Entry point for loading Sagas.
Constructor Details
#initialize(host, sagaManager) ⇒ SagaLoader
Constructor
14 15 16 17 18 19 20 |
# File 'lib/rservicebus/SagaLoader.rb', line 14 def initialize( host, sagaManager ) @host = host @sagaManager = sagaManager @listOfLoadedPaths = Hash.new end |
Instance Attribute Details
#sagaList ⇒ Object (readonly)
Returns the value of attribute sagaList.
6 7 8 |
# File 'lib/rservicebus/SagaLoader.rb', line 6 def sagaList @sagaList end |
Instance Method Details
#getListOfFilesForDir(path) ⇒ Array
This method is overloaded for unit tests
96 97 98 99 100 101 102 |
# File 'lib/rservicebus/SagaLoader.rb', line 96 def getListOfFilesForDir( path ) list = Dir[path + '/*']; RServiceBus.rlog "SagaLoader.getListOfFilesForDir. path: #{path}, list: #{list}" return list end |
#getRequirePath(filePath) ⇒ Object
Cleans the given path to ensure it can be used for as a parameter for the require statement.
25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/rservicebus/SagaLoader.rb', line 25 def getRequirePath( filePath ) unless filePath.start_with?('/') then filePath = './' + filePath end if File.exists?( filePath ) then return filePath.sub( '.rb', '') end abort( 'Filepath, ' + filePath + ", given for Saga require doesn't exist" ); end |
#getSagaName(filePath) ⇒ Object
Extract the top level dir or file name as it is the msg name
107 108 109 110 111 112 113 114 |
# File 'lib/rservicebus/SagaLoader.rb', line 107 def getSagaName( filePath ) baseName = File.basename( filePath ) extName = File.extname( baseName ) sagaName = baseName.sub( extName, '') return "Saga_#{sagaName}" end |
#loadSaga(filePath, sagaName) ⇒ Object
Wrapper function
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/rservicebus/SagaLoader.rb', line 66 def loadSaga(filePath, sagaName) if @listOfLoadedPaths.has_key?( filePath ) then RServiceBus.log "Not reloading, #{filePath}" return end begin RServiceBus.rlog 'filePath: ' + filePath RServiceBus.rlog 'sagaName: ' + sagaName saga = self.loadSagaFromFile( sagaName, filePath ) RServiceBus.log 'Loaded Saga: ' + sagaName @sagaManager.RegisterSaga( saga ) @listOfLoadedPaths[filePath] = 1 rescue Exception => e puts 'Exception loading saga from file: ' + filePath puts e. puts e.backtrace[0] abort() end end |
#loadSagaFromFile(sagaName, filePath) ⇒ RServiceBus::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.
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/rservicebus/SagaLoader.rb', line 45 def loadSagaFromFile( sagaName, filePath ) requirePath = self.getRequirePath( filePath ) require requirePath begin saga = Object.const_get(sagaName); rescue Exception => e puts 'Expected class name: ' + sagaName + ', not found after require: ' + requirePath puts '**** Check in ' + filePath + ' that the class is named : ' + sagaName puts '( In case its not that )' raise e end return saga end |
#loadSagasFromPath(baseDir) ⇒ Object
Entry point for loading Sagas
120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/rservicebus/SagaLoader.rb', line 120 def loadSagasFromPath(baseDir) RServiceBus.rlog "SagaLoader.loadSagasFromPath. baseDir: #{baseDir}" self.getListOfFilesForDir(baseDir).each do |filePath| unless filePath.end_with?('.') then sagaName = self.getSagaName(filePath) self.loadSaga(filePath, sagaName) end end return self end |