Class: RServiceBus::Saga_Manager

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

Instance Method Summary collapse

Constructor Details

#initialize(host, resourceManager, sagaStorage) ⇒ Saga_Manager

Returns a new instance of Saga_Manager.



6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/rservicebus/Saga/Manager.rb', line 6

def initialize( host, resourceManager, sagaStorage )
	@handler = Hash.new
	@startWith = Hash.new
	@saga = Hash.new

	@host = host

       @resourceManager = resourceManager
       @resourceListBySagaName = Hash.new
       
       @sagaStorage = sagaStorage
end

Instance Method Details

#GetMethodsByPrefix(saga, prefix) ⇒ Object



20
21
22
23
24
25
26
27
# File 'lib/rservicebus/Saga/Manager.rb', line 20

def GetMethodsByPrefix( saga, prefix )
       list = []
       saga.instance_methods.each do |name|
           list.push name.to_s.sub( prefix, '') if name.to_s.slice( 0,prefix.length ) == prefix
       end
       
       return list
end

#GetStartWithMethodNames(saga) ⇒ Object



29
30
31
# File 'lib/rservicebus/Saga/Manager.rb', line 29

def GetStartWithMethodNames( saga )
	return self.GetMethodsByPrefix( saga, 'StartWith_')
end

#Handle(rmsg) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/rservicebus/Saga/Manager.rb', line 88

def Handle( rmsg )
    @resourcesUsed = Hash.new
    handled = false
    msg = rmsg.msg

    RServiceBus.log "SagaManager, started processing, #{msg.class.name}", true
    unless @startWith[msg.class.name].nil? then
      @startWith[msg.class.name].each do |saga|
        data = Saga_Data.new(saga)
        @sagaStorage.Set(data)

        methodName = "StartWith_#{msg.class.name}"
        self.ProcessMsg(saga, data, methodName, msg)

        handled = true
      end
    end
    return handled if handled == true


    return false if rmsg.correlationId.nil?
    data = @sagaStorage.Get( rmsg.correlationId )
    return handled if data.nil?
    methodName = "Handle_#{msg.class.name}"
    saga = @saga[data.sagaClassName];
    self.ProcessMsg( saga, data, methodName, msg );


    return true
end

#interrogateSagaForAppResources(saga) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/rservicebus/Saga/Manager.rb', line 45

def interrogateSagaForAppResources( saga )
    RServiceBus.rlog "Checking app resources for: #{saga.class.name}"
    RServiceBus.rlog "If your attribute is not getting set, check that it is in the 'attr_accessor' list"
    
    @resourceListBySagaName[saga.class.name] = Array.new
    @resourceManager.getAll.each do |k,v|
        if saga.class.method_defined?( k ) then
            @resourceListBySagaName[saga.class.name] << k
            RServiceBus.log "Resource attribute, #{k}, found for: " + saga.class.name
        end
    end

    return self
end

#prepSaga(saga) ⇒ Object



78
79
80
81
82
83
84
85
86
# File 'lib/rservicebus/Saga/Manager.rb', line 78

def prepSaga( saga )
  unless @resourceListBySagaName[saga.class.name].nil? then
    @resourceListBySagaName[saga.class.name].each do |k, v|
      saga.instance_variable_set("@#{k}", @resourceManager.get(k).getResource())
      RServiceBus.rlog "App resource attribute, #{k}, set for: " + saga.class.name
    end
  end

end

#ProcessMsg(saga, data, methodName, msg) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/rservicebus/Saga/Manager.rb', line 119

def ProcessMsg( saga, data, methodName, msg )
    @host.sagaData = data
    saga.data = data
    self.prepSaga( saga )

    if saga.class.method_defined?( methodName ) then
        saga.send methodName, msg
    end

    if data.finished == true then
        @sagaStorage.Delete data.correlationId
    end

    @host.sagaData = nil
    #Save Data
end

#RegisterSaga(saga) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/rservicebus/Saga/Manager.rb', line 61

def RegisterSaga( saga )
       s = saga.new
       self.setBusAttributeIfRequested( s )

	self.GetStartWithMethodNames( saga ).each do |msgName|
           @startWith[msgName] = Array.new if @startWith[msgName].nil?
           @startWith[msgName] << s

           RServiceBus.log "Registered, #{saga.name}, to StartWith, #{msgName}"
       end

	@saga[saga.name] = s
       
       self.interrogateSagaForAppResources( s )
end

#setBusAttributeIfRequested(saga) ⇒ Object

setBusAttributeIfRequested

Parameters:

  • saga (RServiceBus::Saga)


36
37
38
39
40
41
42
43
# File 'lib/rservicebus/Saga/Manager.rb', line 36

def setBusAttributeIfRequested( saga )
    if defined?( saga.Bus ) then
        saga.Bus = @host
        RServiceBus.log 'Bus attribute set for: ' + saga.class.name
    end
    
    return self
end