rservicebus2 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (67) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE +64 -0
  3. data/bin/return_messages_to_source_queue +114 -0
  4. data/bin/rsb_ctl +38 -0
  5. data/bin/rservicebus2 +14 -0
  6. data/bin/rservicebus2-create +107 -0
  7. data/bin/rservicebus2-init +104 -0
  8. data/bin/rservicebus2-transport +16 -0
  9. data/bin/send_empty_message +15 -0
  10. data/lib/rservicebus.rb +59 -0
  11. data/lib/rservicebus/agent.rb +54 -0
  12. data/lib/rservicebus/appresource.rb +65 -0
  13. data/lib/rservicebus/appresource/dir.rb +29 -0
  14. data/lib/rservicebus/appresource/file.rb +8 -0
  15. data/lib/rservicebus/appresource/fluiddb.rb +24 -0
  16. data/lib/rservicebus/appresource_configure.rb +33 -0
  17. data/lib/rservicebus/audit.rb +28 -0
  18. data/lib/rservicebus/circuitbreaker.rb +79 -0
  19. data/lib/rservicebus/config.rb +168 -0
  20. data/lib/rservicebus/cron_manager.rb +76 -0
  21. data/lib/rservicebus/endpointmapping.rb +72 -0
  22. data/lib/rservicebus/errormessage.rb +14 -0
  23. data/lib/rservicebus/handler_loader.rb +162 -0
  24. data/lib/rservicebus/handler_manager.rb +131 -0
  25. data/lib/rservicebus/helper_functions.rb +85 -0
  26. data/lib/rservicebus/host.rb +487 -0
  27. data/lib/rservicebus/message.rb +78 -0
  28. data/lib/rservicebus/message/statisticoutput.rb +7 -0
  29. data/lib/rservicebus/message/subscription.rb +10 -0
  30. data/lib/rservicebus/message/verboseoutput.rb +7 -0
  31. data/lib/rservicebus/monitor.rb +61 -0
  32. data/lib/rservicebus/monitor/csvdir.rb +52 -0
  33. data/lib/rservicebus/monitor/dir.rb +139 -0
  34. data/lib/rservicebus/monitor/dirnotifier.rb +101 -0
  35. data/lib/rservicebus/monitor/message.rb +11 -0
  36. data/lib/rservicebus/monitor/xmldir.rb +11 -0
  37. data/lib/rservicebus/monitor_configure.rb +71 -0
  38. data/lib/rservicebus/mq.rb +98 -0
  39. data/lib/rservicebus/mq/beanstalk.rb +72 -0
  40. data/lib/rservicebus/resource_manager.rb +69 -0
  41. data/lib/rservicebus/saga/base.rb +17 -0
  42. data/lib/rservicebus/saga/data.rb +20 -0
  43. data/lib/rservicebus/saga/manager.rb +128 -0
  44. data/lib/rservicebus/saga_loader.rb +118 -0
  45. data/lib/rservicebus/saga_storage.rb +18 -0
  46. data/lib/rservicebus/saga_storage/dir.rb +87 -0
  47. data/lib/rservicebus/saga_storage/inmemory.rb +37 -0
  48. data/lib/rservicebus/sendat_manager.rb +33 -0
  49. data/lib/rservicebus/sendat_storage.rb +20 -0
  50. data/lib/rservicebus/sendat_storage/file.rb +37 -0
  51. data/lib/rservicebus/sendat_storage/inmemory.rb +20 -0
  52. data/lib/rservicebus/state_manager.rb +30 -0
  53. data/lib/rservicebus/state_storage.rb +18 -0
  54. data/lib/rservicebus/state_storage/dir.rb +66 -0
  55. data/lib/rservicebus/state_storage/inmemory.rb +25 -0
  56. data/lib/rservicebus/statistic_manager.rb +86 -0
  57. data/lib/rservicebus/stats.rb +68 -0
  58. data/lib/rservicebus/subscription_manager.rb +31 -0
  59. data/lib/rservicebus/subscription_storage.rb +39 -0
  60. data/lib/rservicebus/subscription_storage/file.rb +42 -0
  61. data/lib/rservicebus/subscription_storage/redis.rb +69 -0
  62. data/lib/rservicebus/subscription_storage_configure.rb +19 -0
  63. data/lib/rservicebus/test.rb +2 -0
  64. data/lib/rservicebus/test/bus.rb +32 -0
  65. data/lib/rservicebus/transporter.rb +142 -0
  66. data/lib/rservicebus/usermessage/withpayload.rb +10 -0
  67. metadata +184 -0
@@ -0,0 +1,76 @@
1
+ require 'parse-cron'
2
+
3
+ module RServiceBus
4
+ # Globber
5
+ class Globber
6
+ def self.parse_to_regex(str)
7
+ escaped = Regexp.escape(str).gsub('\*', '.*?')
8
+ Regexp.new "^#{escaped}$", Regexp::IGNORECASE
9
+ end
10
+
11
+ def initialize(str)
12
+ @regex = self.class.parse_to_regex str
13
+ end
14
+
15
+ def =~(str)
16
+ !!(str =~ @regex)
17
+ end
18
+ end
19
+
20
+ class NoMatchingMsgForCron < StandardError
21
+ end
22
+
23
+ # Cron Manager
24
+ class CronManager
25
+ def get_matching_msg_names(name)
26
+ list = []
27
+ @msg_names.each do |n|
28
+ list << n if Globber.new(name) =~ n
29
+ end
30
+ fail NoMatchingMsgForCron, name if list.length == 0
31
+ list
32
+ end
33
+
34
+ def add_cron(name, cron_string)
35
+ get_matching_msg_names(name).each do |n|
36
+ hash = {}
37
+ hash['name'] = n
38
+ hash['last'] = Time.now
39
+ hash['v'] = cron_string
40
+ hash['cron'] = CronParser.new(cron_string)
41
+ hash['next'] = hash['cron'].next(Time.now)
42
+ @list << hash
43
+ @bus.log("Cron set for, #{n}, #{cron_string}, next run, #{hash['next']}")
44
+ end
45
+ end
46
+
47
+ def initialize(host, msg_names = [])
48
+ @bus = host
49
+ @msg_names = msg_names
50
+
51
+ RServiceBus.rlog 'Load Cron'
52
+ @list = []
53
+ ENV.each do |k, vs|
54
+ if k.start_with?('RSBCRON_')
55
+ add_cron(k.sub('RSBCRON_', ''), vs)
56
+ elsif k.start_with?('RSBCRON')
57
+ vs.split(';').each do |v|
58
+ parts = v.split(' ', 6)
59
+ add_cron(parts.pop, parts.join(' '))
60
+ end
61
+ end
62
+ end
63
+ end
64
+
65
+ def run
66
+ now = Time.now
67
+ @list.each_with_index do |v, idx|
68
+ next if now <= v['next']
69
+
70
+ RServiceBus.rlog "CronManager.Send, #{v['name']}"
71
+ @bus.send(RServiceBus.create_anonymous_class(v['name']))
72
+ @list[idx]['next'] = v['cron'].next(now)
73
+ end
74
+ end
75
+ end
76
+ end
@@ -0,0 +1,72 @@
1
+ module RServiceBus
2
+ # Marshals data for message end points
3
+ # Expected format: <msg mame 1>:<end point 1>;<msg mame 2>:<end point 2>
4
+ class EndpointMapping
5
+ def get_value(name)
6
+ RServiceBus.get_value(name)
7
+ end
8
+
9
+ def log(string, _ver = false)
10
+ RServiceBus.log(string)
11
+ end
12
+
13
+ def configure_mapping(mapping)
14
+ match = mapping.match(/(.+):(.+)/)
15
+ if match.nil?
16
+ log 'Mapping string provided is invalid'
17
+ log "The entire mapping string is: #{mapping}"
18
+ log "*** Could not find ':' in mapping entry, #{line}"
19
+ exit
20
+ end
21
+
22
+ RServiceBus.rlog "EndpointMapping.configureMapping: #{match[1]}, #{match[2]}"
23
+ @endpoints[match[1]] = match[2]
24
+
25
+ @queue_name_list.each do |q|
26
+ if q != match[2] && q.downcase == match[2].downcase
27
+ log('*** Two queues specified with only case sensitive difference.')
28
+ log("*** #{q} != #{match[2]}")
29
+ log('*** If you meant these queues to be the same, please match case
30
+ and restart the bus.')
31
+ end
32
+ end
33
+ @queue_name_list << match[2]
34
+ end
35
+
36
+ def configure(local_queue_name=nil)
37
+ log('EndpointMapping.Configure')
38
+
39
+ @queue_name_list = []
40
+ @queue_name_list << local_queue_name unless local_queue_name.nil?
41
+
42
+ unless get_value('MESSAGE_ENDPOINT_MAPPING').nil?
43
+ log('*** MESSAGE_ENDPOINT_MAPPING environment variable was detected')
44
+ log("*** You may have intended MESSAGE_ENDPOINT_MAPPINGS, note the 'S'
45
+ on the end")
46
+ end
47
+
48
+ mappings = get_value('MESSAGE_ENDPOINT_MAPPINGS')
49
+ return self if mappings.nil?
50
+
51
+ mappings.split(';').each do |mapping|
52
+ configure_mapping(mapping)
53
+ end
54
+
55
+ self
56
+ end
57
+
58
+ def initialize
59
+ @endpoints = {}
60
+ end
61
+
62
+ def get(msg_name)
63
+ return @endpoints[msg_name] if @endpoints.key?(msg_name)
64
+
65
+ nil
66
+ end
67
+
68
+ def get_subscription_endpoints
69
+ @endpoints.keys.select { |el| el.end_with?('Event') }
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,14 @@
1
+ module RServiceBus
2
+
3
+ # Error Message
4
+ class ErrorMessage
5
+ attr_reader :occurredat, :source_queue, :error_msg
6
+
7
+ def initialize(source_queue, error_msg)
8
+ @occurredat = DateTime.now
9
+
10
+ @source_queue = source_queue
11
+ @error_msg = error_msg
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,162 @@
1
+ module RServiceBus
2
+ # Given a directory, this class is responsible for finding
3
+ # msgnames,
4
+ # handlernames, and
5
+ # loading handlers
6
+ class HandlerLoader
7
+ attr_reader :handlerList
8
+
9
+ # Constructor
10
+ #
11
+ # @param [RServiceBus::Host] host instance
12
+ # @param [Hash] appResources As hash[k,v] where k is the name of a
13
+ # resource, and v is the resource
14
+ def initialize(host, handler_manager)
15
+ @host = host
16
+
17
+ @handler_manager = handler_manager
18
+
19
+ @list_of_loaded_paths = {}
20
+ end
21
+
22
+ # Cleans the given path to ensure it can be used for as a parameter for the require statement.
23
+ # @param [String] file_path the path to be cleaned
24
+ def get_require_path(file_path)
25
+ file_path = './' + file_path unless file_path.start_with?('/')
26
+
27
+ return file_path.sub('.rb', '') if File.exist?(file_path)
28
+
29
+ abort('Filepath, ' + file_path + ", given for MessageHandler require
30
+ doesn't exist")
31
+ end
32
+
33
+ # Instantiate the handler named in handlerName from the file name in
34
+ # file_path. Exceptions will be raised if encountered when loading handlers.
35
+ # This is a load time activity, so handlers should load correctly. As much
36
+ # information as possible is returned to enable the handler to be fixed,
37
+ # or configuration corrected.
38
+ # @param [String] handler_name name of the handler to instantiate
39
+ # @param [String] file_path the path to the file to be loaded
40
+ # @return [RServiceBus::Handler] the loader
41
+ def load_handler_from_file(handler_name, file_path)
42
+ require_path = get_require_path(file_path)
43
+
44
+ require require_path
45
+ begin
46
+ handler = Object.const_get(handler_name).new
47
+ rescue StandardError => e
48
+ puts 'Expected class name: ' + handler_name + ', not found after
49
+ require: ' + require_path
50
+ puts '**** Check in ' + file_path + ' that the class is named : ' +
51
+ handler_name
52
+ puts '( In case its not that )'
53
+ raise e
54
+ end
55
+
56
+ handler
57
+ end
58
+
59
+ # Wrapper function
60
+ #
61
+ # @param [String] file_path
62
+ # @param [String] handler_name
63
+ # @returns [RServiceBus::Handler] handler
64
+ def load_handler(msg_name, file_path, handler_name)
65
+ if @list_of_loaded_paths.key?(file_path)
66
+ RServiceBus.log "Not reloading, #{file_path}"
67
+ return
68
+ end
69
+
70
+ begin
71
+ RServiceBus.rlog 'file_path: ' + file_path
72
+ RServiceBus.rlog 'handler_name: ' + handler_name
73
+
74
+ handler = load_handler_from_file(handler_name, file_path)
75
+ RServiceBus.log 'Loaded Handler: ' + handler_name
76
+
77
+ @handler_manager.add_handler(msg_name, handler)
78
+
79
+ @list_of_loaded_paths[file_path] = 1
80
+ rescue StandardError => e
81
+ puts 'Exception loading handler from file: ' + file_path
82
+ puts e.message
83
+ puts e.backtrace[0]
84
+ abort
85
+ end
86
+ end
87
+
88
+ # This method is overloaded for unit tests
89
+ #
90
+ # @param [String] path directory to check
91
+ # @return [Array] a list of paths to files found in the given path
92
+ def get_list_of_files_for_dir(path)
93
+ list = Dir[path + '/*']
94
+ RServiceBus.rlog "HandlerLoader.getListOfFilesForDir. path: #{path},
95
+ list: #{list}"
96
+ list
97
+ end
98
+
99
+ # Multiple handlers for the same msg can be placed inside a top level
100
+ # directory. The msg name is than taken from the directory, and the
101
+ # handlers from the files inside that directory
102
+ #
103
+ # @param [String] msg_name name of message
104
+ # @param [String] base_dir directory to check for handlers of [msg_name]
105
+ def load_handlers_from_second_level_path(msg_name, base_dir)
106
+ get_list_of_files_for_dir(base_dir).each do |file_path|
107
+ next if file_path.end_with?('.')
108
+
109
+ ext_name = File.extname(file_path)
110
+ if !File.directory?(file_path) && ext_name == '.rb'
111
+ file_name = File.basename(file_path).sub('.rb', '')
112
+ handler_name = "MessageHandler_#{msg_name}_#{file_name}"
113
+
114
+ load_handler(msg_name, file_path, handler_name)
115
+ end
116
+ end
117
+
118
+ self
119
+ end
120
+
121
+ # Extract the top level dir or file name as it is the msg name
122
+ #
123
+ # @param [String] file_path path to check - this can be a directory or file
124
+ def get_msg_name(file_path)
125
+ base_name = File.basename(file_path)
126
+ ext_name = File.extname(base_name)
127
+ file_name = base_name.sub(ext_name, '')
128
+
129
+ msg_name = file_name
130
+ end
131
+
132
+ # Load top level handlers from the given directory
133
+ #
134
+ # @param [String] baseDir directory to check - should not have trailing slash
135
+ def load_handlers_from_top_level_path(base_dir)
136
+ RServiceBus.rlog "HandlerLoader.loadHandlersFromTopLevelPath. baseDir: #{base_dir}"
137
+ get_list_of_files_for_dir(base_dir).each do |file_path|
138
+ unless file_path.end_with?('.')
139
+ msg_name = get_msg_name(file_path)
140
+ if File.directory?(file_path)
141
+ load_handlers_from_second_level_path(msg_name, file_path)
142
+ else
143
+ handler_name = "MessageHandler_#{msg_name}"
144
+ load_handler(msg_name, file_path, handler_name)
145
+ end
146
+ end
147
+ end
148
+
149
+ self
150
+ end
151
+
152
+ # Entry point for loading handlers
153
+ #
154
+ # @param [String] baseDir directory to check - should not have trailing
155
+ # slash
156
+ def load_handlers_from_path(base_dir)
157
+ load_handlers_from_top_level_path(base_dir)
158
+
159
+ self
160
+ end
161
+ end
162
+ end
@@ -0,0 +1,131 @@
1
+ module RServiceBus
2
+ # Given a directory, this class is responsible for finding
3
+ # msgnames,
4
+ # handlernames, and
5
+ # loading handlers
6
+ class HandlerManager
7
+ # Constructor
8
+ #
9
+ # @param [RServiceBus::Host] host instance
10
+ # @param [Hash] app_resources As hash[k,v] where k is the name of a resource, and v is the resource
11
+ def initialize(host, resource_manager, state_manager)
12
+ @host = host
13
+ @resource_manager = resource_manager
14
+ @state_manager = state_manager
15
+
16
+ @handler_list = {}
17
+ @resource_list_by_handler_name = {}
18
+ end
19
+
20
+ # setBusAttributeIfRequested
21
+ #
22
+ # @param [RServiceBus::Handler] handler
23
+ def set_bus_attribute_if_requested(handler)
24
+ if defined?(handler.bus)
25
+ handler.bus = @host
26
+ RServiceBus.log 'Bus attribute set for: ' + handler.class.name
27
+ end
28
+
29
+ self
30
+ end
31
+
32
+ # setStateAttributeIfRequested
33
+ #
34
+ # @param [RServiceBus::Handler] handler
35
+ def set_state_attribute_if_requested(handler)
36
+ if defined?(handler.State)
37
+ handler.State = @state_manager.get(handler)
38
+ RServiceBus.log 'Bus attribute set for: ' + handler.class.name
39
+ end
40
+
41
+ self
42
+ end
43
+
44
+ # checkIfStateAttributeRequested
45
+ #
46
+ # @param [RServiceBus::Handler] handler
47
+ def check_if_state_attribute_requested(handler)
48
+ @state_manager.Required if defined?(handler.state)
49
+
50
+ self
51
+ end
52
+
53
+ def interrogate_handler_for_app_resources(handler)
54
+ RServiceBus.rlog "Checking app resources for: #{handler.class.name}"
55
+ RServiceBus.rlog "If your attribute is not getting set, check that it is in the 'attr_accessor' list"
56
+
57
+ @resource_list_by_handler_name[handler.class.name] = []
58
+ @resource_manager.get_all.each do |k, v|
59
+ next unless handler.class.method_defined?(k)
60
+
61
+ @resource_list_by_handler_name[handler.class.name] << k
62
+ RServiceBus.log "Resource attribute, #{k}, found for: " +
63
+ handler.class.name
64
+ end
65
+
66
+ self
67
+ end
68
+
69
+ def add_handler(msg_name, handler)
70
+ @handler_list[msg_name] = [] if @handler_list[msg_name].nil?
71
+ return unless @handler_list[msg_name].index{ |x| x.class.name == handler.class.name }.nil?
72
+
73
+ @handler_list[msg_name] << handler
74
+ set_bus_attribute_if_requested(handler)
75
+ check_if_state_attribute_requested(handler)
76
+ interrogate_handler_for_app_resources(handler)
77
+ end
78
+
79
+ # As named
80
+ #
81
+ # @param [String] msgName
82
+ # @param [Array] appResources A list of appResource
83
+ def get_list_of_resources_needed_to_process_msg(msg_name)
84
+ return [] if @handler_list[msg_name].nil?
85
+
86
+ list = []
87
+ @handler_list[msg_name].each do |handler|
88
+ list = list + @resource_list_by_handler_name[handler.class.name] unless @resource_list_by_handler_name[handler.class.name].nil?
89
+ end
90
+ list.uniq!
91
+ end
92
+
93
+ def set_resources_for_handlers_needed_to_process_msg(msg_name)
94
+ @handler_list[msg_name].each do |handler|
95
+ set_state_attribute_if_requested(handler)
96
+
97
+ next if @resource_list_by_handler_name[handler.class.name].nil?
98
+ @resource_list_by_handler_name[handler.class.name].each do |k|
99
+ handler.instance_variable_set("@#{k}", @resource_manager.get(k).get_resource)
100
+ RServiceBus.rlog "App resource attribute, #{k}, set for: " + handler.class.name
101
+ end
102
+ end
103
+ end
104
+
105
+ def get_handler_list_for_msg(msg_name)
106
+ return [] if @handler_list[msg_name].nil?
107
+
108
+ list = get_list_of_resources_needed_to_process_msg(msg_name)
109
+ set_resources_for_handlers_needed_to_process_msg(msg_name)
110
+
111
+ @handler_list[msg_name]
112
+ end
113
+
114
+ def can_msg_be_handled_locally(msg_name)
115
+ @handler_list.key?(msg_name)
116
+ end
117
+
118
+ def get_stats
119
+ list = []
120
+ @handler_list.each do |k, v|
121
+ list << v.inspect
122
+ end
123
+
124
+ list
125
+ end
126
+
127
+ def get_list_of_msg_names
128
+ @handler_list.keys
129
+ end
130
+ end
131
+ end