Class: RServiceBus2::SagaManager
- Inherits:
-
Object
- Object
- RServiceBus2::SagaManager
- Defined in:
- lib/rservicebus2/saga/manager.rb
Overview
Saga Manager
Instance Method Summary collapse
- #get_methods_by_prefix(saga, prefix) ⇒ Object
- #get_start_with_method_names(saga) ⇒ Object
- #handle(rmsg) ⇒ Object
-
#initialize(host, resource_manager, saga_storage) ⇒ SagaManager
constructor
A new instance of SagaManager.
- #interrogate_saga_for_app_resources(saga) ⇒ Object
- #prep_saga(saga) ⇒ Object
- #process_msg(saga, data, method_name, msg) ⇒ Object
- #register_saga(saga) ⇒ Object
-
#set_bus_attribute_if_requested(saga) ⇒ Object
setBusAttributeIfRequested.
Constructor Details
#initialize(host, resource_manager, saga_storage) ⇒ SagaManager
Returns a new instance of SagaManager.
4 5 6 7 8 9 10 11 12 13 |
# File 'lib/rservicebus2/saga/manager.rb', line 4 def initialize(host, resource_manager, saga_storage) @handler = {} @start_with = {} @saga = {} @host = host @resource_manager = resource_manager @resource_list_by_saga_name = {} @saga_storage = saga_storage end |
Instance Method Details
#get_methods_by_prefix(saga, prefix) ⇒ Object
15 16 17 18 19 20 21 22 23 24 |
# File 'lib/rservicebus2/saga/manager.rb', line 15 def get_methods_by_prefix(saga, prefix) list = [] d_prefix = prefix.downcase saga.instance_methods.each do |name| d_name = name.downcase list.push d_name.to_s.sub(d_prefix, '') if d_name.to_s.slice(0, d_prefix.length) == d_prefix end list.uniq # This takes care of a very rare uppercase / lowercase issue end |
#get_start_with_method_names(saga) ⇒ Object
26 27 28 |
# File 'lib/rservicebus2/saga/manager.rb', line 26 def get_start_with_method_names(saga) get_methods_by_prefix(saga, 'start_with_') end |
#handle(rmsg) ⇒ Object
84 85 86 87 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 |
# File 'lib/rservicebus2/saga/manager.rb', line 84 def handle(rmsg) @resources_used = {} handled = false msg = rmsg.msg msg_class_name = msg.class.name.downcase RServiceBus2.log "SagaManager, started processing, #{msg_class_name}", true if @start_with.key?(msg_class_name) # unless @start_with[msg_class_name].nil? @start_with[msg_class_name].each do |saga| data = SagaData.new(saga) @saga_storage.set(data) method_name = "start_with_#{msg_class_name}" process_msg(saga, data, method_name, msg) handled = true end end return handled if handled == true return false if rmsg.correlation_id.nil? data = @saga_storage.get(rmsg.correlation_id) return false if data.nil? method_name = "handle_#{msg_class_name}" saga = @saga[data.saga_class_name] process_msg(saga, data, method_name, msg) true end |
#interrogate_saga_for_app_resources(saga) ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/rservicebus2/saga/manager.rb', line 41 def interrogate_saga_for_app_resources(saga) RServiceBus2.rlog "Checking app resources for: #{saga.class.name}" RServiceBus2.rlog "If your attribute is not getting set, check that it is in the 'attr_accessor' list" @resource_list_by_saga_name[saga.class.name] = [] @resource_manager.all.each do |k, v| if saga.class.method_defined?(k) @resource_list_by_saga_name[saga.class.name] << k RServiceBus2.log "Resource attribute, #{k}, found for: " + saga.class.name end end self end |
#prep_saga(saga) ⇒ Object
75 76 77 78 79 80 81 82 |
# File 'lib/rservicebus2/saga/manager.rb', line 75 def prep_saga(saga) return if @resource_list_by_saga_name[saga.class.name].nil? @resource_list_by_saga_name[saga.class.name].each do |k, v| saga.instance_variable_set("@#{k}", @resource_manager.get(k).resource) RServiceBus2.rlog "App resource attribute, #{k}, set for: " + saga.class.name end end |
#process_msg(saga, data, method_name, msg) ⇒ Object
117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/rservicebus2/saga/manager.rb', line 117 def process_msg(saga, data, method_name, msg) @host.saga_data = data saga.data = data prep_saga(saga) saga.send(method_name, msg) if saga.class.method_defined?(method_name) @saga_storage.delete(data.correlation_id) if data.finished == true @host.saga_data = nil end |
#register_saga(saga) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/rservicebus2/saga/manager.rb', line 58 def register_saga(saga) s = saga.new set_bus_attribute_if_requested(s) get_start_with_method_names(saga).each do |msg_name| @start_with[msg_name] = [] if @start_with[msg_name].nil? @start_with[msg_name] << s RServiceBus2.log "Registered, #{saga.name}, to start_with, #{msg_name}", true end @saga[saga.name] = s interrogate_saga_for_app_resources(s) end |
#set_bus_attribute_if_requested(saga) ⇒ Object
setBusAttributeIfRequested
32 33 34 35 36 37 38 39 |
# File 'lib/rservicebus2/saga/manager.rb', line 32 def set_bus_attribute_if_requested(saga) if defined?(saga.bus) saga.bus = @host RServiceBus2.log 'Bus attribute set for: ' + saga.class.name end self end |