rservicebus2 0.2.10 → 0.2.15

Sign up to get free protection for your applications and to get access to all the features.
Files changed (40) hide show
  1. checksums.yaml +4 -4
  2. data/lib/rservicebus2.rb +6 -5
  3. data/lib/rservicebus2/appresource/awss3.rb +3 -3
  4. data/lib/rservicebus2/appresource/awssqs.rb +2 -0
  5. data/lib/rservicebus2/appresource/dir.rb +3 -0
  6. data/lib/rservicebus2/appresource/file.rb +2 -0
  7. data/lib/rservicebus2/appresource/fluiddb.rb +3 -0
  8. data/lib/rservicebus2/appresource/fluiddb2.rb +3 -1
  9. data/lib/rservicebus2/config.rb +38 -29
  10. data/lib/rservicebus2/endpointmapping.rb +2 -4
  11. data/lib/rservicebus2/handler_manager.rb +1 -1
  12. data/lib/rservicebus2/helper_functions.rb +13 -12
  13. data/lib/rservicebus2/host.rb +1 -1
  14. data/lib/rservicebus2/message.rb +2 -0
  15. data/lib/rservicebus2/message/statisticoutput.rb +2 -0
  16. data/lib/rservicebus2/message/subscription.rb +2 -0
  17. data/lib/rservicebus2/message/verboseoutput.rb +2 -0
  18. data/lib/rservicebus2/monitor.rb +7 -7
  19. data/lib/rservicebus2/monitor/awss3.rb +3 -1
  20. data/lib/rservicebus2/monitor/awssqs.rb +9 -7
  21. data/lib/rservicebus2/monitor/beanstalk.rb +53 -0
  22. data/lib/rservicebus2/monitor/dir.rb +24 -22
  23. data/lib/rservicebus2/monitor/dirnotifier.rb +4 -1
  24. data/lib/rservicebus2/monitor/message.rb +2 -0
  25. data/lib/rservicebus2/monitor/xmldir.rb +2 -0
  26. data/lib/rservicebus2/monitor_configure.rb +49 -36
  27. data/lib/rservicebus2/mq.rb +27 -30
  28. data/lib/rservicebus2/saga_loader.rb +27 -24
  29. data/lib/rservicebus2/saga_storage.rb +5 -4
  30. data/lib/rservicebus2/sendat_storage/file.rb +0 -1
  31. data/lib/rservicebus2/state_manager.rb +3 -3
  32. data/lib/rservicebus2/state_storage.rb +7 -6
  33. data/lib/rservicebus2/statistic_manager.rb +7 -3
  34. data/lib/rservicebus2/subscription_manager.rb +7 -5
  35. data/lib/rservicebus2/subscription_storage.rb +6 -5
  36. data/lib/rservicebus2/subscription_storage/file.rb +5 -18
  37. data/lib/rservicebus2/subscription_storage_configure.rb +2 -0
  38. data/lib/rservicebus2/transporter.rb +63 -52
  39. metadata +3 -3
  40. data/lib/rservicebus2/stats.rb +0 -68
@@ -0,0 +1,53 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'beanstalk-client'
4
+ require 'cgi'
5
+
6
+ module RServiceBus2
7
+ # Monitor S3 Bucket for objects
8
+ class MonitorBeanstalk < Monitor
9
+ def deduce_timeout(uri)
10
+ return 5 if uri.query.nil?
11
+
12
+ CGI.parse(u.query)['timeout1']&.first || 5
13
+ end
14
+
15
+ # rubocop:disable Metrics/MethodLength
16
+ def connect(uri)
17
+ @uri = uri
18
+ @timeout = deduce_timeout(uri)
19
+
20
+ queue_name = uri.path.sub('/', '')
21
+
22
+ port ||= 11_300
23
+ connection_string = "#{uri.host}:#{port}"
24
+ @beanstalk = Beanstalk::Pool.new([connection_string])
25
+
26
+ @beanstalk.watch(queue_name)
27
+ @message_uri = "beanstalk://#{uri.host}:#{port}/#{queue_name}"
28
+ rescue StandardError => e
29
+ puts "Error connecting to Beanstalk, Host string, #{connection_string}"
30
+ if e.message == 'Beanstalk::NotConnected'
31
+ puts '***Most likely, beanstalk is not running. Start beanstalk, and try running this again.\n' \
32
+ "***If you still get this error, check beanstalk is running at, #{connection_string}"
33
+ abort
34
+ end
35
+
36
+ puts e.message
37
+ puts e.backtrace
38
+ end
39
+ # rubocop:enable Metrics/MethodLength
40
+
41
+ def look
42
+ job = @beanstalk.reserve @timeout
43
+ send(job.body, "#{@message_uri}/#{job.id}")
44
+ job_body = job.body
45
+ job.delete
46
+ job_body
47
+ rescue StandardError => e
48
+ return if e.message == 'TIMED_OUT'
49
+
50
+ raise e
51
+ end
52
+ end
53
+ end
@@ -1,38 +1,39 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'cgi'
2
4
  require 'zip/zip'
3
5
  require 'zlib'
4
6
 
5
7
  module RServiceBus2
6
8
  # Monitor Directory for files
9
+
7
10
  # rubocop:disable Metrics/ClassLength
8
11
  class MonitorDir < Monitor
9
- def connect(uri)
12
+ def input_dir(uri)
10
13
  # Pass the path through the Dir object to check syntax on startup
11
- begin
12
- input_dir = Dir.new(uri.path)
13
- unless File.writable?(uri.path)
14
- puts "***** Directory is not writable, #{uri.path}."
15
- puts "***** Make the directory, #{uri.path}, writable and try again."
16
- abort
17
- end
18
- rescue Errno::ENOENT
19
- puts "***** Directory does not exist, #{uri.path}."
20
- puts "***** Create the directory, #{uri.path}, and try again."
21
- puts "***** eg, mkdir #{uri.path}"
22
- abort
23
- rescue Errno::ENOTDIR
24
- puts "***** The specified path does not point to a directory,
25
- #{uri.path}."
26
- puts "***** Either repoint path to a directory, or remove, #{uri.path},
27
- and create it as a directory."
28
- puts "***** eg, rm #{uri.path} && mkdir #{uri.path}"
29
- abort
30
- end
14
+ return Dir.new(uri.path) if File.writable?(uri.path)
15
+
16
+ puts "***** Directory is not writable, #{uri.path}.\n" \
17
+ "***** Make the directory, #{uri.path}, writable and try again."
18
+ abort
19
+ rescue Errno::ENOENT
20
+ puts "***** Directory does not exist, #{uri.path}.\n" \
21
+ "***** Create the directory, #{uri.path}, and try again.\n" \
22
+ "***** eg, mkdir #{uri.path}"
23
+ abort
24
+ rescue Errno::ENOTDIR
25
+ puts "***** The specified path does not point to a directory, #{uri.path}.\n" \
26
+ "***** Either repoint path to a directory, or remove, #{uri.path}, and create it as a directory.\n" \
27
+ "***** eg, rm #{uri.path} && mkdir #{uri.path}"
28
+ abort
29
+ end
31
30
 
32
- @path = input_dir.path
31
+ def connect(uri)
32
+ @path = input_dir(uri).path
33
33
  @input_filter = []
34
34
 
35
35
  return if uri.query.nil?
36
+
36
37
  parts = CGI.parse(uri.query)
37
38
  @querystringparts = parts
38
39
  if parts.key?('archive')
@@ -141,4 +142,5 @@ module RServiceBus2
141
142
  end
142
143
  end
143
144
  end
145
+ # rubocop:enable Metrics/ClassLength
144
146
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'cgi'
2
4
  require 'fileutils'
3
5
  require 'pathname'
@@ -6,6 +8,7 @@ module RServiceBus2
6
8
  # Monitor for Directory
7
9
  class MonitorDirNotifier < Monitor
8
10
  attr_reader :path, :processing_folder, :filter
11
+
9
12
  def connect(uri)
10
13
  # Pass the path through the Dir object to check syntax on startup
11
14
  begin
@@ -45,7 +48,7 @@ module RServiceBus2
45
48
  begin
46
49
  open_folder processing_uri.path
47
50
  unless File.writable?(processing_uri.path)
48
- puts "***** Processing Directory is not writable,
51
+ puts "***** 1Processing Directory is not writable,
49
52
  #{processing_uri.path}."
50
53
  puts "***** Make the directory, #{processing_uri.path},
51
54
  writable and try again."
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module RServiceBus2
2
4
  # Monitor Message
3
5
  class MonitorMessage
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'rservicebus2/monitor/dir'
2
4
  require 'xmlsimple'
3
5
 
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'rservicebus2/monitor'
2
4
  require 'rservicebus2/monitor/message'
3
5
 
@@ -16,56 +18,67 @@ module RServiceBus2
16
18
  @resource_list = {}
17
19
  end
18
20
 
21
+ def conditionally_set_app_resource(monitor, key, val)
22
+ monitor.instance_variable_set("@#{key}", val.resource)
23
+ @resource_list[monitor.class.name] = [] if @resource_list[monitor.class.name].nil?
24
+ @resource_list[monitor.class.name] << val
25
+ @host.log "App resource attribute, #{key}, set for: #{monitor.class.name}"
26
+ end
27
+
19
28
  # Assigns appropriate resources to writable attributes in the handler that
20
29
  # match keys in the resource hash
21
30
  # @param [RServiceBus2::Handler] handler
22
- def set_app_resources(monitor)
31
+ def conditionally_set_app_resources(monitor)
23
32
  RServiceBus2.rlog "Checking app resources for: #{monitor.class.name}"
24
- RServiceBus2.rlog "If your attribute is not getting set, check that it is
25
- in the 'attr_accessor' list"
33
+ RServiceBus2.rlog "If your attribute is not getting set, check that it is in the 'attr_accessor' list"
26
34
  @resource_manager.all.each do |k, v|
27
35
  next unless monitor.class.method_defined?(k)
28
36
 
29
- monitor.instance_variable_set("@#{k}", v.resource)
30
- @resource_list[monitor.class.name] = [] if
31
- @resource_list[monitor.class.name].nil?
32
- @resource_list[monitor.class.name] << v
33
- @host.log "App resource attribute, #{k}, set for: " +
34
- monitor.class.name
37
+ conditionally_set_app_resource(monitor, k, v)
35
38
  end
36
39
  self
37
40
  end
38
41
 
39
- def get_monitors(env)
40
- monitors = []
42
+ # rubocop:disable Metrics/MethodLength
43
+ def load_monitor(key, val)
44
+ name = key.sub('RSBOB_', '')
45
+ uri = URI.parse(val)
46
+ monitor = nil
47
+ case uri.scheme
48
+ when 'dir'
49
+ require 'rservicebus2/monitor/dir'
50
+ monitor = MonitorDir.new(@host, name, uri)
51
+ when 'awss3'
52
+ require 'rservicebus2/monitor/awss3'
53
+ monitor = MonitorAWSS3.new(@host, name, uri)
54
+ when 'awssqs'
55
+ require 'rservicebus2/monitor/awssqs'
56
+ monitor = MonitorAWSSQS.new(@host, name, uri)
57
+ when 'dirnotifier'
58
+ require 'rservicebus2/monitor/dirnotifier'
59
+ monitor = MonitorDirNotifier.new(@host, name, uri)
60
+ when 'beanstalk'
61
+ require 'rservicebus2/monitor/beanstalk'
62
+ monitor = MonitorBeanstalk.new(@host, name, uri)
63
+ else
64
+ abort("Scheme, #{uri.scheme}, not recognised when configuring
65
+ Monitor, #{key}=#{val}")
66
+ end
67
+ monitor
68
+ end
69
+ # rubocop:enable Metrics/MethodLength
70
+
71
+ def monitors(env)
72
+ list = []
41
73
 
42
74
  env.each do |k, v|
43
- if v.is_a?(String) && k.start_with?('RSBOB_')
44
- uri = URI.parse(v)
45
- name = k.sub('RSBOB_', '')
46
- monitor = nil
47
- case uri.scheme
48
- when 'dir'
49
- require 'rservicebus2/monitor/dir'
50
- monitor = MonitorDir.new(@host, name, uri)
51
- when 'awss3'
52
- require 'rservicebus2/monitor/awss3'
53
- monitor = MonitorAWSS3.new(@host, name, uri)
54
- when 'awssqs'
55
- require 'rservicebus2/monitor/awssqs'
56
- monitor = MonitorAWSSQS.new(@host, name, uri)
57
- when 'dirnotifier'
58
- require 'rservicebus2/monitor/dirnotifier'
59
- monitor = MonitorDirNotifier.new(@host, name, uri)
60
- else
61
- abort("Scheme, #{uri.scheme}, not recognised when configuring
62
- Monitor, #{k}=#{v}")
63
- end
64
- set_app_resources(monitor)
65
- monitors << monitor
66
- end
75
+ next unless v.is_a?(String) && k.start_with?('RSBOB_')
76
+
77
+ monitor = load_monitor(k, v)
78
+ conditionally_set_app_resources(monitor)
79
+ list << monitor
67
80
  end
68
- monitors
81
+ list
69
82
  end
70
83
  end
71
84
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'uri'
2
4
 
3
5
  module RServiceBus2
@@ -10,6 +12,7 @@ module RServiceBus2
10
12
  class MQ
11
13
  attr_reader :local_queue_name
12
14
 
15
+ # rubocop:disable Metrics/MethodLength
13
16
  def self.get
14
17
  mq_string = RServiceBus2.get_value('RSBMQ', 'beanstalk://localhost')
15
18
  uri = URI.parse(mq_string)
@@ -34,31 +37,26 @@ module RServiceBus2
34
37
 
35
38
  mq
36
39
  end
40
+ # rubocop:enable Metrics/MethodLength
37
41
 
38
42
  # Resources are attached, and are be specified using the URI syntax
39
43
  # @param [URI] uri the type and location of queue,
40
44
  # eg beanstalk://127.0.0.1/foo
41
45
  # @param [Integer] timeout the amount of time to wait for a msg to arrive
46
+
47
+ # rubocop:disable Metrics/MethodLength
42
48
  def initialize(uri)
43
- if uri.is_a? URI
44
- @uri = uri
45
- else
46
- puts 'uri must be a valid URI'
47
- abort
48
- end
49
+ abort 'Paramter to mq must be a valid URI' unless uri.is_a? URI
49
50
 
50
- if uri.path == '' || uri.path == '/'
51
- @local_queue_name = RServiceBus2.get_value('APPNAME', 'RServiceBus')
52
- else
53
- @local_queue_name = uri.path
54
- @local_queue_name[0] = ''
55
- end
51
+ @uri = uri
52
+ @local_queue_name = uri.path
53
+ @local_queue_name[0] = '' if @local_queue_name[0] == '/'
54
+ @local_queue_name = RServiceBus2.get_value('APPNAME', 'RServiceBus') if @local_queue_name == ''
56
55
 
57
56
  if @local_queue_name == ''
58
- puts "@local_queue_name: #{@local_queue_name}"
59
- puts 'Queue name must be supplied '
60
- puts "*** uri, #{uri}, needs to contain a queue name"
61
- puts '*** the structure is scheme://host[:port]/queuename'
57
+ puts 'Queue name must be supplied ' \
58
+ "*** uri, #{uri}, needs to contain a queue name" \
59
+ '*** the structure is scheme://host[:port]/queuename'
62
60
  abort
63
61
  end
64
62
 
@@ -66,42 +64,41 @@ module RServiceBus2
66
64
  connect(uri.host, uri.port)
67
65
  subscribe(@local_queue_name)
68
66
  end
67
+ # rubocop:enable Metrics/MethodLength
69
68
 
70
69
  # Connect to the broker
71
70
  # @param [String] host machine runnig the mq
72
71
  # @param [String] port port the mq is running on
73
72
  def connect(_host, _port)
74
- fail 'Method, connect, needs to be implemented'
73
+ raise 'Method, connect, needs to be implemented'
75
74
  end
76
75
 
77
76
  # Connect to the receiving queue
78
77
  # @param [String] queuename name of the receiving queue
79
78
  def subscribe(_queuename)
80
- fail 'Method, subscribe, needs to be implemented'
79
+ raise 'Method, subscribe, needs to be implemented'
81
80
  end
82
81
 
83
82
  # Get next msg from queue
84
83
  def pop
85
- fail 'Method, pop, needs to be implemented'
84
+ raise 'Method, pop, needs to be implemented'
86
85
  end
87
86
 
88
87
  # "Commit" the pop
89
88
  def ack
90
- fail 'Method, ack, needs to be implemented'
89
+ raise 'Method, ack, needs to be implemented'
91
90
  end
92
91
 
93
92
  # At least called in the Host rescue block, to ensure all network links are
94
93
  # healthy
95
94
  # @param [String] queue_name name of the queue to which the msg should be sent
96
95
  # @param [String] msg msg to be sent
97
- def send(queue_name, msg)
98
- begin
99
- @connection.close
100
- rescue
101
- puts 'AppResource. An error was raised while closing connection to, ' + @uri.to_s
102
- end
103
-
104
- end
105
-
106
- end
96
+ def send(_queue_name, _msg)
97
+ @connection.close
98
+ rescue StandardError => e
99
+ puts "AppResource. An error was raised while closing connection to, #{@uri}"
100
+ puts "Error: #{e.message}"
101
+ puts "Backtrace: #{e.backtrace}"
102
+ end
103
+ end
107
104
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module RServiceBus2
2
4
  # Given a directory, this class is responsible loading Sagas
3
5
  class SagaLoader
@@ -17,11 +19,11 @@ module RServiceBus2
17
19
  # require statement.
18
20
  # @param [String] file_path the path to be cleaned
19
21
  def get_require_path(file_path)
20
- file_path = './' + file_path unless file_path.start_with?('/')
22
+ file_path = "./#{file_path}" unless file_path.start_with?('/')
21
23
 
22
24
  return file_path.sub('.rb', '') if File.exist?(file_path)
23
25
 
24
- abort('Filepath, ' + file_path + ", given for Saga require doesn't exist")
26
+ abort("Filepath, #{file_path}, given for Saga require doesn't exist")
25
27
  end
26
28
 
27
29
  # Instantiate the saga named in sagaName from the file name in file_path
@@ -32,6 +34,8 @@ module RServiceBus2
32
34
  # @param [String] sagaName name of the saga to instantiate
33
35
  # @param [String] file_path the path to the file to be loaded
34
36
  # @return [RServiceBus2::Saga] the loader
37
+
38
+ # rubocop:disable Metrics/MethodLength
35
39
  def load_saga_from_file(saga_name, file_path)
36
40
  require_path = get_require_path(file_path)
37
41
 
@@ -39,52 +43,51 @@ module RServiceBus2
39
43
  begin
40
44
  saga = Object.const_get(saga_name)
41
45
  rescue StandardError => e
42
- puts 'Expected class name: ' + saga_name + ', not found after require:
43
- ' + require_path
44
- puts '**** Check in ' + file_path + ' that the class is named : ' +
45
- saga_name
46
- puts '( In case its not that )'
46
+ puts "Expected class name: #{saga_name}, not found after require: #{require_path}" \
47
+ "**** Check in #{file_path} that the class is named: #{saga_name}" \
48
+ '( In case its not that )'
47
49
  raise e
48
50
  end
49
51
  saga
50
52
  end
53
+ # rubocop:enable Metrics/MethodLength
51
54
 
52
55
  # Wrapper function
53
56
  # @param [String] file_path
54
57
  # @param [String] sagaName
55
58
  # @returns [RServiceBus2::Saga] saga
59
+
60
+ # rubocop:disable Metrics/MethodLength
56
61
  def load_saga(file_path, saga_name)
57
62
  if @list_of_loaded_paths.key?(file_path)
58
63
  RServiceBus2.log "Not reloading, #{file_path}"
59
64
  return
60
65
  end
61
66
 
62
- begin
63
- RServiceBus2.rlog 'file_path: ' + file_path
64
- RServiceBus2.rlog 'saga_name: ' + saga_name
67
+ RServiceBus2.rlog "file_path: #{file_path}"
68
+ RServiceBus2.rlog "saga_name: #{saga_name}"
65
69
 
66
- saga = load_saga_from_file(saga_name, file_path)
67
- RServiceBus2.log 'Loaded Saga: ' + saga_name
70
+ saga = load_saga_from_file(saga_name, file_path)
71
+ RServiceBus2.log "Loaded Saga: #{saga_name}"
68
72
 
69
- @saga_manager.register_saga(saga)
73
+ @saga_manager.register_saga(saga)
70
74
 
71
- @list_of_loaded_paths[file_path] = 1
72
- rescue StandardError => e
73
- puts 'Exception loading saga from file: ' + file_path
74
- puts e.message
75
- puts e.backtrace[0]
76
- abort
77
- end
75
+ @list_of_loaded_paths[file_path] = 1
76
+ rescue StandardError => e
77
+ puts "Exception loading saga from file: #{file_path}"
78
+ puts e.message
79
+ puts e.backtrace[0]
80
+ abort
78
81
  end
82
+ # rubocop:enable Metrics/MethodLength
79
83
 
80
84
  # This method is overloaded for unit tests
81
85
  # @param [String] path directory to check
82
86
  # @return [Array] a list of paths to files found in the given path
83
87
  def get_list_of_files_for_dir(path)
84
- list = Dir[path + '/*']
88
+ list = Dir["#{path}/*"]
85
89
 
86
- RServiceBus2.rlog "SagaLoader.getListOfFilesForDir. path: #{path},
87
- list: #{list}"
90
+ RServiceBus2.rlog "SagaLoader.getListOfFilesForDir. path: #{path}, list: #{list}"
88
91
 
89
92
  list
90
93
  end
@@ -97,7 +100,7 @@ module RServiceBus2
97
100
 
98
101
  saga_name = base_name.sub(ext_name, '')
99
102
 
100
- "saga_#{saga_name}".gsub(/(?<=_|^)(\w)/){$1.upcase}.gsub(/(?:_)(\w)/,'\1')
103
+ "saga_#{saga_name}".gsub(/(?<=_|^)(\w)/) { Regexp.last_match(1).upcase }.gsub(/(?:_)(\w)/, '\1')
101
104
  end
102
105
 
103
106
  # Entry point for loading Sagas