Class: RServiceBus::SagaLoader

Inherits:
Object
  • Object
show all
Defined in:
lib/rservicebus/SagaLoader.rb

Overview

Given a directory, this class is responsible loading Sagas

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host, sagaManager) ⇒ SagaLoader

Constructor

Parameters:

  • host (RServiceBus::Host)

    instance

  • appResources (Hash)

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



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

#sagaListObject (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

Parameters:

  • path (String)

    directory to check

Returns:

  • (Array)

    a list of paths to files found in the given path



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.

Parameters:

  • filePath (String)

    the path to be cleaned



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

Parameters:

  • filePath (String)

    path to check - this can be a directory or file



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

Parameters:

  • filePath (String)
  • sagaName (String)


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.message
		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.

Parameters:

  • sagaName (String)

    name of the saga to instantiate

  • filePath (String)

    the path to the file to be loaded

Returns:

  • (RServiceBus::Saga)

    the loader



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

Parameters:

  • baseDir (String)

    directory to check - should not have trailing slash



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