rservicebus2 0.2.9 → 0.2.14

Sign up to get free protection for your applications and to get access to all the features.
Files changed (47) hide show
  1. checksums.yaml +4 -4
  2. data/lib/rservicebus2.rb +6 -5
  3. data/lib/rservicebus2/agent.rb +2 -2
  4. data/lib/rservicebus2/appresource/awss3.rb +3 -3
  5. data/lib/rservicebus2/appresource/awssqs.rb +2 -0
  6. data/lib/rservicebus2/appresource/dir.rb +3 -0
  7. data/lib/rservicebus2/appresource/file.rb +2 -0
  8. data/lib/rservicebus2/appresource/fluiddb.rb +3 -0
  9. data/lib/rservicebus2/appresource/fluiddb2.rb +3 -1
  10. data/lib/rservicebus2/config.rb +38 -29
  11. data/lib/rservicebus2/endpointmapping.rb +2 -4
  12. data/lib/rservicebus2/errormessage.rb +1 -1
  13. data/lib/rservicebus2/handler_manager.rb +1 -1
  14. data/lib/rservicebus2/helper_functions.rb +13 -12
  15. data/lib/rservicebus2/host.rb +5 -5
  16. data/lib/rservicebus2/message.rb +11 -19
  17. data/lib/rservicebus2/message/statisticoutput.rb +2 -0
  18. data/lib/rservicebus2/message/subscription.rb +2 -0
  19. data/lib/rservicebus2/message/verboseoutput.rb +2 -0
  20. data/lib/rservicebus2/monitor.rb +7 -7
  21. data/lib/rservicebus2/monitor/awss3.rb +3 -1
  22. data/lib/rservicebus2/monitor/awssqs.rb +9 -7
  23. data/lib/rservicebus2/monitor/beanstalk.rb +53 -0
  24. data/lib/rservicebus2/monitor/dir.rb +25 -23
  25. data/lib/rservicebus2/monitor/dirnotifier.rb +4 -1
  26. data/lib/rservicebus2/monitor/message.rb +2 -0
  27. data/lib/rservicebus2/monitor/xmldir.rb +2 -0
  28. data/lib/rservicebus2/monitor_configure.rb +49 -36
  29. data/lib/rservicebus2/mq.rb +27 -30
  30. data/lib/rservicebus2/mq/beanstalk.rb +5 -1
  31. data/lib/rservicebus2/saga/data.rb +1 -1
  32. data/lib/rservicebus2/saga_loader.rb +27 -24
  33. data/lib/rservicebus2/saga_storage.rb +5 -4
  34. data/lib/rservicebus2/sendat_manager.rb +5 -3
  35. data/lib/rservicebus2/sendat_storage.rb +5 -3
  36. data/lib/rservicebus2/sendat_storage/file.rb +6 -2
  37. data/lib/rservicebus2/sendat_storage/inmemory.rb +2 -0
  38. data/lib/rservicebus2/state_manager.rb +5 -4
  39. data/lib/rservicebus2/state_storage.rb +7 -6
  40. data/lib/rservicebus2/statistic_manager.rb +7 -3
  41. data/lib/rservicebus2/subscription_manager.rb +7 -5
  42. data/lib/rservicebus2/subscription_storage.rb +6 -5
  43. data/lib/rservicebus2/subscription_storage/file.rb +5 -18
  44. data/lib/rservicebus2/subscription_storage_configure.rb +2 -0
  45. data/lib/rservicebus2/transporter.rb +63 -52
  46. metadata +3 -3
  47. data/lib/rservicebus2/stats.rb +0 -68
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'beanstalk-client'
2
4
  require 'rservicebus2/mq'
3
5
 
@@ -34,6 +36,7 @@ module RServiceBus2
34
36
  abort
35
37
  end
36
38
  end
39
+ # rubocop:enable Metrics/AbcSize,Metrics/MethodLength
37
40
 
38
41
  def subscribe(queuename)
39
42
  @beanstalk.watch(queuename)
@@ -45,6 +48,7 @@ module RServiceBus2
45
48
  @job = @beanstalk.reserve @timeout
46
49
  rescue StandardError => e
47
50
  raise NoMsgToProcess if e.message == 'TIMED_OUT'
51
+
48
52
  raise e
49
53
  end
50
54
  @job.body
@@ -63,7 +67,7 @@ module RServiceBus2
63
67
  if msg.length > @max_job_size
64
68
  puts '***Attempting to send a msg which will not fit on queue.'
65
69
  puts "***Msg size, #{msg.length}, max msg size, #{@max_job_size}."
66
- fail JobTooBigError, "Msg size, #{msg.length}, max msg size,
70
+ raise JobTooBigError, "Msg size, #{msg.length}, max msg size,
67
71
  #{@max_job_size}"
68
72
  end
69
73
  @beanstalk.use(queue_name)
@@ -5,7 +5,7 @@ module RServiceBus2
5
5
  attr_accessor :finished
6
6
 
7
7
  def initialize(saga)
8
- @createdat = DateTime.now
8
+ @createdat = Time.now
9
9
  @correlation_id = UUIDTools::UUID.random_create
10
10
  @saga_class_name = saga.class.name
11
11
  @finished = false
@@ -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
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module RServiceBus2
2
4
  # Saga Storage
3
5
  class SagaStorage
@@ -5,13 +7,12 @@ module RServiceBus2
5
7
  case uri.scheme
6
8
  when 'dir'
7
9
  require 'rservicebus2/saga_storage/dir'
8
- return SagaStorageDir.new(uri)
10
+ SagaStorageDir.new(uri)
9
11
  when 'inmem'
10
12
  require 'rservicebus2/saga_storage/inmemory'
11
- return SagaStorageInMemory.new(uri)
13
+ SagaStorageInMemory.new(uri)
12
14
  else
13
- abort("Scheme, #{uri.scheme}, not recognised when configuring
14
- SagaStorage, #{uri}")
15
+ abort("Scheme, #{uri.scheme}, not recognised when configuring SagaStorage, #{uri}")
15
16
  end
16
17
  end
17
18
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'rservicebus2/sendat_storage'
2
4
 
3
5
  module RServiceBus2
@@ -16,9 +18,9 @@ module RServiceBus2
16
18
  end
17
19
 
18
20
  def process
19
- now = DateTime.now
20
- @sendat_storage.get_all.each_with_index do |row, idx|
21
- next if row['timestamp'] <= now
21
+ now = Time.now
22
+ @sendat_storage.all.each_with_index do |row, idx|
23
+ next if now <= row['timestamp']
22
24
 
23
25
  @bus._send_needs_wrapping(row['msg'], row['queue_name'],
24
26
  row['correlation_id'])
@@ -1,20 +1,22 @@
1
+ # frozen_string_literal: true
1
2
 
2
3
  module RServiceBus2
3
4
  # Send At Storage
4
5
  class SendAtStorage
6
+ # rubocop:disable Metrics/MethodLength
5
7
  def self.get(uri)
6
8
  case uri.scheme
7
9
  when 'file'
8
10
  require 'rservicebus2/sendat_storage/file'
9
- return SendAtStorageFile.new(uri)
11
+ SendAtStorageFile.new(uri)
10
12
  when 'inmem'
11
13
  require 'rservicebus2/sendat_storage/inmemory'
12
- return SendAtStorageInMemory.new(uri)
14
+ SendAtStorageInMemory.new(uri)
13
15
  else
14
16
  abort("Scheme, #{uri.scheme}, not recognised when configuring
15
17
  SendAtStorage, #{uri}")
16
18
  end
17
19
  end
18
-
20
+ # rubocop:enable Metrics/MethodLength
19
21
  end
20
22
  end
@@ -1,8 +1,12 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module RServiceBus2
2
4
  # Send at storage file
3
5
  class SendAtStorageFile
4
6
  def initialize(uri)
7
+ RServiceBus2.log "SendAtStorageFile configured: #{uri.path}"
5
8
  @list = load(uri.path)
9
+ @path = uri.path
6
10
  end
7
11
 
8
12
  def load(path)
@@ -20,7 +24,7 @@ module RServiceBus2
20
24
  save
21
25
  end
22
26
 
23
- def get_all
27
+ def all
24
28
  @list
25
29
  end
26
30
 
@@ -31,7 +35,7 @@ module RServiceBus2
31
35
 
32
36
  def save
33
37
  content = YAML.dump(@list)
34
- File.open(@uri.path, 'w') { |f| f.write(YAML.dump(content)) }
38
+ File.open(@path, 'w') { |f| f.write(content) }
35
39
  end
36
40
  end
37
41
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module RServiceBus2
2
4
  # Send at storage in memory
3
5
  class SendAtStorageInMemory
@@ -1,7 +1,8 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'rservicebus2/state_storage'
2
4
 
3
5
  module RServiceBus2
4
-
5
6
  # State Manager
6
7
  class StateManager
7
8
  def required
@@ -16,15 +17,15 @@ module RServiceBus2
16
17
  end
17
18
 
18
19
  def begin
19
- @state_storage.begin unless @state_storage.nil?
20
+ @state_storage&.begin
20
21
  end
21
22
 
22
23
  def get(handler)
23
- @state_storage.get(handler) unless @state_storage.nil?
24
+ @state_storage&.get(handler)
24
25
  end
25
26
 
26
27
  def commit
27
- @state_storage.commit unless @state_storage.nil?
28
+ @state_storage&.commit
28
29
  end
29
30
  end
30
31
  end
@@ -1,17 +1,18 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module RServiceBus2
2
4
  # State Storage
3
5
  class StateStorage
4
6
  def self.get(uri)
5
7
  case uri.scheme
6
8
  when 'dir'
7
- require 'rservicebus2/state_storage/dir.rb'
8
- return StateStorageDir.new(uri)
9
+ require 'rservicebus2/state_storage/dir'
10
+ StateStorageDir.new(uri)
9
11
  when 'inmem'
10
- require 'rservicebus2/state_storage/inmemory.rb'
11
- return StateStorageInMemory.new(uri)
12
+ require 'rservicebus2/state_storage/inmemory'
13
+ StateStorageInMemory.new(uri)
12
14
  else
13
- abort("Scheme, #{uri.scheme}, not recognised when configuring
14
- StateStorage, #{uri}")
15
+ abort("Scheme, #{uri.scheme}, not recognised when configuring StateStorage, #{uri}")
15
16
  end
16
17
  end
17
18
  end
@@ -1,8 +1,11 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module RServiceBus2
2
4
  # Used to collect various run time stats for runtime reporting
3
5
  class StatisticManager
4
6
  attr_accessor :output
5
7
 
8
+ # rubocop:disable Metrics/MethodLength
6
9
  def initialize(host)
7
10
  @host = host
8
11
  @hash = {}
@@ -20,6 +23,7 @@ module RServiceBus2
20
23
  RServiceBus2.get_value('STAT_OUTPUT_COUNTDOWN', '1').to_i
21
24
  @stat_output_countdown = 0
22
25
  end
26
+ # rubocop:enable Metrics/MethodLength
23
27
 
24
28
  def inc_total_processed
25
29
  @total_processed += 1
@@ -51,7 +55,7 @@ module RServiceBus2
51
55
  @total_by_message_type[class_name] += 1
52
56
  end
53
57
 
54
- def get_for_reporting_2
58
+ def internal_formatted_reporting
55
59
  return unless @written == false
56
60
 
57
61
  @written = true
@@ -63,7 +67,7 @@ module RServiceBus2
63
67
  types
64
68
  end
65
69
 
66
- def get_for_reporting_9
70
+ def formatted_reporting
67
71
  "T:#{@total_processed};" \
68
72
  "E:#{@total_errored};" \
69
73
  "S:#{@total_sent};" \
@@ -72,7 +76,7 @@ module RServiceBus2
72
76
  end
73
77
 
74
78
  def report
75
- @host.log(get_for_reporting_9) if @output
79
+ @host.log(formatted_reporting) if @output
76
80
  end
77
81
 
78
82
  def tick
@@ -1,9 +1,11 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module RServiceBus2
2
4
  # Subscription Manager
3
5
  class SubscriptionManager
4
6
  def initialize(subscription_storage)
5
7
  @subscription_storage = subscription_storage
6
- @subscriptions = @subscription_storage.get_all
8
+ @subscriptions = @subscription_storage.all
7
9
  end
8
10
 
9
11
  # Get subscriptions for given eventName
@@ -11,7 +13,8 @@ module RServiceBus2
11
13
  subscriptions = @subscriptions[event_name]
12
14
  if subscriptions.nil?
13
15
  RServiceBus2.log "No subscribers for event, #{event_name}"
14
- RServiceBus2.log "If there should be, ensure you have the appropriate evironment variable set, eg MESSAGE_ENDPOINT_MAPPINGS=#{event_name}:<Queue Name>"
16
+ RServiceBus2.log 'If there should be, ensure you have the appropriate evironment variable set, ' \
17
+ "eg MESSAGE_ENDPOINT_MAPPINGS=#{event_name}:<Queue Name>"
15
18
  return []
16
19
  end
17
20
 
@@ -19,13 +22,12 @@ module RServiceBus2
19
22
  end
20
23
 
21
24
  def add(event_name, queue_name)
22
- RServiceBus2.log 'Adding subscription for, ' +
23
- event_name + ', to, ' + queue_name
25
+ RServiceBus2.log "Adding subscription for, #{event_name}, to, #{queue_name}"
24
26
  @subscriptions = @subscription_storage.add(event_name, queue_name)
25
27
  end
26
28
 
27
29
  def remove(_event_name, _queue_name)
28
- fail 'Method, remove, needs to be implemented for this subscription storage'
30
+ raise 'Method, remove, needs to be implemented for this subscription storage'
29
31
  end
30
32
  end
31
33
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'uri'
2
4
 
3
5
  module RServiceBus2
@@ -14,8 +16,8 @@ module RServiceBus2
14
16
  end
15
17
 
16
18
  # Get a list of all subscription, as an Array
17
- def get_all
18
- fail 'Method, get_all, needs to be implemented for SubscriptionStorage'
19
+ def all
20
+ raise 'Method, all, needs to be implemented for SubscriptionStorage'
19
21
  end
20
22
 
21
23
  # Add a new subscription
@@ -23,7 +25,7 @@ module RServiceBus2
23
25
  # has asked for notification
24
26
  # @param [String] queue_name the queue to which the event should be sent
25
27
  def add(_event_name, _queue_name)
26
- fail 'Method, add, needs to be implemented for this subscription storage'
28
+ raise 'Method, add, needs to be implemented for this subscription storage'
27
29
  end
28
30
 
29
31
  # Remove an existing subscription
@@ -32,8 +34,7 @@ module RServiceBus2
32
34
  # has asked for notification
33
35
  # @param [String] queue_name the queue to which the event should be sent
34
36
  def remove(_event_name, _queue_name)
35
- fail 'Method, remove, needs to be implemented for this subscription
36
- storage'
37
+ raise 'Method, remove, needs to be implemented for this subscription storage'
37
38
  end
38
39
  end
39
40
  end
@@ -1,15 +1,9 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module RServiceBus2
2
4
  # Implementation of Subscription Storage to Redis
3
5
  class SubscriptionStorageFile < SubscriptionStorage
4
- # Constructor
5
- #
6
- # @param [String] app_ame Name of the application, used as a Namespace
7
- # @param [String] uri resource location to attach, eg redis://127.0.0.1/foo
8
- def initialize(app_name, uri)
9
- super(app_name, uri)
10
- end
11
-
12
- def get_all
6
+ def all
13
7
  RServiceBus2.log 'Load subscriptions'
14
8
  return {} unless File.exist?(@uri.path)
15
9
 
@@ -17,13 +11,7 @@ module RServiceBus2
17
11
  end
18
12
 
19
13
  def add(event_name, queue_name)
20
- # s => subscriptions
21
- if File.exist?(@uri.path)
22
- s = YAML.load(File.open(@uri.path))
23
- else
24
- s = {}
25
- end
26
-
14
+ s = File.exist?(@uri.path) ? YAML.load(File.open(@uri.path)) : {}
27
15
  s[event_name] = [] if s[event_name].nil?
28
16
 
29
17
  s[event_name] << queue_name
@@ -35,8 +23,7 @@ module RServiceBus2
35
23
  end
36
24
 
37
25
  def remove(_event_name, _queue_name)
38
- fail 'Method, remove, needs to be implemented for this
39
- subscription storage'
26
+ raise 'Method, remove, needs to be implemented for this subscription storage'
40
27
  end
41
28
  end
42
29
  end