rservicebus2 0.2.5 → 0.2.10

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'zlib'
2
4
  require 'yaml'
3
5
  require 'uuidtools'
@@ -5,10 +7,12 @@ require 'uuidtools'
5
7
  module RServiceBus2
6
8
  # This is the top level message that is passed around the bus
7
9
  class Message
8
- attr_reader :return_address, :msg_id, :remote_queue_name, :remote_host_name,
10
+ attr_reader :return_address, :msg_id,
9
11
  :last_error_source_queue, :last_error_string, :correlation_id,
10
12
  :sendat, :error_list
11
13
 
14
+ attr_accessor :remote_host_name, :remote_queue_name, :send_at
15
+
12
16
  # Constructor
13
17
  #
14
18
  # @param [Object] msg The msg to be sent
@@ -26,7 +30,7 @@ module RServiceBus2
26
30
  @correlation_id = correlation_id
27
31
  @return_address = return_address
28
32
 
29
- @createdat = DateTime.now
33
+ @createdat = Time.now
30
34
 
31
35
  @msg_id = UUIDTools::UUID.random_create
32
36
  @error_list = []
@@ -47,25 +51,11 @@ module RServiceBus2
47
51
  @error_list << RServiceBus2::ErrorMessage.new(source_queue, error_string)
48
52
  end
49
53
 
50
- def set_remote_host_name(host_name)
51
- @remote_host_name = host_name
52
- end
53
-
54
- def set_remote_queue_name(queue_name)
55
- @remote_queue_name = queue_name
56
- end
57
-
58
- def send_at(timestamp)
59
- @send_at = timestamp
60
- end
61
-
62
54
  # @return [Object] The msg to be sent
63
55
  def msg
64
- if @compressed == true
65
- return YAML.load(Zlib::Inflate.inflate(@_msg))
66
- else
67
- return YAML.load( @_msg )
68
- end
56
+ return YAML.load(Zlib::Inflate.inflate(@_msg)) if @compressed == true
57
+
58
+ YAML.load(@_msg)
69
59
  rescue ArgumentError => e
70
60
  raise e if e.message.index('undefined class/module ').nil?
71
61
 
@@ -26,7 +26,6 @@ module RServiceBus2
26
26
  def initialize(bus, name, uri)
27
27
  @bus = bus
28
28
  new_anonymous_class = Class.new(MonitorMessage)
29
- puts "name: #{name}"
30
29
  Object.const_set(name, new_anonymous_class)
31
30
  @msg_type = Object.const_get(name)
32
31
 
@@ -17,56 +17,34 @@ module RServiceBus2
17
17
  content
18
18
  end
19
19
 
20
- # rubocop:disable Metrics/MethodLength
21
- def read_content_from_file(file_path)
22
- content = ''
23
- if @input_filter.length > 0
24
- if @input_filter[0] == 'ZIP'
25
- content = read_content_from_zip_file(file_path)
26
- elsif @input_filter[0] == 'GZ'
27
- content = read_content_from_gz_file(file_path)
28
- elsif @input_filter[0] == 'TAR'
29
- fail 'TAR reader not implemented'
30
- end
31
-
32
- else
33
- content = IO.read(file_path)
34
- end
35
-
36
- content
37
- end
38
-
39
20
  def process_path(object_key)
40
21
  # content = read_content_from_object(object_key)
41
- content = object_key
22
+ resp = @s3_client.get_object(bucket: @bucket_name, key: object_key)
23
+
24
+ # call #read or #string on the response body
25
+ content = resp.body.read
42
26
  payload = process_content(content)
43
27
 
44
- send(payload, URI.parse(URI.encode("s3://#{@region}/#{@bucket_name}/#{object_key}")))
28
+ send(payload, URI.parse(CGI.escape("s3://#{@region}/#{@bucket_name}/#{object_key}")))
29
+
30
+ @s3_client.delete_object({ bucket: @bucket_name, key: object_key })
31
+
45
32
  content
46
33
  end
47
34
 
48
35
  def look
49
36
  file_processed = 0
50
- max_files_processed = 1
37
+ max_files_processed = 2
51
38
 
52
- objects = @s3_client.list_objects_v2(
53
- bucket: @bucket_name,
54
- max_keys: max_files_processed
55
- ).contents
56
-
57
- return if objects.count.zero?
39
+ objects = @s3_client.list_objects_v2( bucket: @bucket_name, max_keys: max_files_processed).contents
58
40
 
59
41
  objects.each do |object|
60
42
  RServiceBus2.log "Ready to process, #{object.key}"
61
43
  process_path(object.key)
62
- # content = process_path(object.key)
63
-
64
- # File.unlink(file_path)
65
44
 
66
45
  file_processed += 1
67
46
  RServiceBus2.log "Processed #{file_processed} of #{objects.length}."
68
47
  RServiceBus2.log "Allow system tick #{self.class.name}"
69
- break if file_processed >= max_files_processed
70
48
  end
71
49
  end
72
50
  end
@@ -0,0 +1,32 @@
1
+ require 'aws-sdk-sqs'
2
+ require 'aws-sdk-sts'
3
+
4
+ module RServiceBus2
5
+ # Monitor S3 Bucket for objects
6
+ class MonitorAWSSQS < Monitor
7
+ def connect(uri)
8
+ queue_name = uri.path.sub('/', '')
9
+
10
+ region = uri.host
11
+
12
+ sts_client = Aws::STS::Client.new(region: region)
13
+ caller_identity_account = sts_client.get_caller_identity.account
14
+
15
+ @queue_url = "https://sqs.#{region}.amazonaws.com/" +
16
+ "#{caller_identity_account}/#{queue_name}"
17
+ @sqs_client = Aws::SQS::Client.new(region: region)
18
+ end
19
+
20
+ def look
21
+ # TODO make max available as env variable
22
+ response = @sqs_client.receive_message(queue_url: @queue_url, max_number_of_messages: 1)
23
+ response.messages.each do |message|
24
+ send(message.body, URI.parse(CGI.escape(@queue_url)))
25
+ @sqs_client.delete_message({
26
+ queue_url: @queue_url,
27
+ receipt_handle: message.receipt_handle
28
+ })
29
+ end
30
+ end
31
+ end
32
+ end
@@ -124,7 +124,7 @@ module RServiceBus2
124
124
 
125
125
  unless @archivedir.nil?
126
126
  basename = File.basename(file_path)
127
- new_file_path = "#{@archivedir}/#{basename}.#{DateTime.now.strftime('%Y%m%d%H%M%S%L')}.zip"
127
+ new_file_path = "#{@archivedir}/#{basename}.#{Time.now.strftime('%Y%m%d%H%M%S%L')}.zip"
128
128
  RServiceBus2.log "Writing to archive, #{new_file_path}"
129
129
 
130
130
  Zip::ZipOutputStream.open(new_file_path) do |zos|
@@ -23,10 +23,10 @@ module RServiceBus2
23
23
  RServiceBus2.rlog "Checking app resources for: #{monitor.class.name}"
24
24
  RServiceBus2.rlog "If your attribute is not getting set, check that it is
25
25
  in the 'attr_accessor' list"
26
- @resource_manager.get_all.each do |k, v|
26
+ @resource_manager.all.each do |k, v|
27
27
  next unless monitor.class.method_defined?(k)
28
28
 
29
- monitor.instance_variable_set("@#{k}", v.get_resource)
29
+ monitor.instance_variable_set("@#{k}", v.resource)
30
30
  @resource_list[monitor.class.name] = [] if
31
31
  @resource_list[monitor.class.name].nil?
32
32
  @resource_list[monitor.class.name] << v
@@ -51,6 +51,9 @@ module RServiceBus2
51
51
  when 'awss3'
52
52
  require 'rservicebus2/monitor/awss3'
53
53
  monitor = MonitorAWSS3.new(@host, name, uri)
54
+ when 'awssqs'
55
+ require 'rservicebus2/monitor/awssqs'
56
+ monitor = MonitorAWSSQS.new(@host, name, uri)
54
57
  when 'dirnotifier'
55
58
  require 'rservicebus2/monitor/dirnotifier'
56
59
  monitor = MonitorDirNotifier.new(@host, name, uri)
@@ -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)
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module RServiceBus2
2
4
  # Coordinate Transactions across resources, handlers, and Sagas
3
5
  class ResourceManager
@@ -13,7 +15,7 @@ module RServiceBus2
13
15
  @app_resources[name] = res
14
16
  end
15
17
 
16
- def get_all
18
+ def all
17
19
  @app_resources
18
20
  end
19
21
 
@@ -37,9 +39,8 @@ module RServiceBus2
37
39
  def commit(msg_name)
38
40
  @state_manager.commit
39
41
  @saga_storage.commit
40
- RServiceBus2.rlog "HandlerManager.commitResourcesUsedToProcessMsg,
41
- #{msg_name}"
42
- @current_resources.each do |k, v|
42
+ RServiceBus2.rlog "HandlerManager.commitResourcesUsedToProcessMsg, #{msg_name}"
43
+ @current_resources.each do |_k, v|
43
44
  RServiceBus2.rlog "Commit resource, #{v.class.name}"
44
45
  v.commit
45
46
  v.finished
@@ -48,21 +49,15 @@ module RServiceBus2
48
49
 
49
50
  def rollback(msg_name)
50
51
  @saga_storage.rollback
51
- RServiceBus2.rlog "HandlerManager.rollbackResourcesUsedToProcessMsg,
52
- #{msg_name}"
53
- @current_resources.each do |k, v|
54
- begin
55
- RServiceBus2.rlog "Rollback resource, #{v.class.name}"
56
- v.rollback
57
- v.finished
58
- rescue StandardError => e1
59
- puts "Caught nested exception rolling back, #{v.class.name}, for msg,
60
- #{msg_name}"
61
- puts '****'
62
- puts e1.message
63
- puts e1.backtrace
64
- puts '****'
65
- end
52
+ RServiceBus2.rlog "HandlerManager.rollbackResourcesUsedToProcessMsg, #{msg_name}"
53
+ @current_resources.each do |_k, v|
54
+ RServiceBus2.rlog "Rollback resource, #{v.class.name}"
55
+ v.rollback
56
+ v.finished
57
+ rescue StandardError => e
58
+ puts "Caught nested exception rolling back, #{v.class.name}, for msg,
59
+ #{msg_name}"
60
+ puts "****\n#{e.message}\n#{e.backtrace}\n'****"
66
61
  end
67
62
  end
68
63
  end
@@ -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
@@ -44,7 +44,7 @@ module RServiceBus2
44
44
  is in the 'attr_accessor' list"
45
45
 
46
46
  @resource_list_by_saga_name[saga.class.name] = []
47
- @resource_manager.get_all.each do |k, v|
47
+ @resource_manager.all.each do |k, v|
48
48
  if saga.class.method_defined?(k)
49
49
  @resource_list_by_saga_name[saga.class.name] << k
50
50
  RServiceBus2.log "Resource attribute, #{k}, found for: " +
@@ -76,7 +76,7 @@ module RServiceBus2
76
76
  return if @resource_list_by_saga_name[saga.class.name].nil?
77
77
 
78
78
  @resource_list_by_saga_name[saga.class.name].each do |k, v|
79
- saga.instance_variable_set("@#{k}", @resource_manager.get(k).get_resource)
79
+ saga.instance_variable_set("@#{k}", @resource_manager.get(k).resource)
80
80
  RServiceBus2.rlog "App resource attribute, #{k}, set for: " + saga.class.name
81
81
  end
82
82
  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,13 @@
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
+ puts "@list: #{@list.class.name}"
10
+ @path = uri.path
6
11
  end
7
12
 
8
13
  def load(path)
@@ -20,7 +25,7 @@ module RServiceBus2
20
25
  save
21
26
  end
22
27
 
23
- def get_all
28
+ def all
24
29
  @list
25
30
  end
26
31
 
@@ -31,7 +36,7 @@ module RServiceBus2
31
36
 
32
37
  def save
33
38
  content = YAML.dump(@list)
34
- File.open(@uri.path, 'w') { |f| f.write(YAML.dump(content)) }
39
+ File.open(@path, 'w') { |f| f.write(content) }
35
40
  end
36
41
  end
37
42
  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
metadata CHANGED
@@ -1,85 +1,85 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rservicebus2
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.5
4
+ version: 0.2.10
5
5
  platform: ruby
6
6
  authors:
7
7
  - Guy Irvine
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-04-27 00:00:00.000000000 Z
11
+ date: 2021-05-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: uuidtools
14
+ name: beanstalk-client
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: 2.2.0
19
+ version: 1.1.1
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: 2.2.0
26
+ version: 1.1.1
27
27
  - !ruby/object:Gem::Dependency
28
- name: json
28
+ name: fluiddb
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: 2.5.1
33
+ version: 0.1.19
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: 2.5.1
40
+ version: 0.1.19
41
41
  - !ruby/object:Gem::Dependency
42
- name: beanstalk-client
42
+ name: json
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
45
  - - "~>"
46
46
  - !ruby/object:Gem::Version
47
- version: 1.1.1
47
+ version: 2.5.1
48
48
  type: :runtime
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
- version: 1.1.1
54
+ version: 2.5.1
55
55
  - !ruby/object:Gem::Dependency
56
- name: fluiddb
56
+ name: parse-cron
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
59
  - - "~>"
60
60
  - !ruby/object:Gem::Version
61
- version: 0.1.19
61
+ version: 0.1.4
62
62
  type: :runtime
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
66
  - - "~>"
67
67
  - !ruby/object:Gem::Version
68
- version: 0.1.19
68
+ version: 0.1.4
69
69
  - !ruby/object:Gem::Dependency
70
- name: parse-cron
70
+ name: uuidtools
71
71
  requirement: !ruby/object:Gem::Requirement
72
72
  requirements:
73
73
  - - "~>"
74
74
  - !ruby/object:Gem::Version
75
- version: 0.1.4
75
+ version: 2.2.0
76
76
  type: :runtime
77
77
  prerelease: false
78
78
  version_requirements: !ruby/object:Gem::Requirement
79
79
  requirements:
80
80
  - - "~>"
81
81
  - !ruby/object:Gem::Version
82
- version: 0.1.4
82
+ version: 2.2.0
83
83
  description: A Ruby interpretation of NServiceBus
84
84
  email: guy@guyirvine.com
85
85
  executables:
@@ -106,6 +106,7 @@ files:
106
106
  - lib/rservicebus2/appresource.rb
107
107
  - lib/rservicebus2/appresource/awsdynamodb.rb
108
108
  - lib/rservicebus2/appresource/awss3.rb
109
+ - lib/rservicebus2/appresource/awssqs.rb
109
110
  - lib/rservicebus2/appresource/dir.rb
110
111
  - lib/rservicebus2/appresource/file.rb
111
112
  - lib/rservicebus2/appresource/fluiddb.rb
@@ -127,6 +128,7 @@ files:
127
128
  - lib/rservicebus2/message/verboseoutput.rb
128
129
  - lib/rservicebus2/monitor.rb
129
130
  - lib/rservicebus2/monitor/awss3.rb
131
+ - lib/rservicebus2/monitor/awssqs.rb
130
132
  - lib/rservicebus2/monitor/dir.rb
131
133
  - lib/rservicebus2/monitor/dirnotifier.rb
132
134
  - lib/rservicebus2/monitor/message.rb