rservicebus2 0.2.17 → 0.2.22

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 920ff69a3fdd0f96882ed1c8bc12dd82d78ed789fa92c664f1b80f1484365058
4
- data.tar.gz: c41fa4a4284bfba83865cc6bac87e54312c26b556a6605f6e73022b8716c921c
3
+ metadata.gz: bdf786cee730fd3a1cb0ecb384d566ead93171cfa566b2b23bff09ab4dc75116
4
+ data.tar.gz: 4f8d812c2cf83ce2c1c561038d6d7b545afdb4e8ba5db414554fda52e19eb3d0
5
5
  SHA512:
6
- metadata.gz: e68d15cc10acdb2565ab02d6d8cfd3dc8b1a871becfe9a6b66f319d763526c16df8977456aadec9b5c2cc66595082bfde320a02458e384b2753b7be68cc01726
7
- data.tar.gz: f1c1c5ea85bf0920206067b92b58544068084b0e631c29ce6fdb00783114f87e507d59d3f2c237d1b307a9b3df7430deaf8ef74bf020ca6874c029801bfe99b0
6
+ metadata.gz: 3787ed00c7dd8947ae2e1b000f3953afb34ceb8ffce6925f9c97a3d7ce67cf8d10e2e0d7989da8b7abee110a608066a8e1f95acb752665376beb811a0636d519
7
+ data.tar.gz: c8f21f22ea7132ecdf43d6a14c681f68071def8e611ab91e1388fa2dbeaf38ad02c2e89ce9ded75551e444ade20eea54d0493f9252fee2dca2e5cab2fd6c3891
@@ -13,7 +13,8 @@ def return_msg(beanstalk, job, request_nbr)
13
13
 
14
14
  msg_name = payload.match('(\w*)', start_index)[1]
15
15
 
16
- msg = YAML.load(payload)
16
+ msg = RServiceBus2.safe_load(payload)
17
+
17
18
  if msg.last_error_string.nil?
18
19
  puts "*** Requested msg, #{request_nbr}, does not have a sourceQueue to
19
20
  which it can be returned"
data/lib/rservicebus2.rb CHANGED
@@ -23,6 +23,7 @@ require 'rservicebus2/config'
23
23
  require 'rservicebus2/endpointmapping'
24
24
  require 'rservicebus2/statistic_manager'
25
25
  require 'rservicebus2/audit'
26
+ require 'rservicebus2/yaml_safe_loader'
26
27
 
27
28
  require 'rservicebus2/message'
28
29
  require 'rservicebus2/message/subscription'
@@ -50,7 +50,7 @@ module RServiceBus2
50
50
  def check_for_reply(queue_name)
51
51
  @mq.subscribe(queue_name)
52
52
  body = @mq.pop
53
- @msg = YAML.load(body)
53
+ @msg = RServiceBus2.safe_load(body)
54
54
  @mq.ack
55
55
  @msg.msg
56
56
  end
@@ -33,7 +33,7 @@ module RServiceBus2
33
33
  finished
34
34
  end
35
35
 
36
- # A notification that ocurs after getResource, to allow cleanup
36
+ # Allow for cleanup, outside of transaction semantics
37
37
  def finished
38
38
  @connection.close
39
39
  end
@@ -53,12 +53,18 @@ module RServiceBus2
53
53
  end
54
54
 
55
55
  # Transaction Semantics
56
- def begin; end
56
+ def begin
57
+ RServiceBus2.rlog "#{self.class.name}. Default transaction.begin called"
58
+ end
57
59
 
58
60
  # Transaction Semantics
59
- def commit; end
61
+ def commit
62
+ RServiceBus2.rlog "#{self.class.name}. Default transaction.commit called"
63
+ end
60
64
 
61
65
  # Transaction Semantics
62
- def rollback; end
66
+ def rollback
67
+ RServiceBus2.rlog "#{self.class.name}. Default transaction.rollback called"
68
+ end
63
69
  end
64
70
  end
@@ -11,6 +11,8 @@ module RServiceBus2
11
11
  Aws::DynamoDB::Client.new(region: region)
12
12
  end
13
13
 
14
- def finished; end
14
+ def finished
15
+ RServiceBus2.rlog "#{self.class.name}. Finished"
16
+ end
15
17
  end
16
18
  end
@@ -11,6 +11,8 @@ module RServiceBus2
11
11
  Aws::S3::Client.new(region: region)
12
12
  end
13
13
 
14
- def finished; end
14
+ def finished
15
+ RServiceBus2.rlog "#{self.class.name}. Finished"
16
+ end
15
17
  end
16
18
  end
@@ -22,6 +22,8 @@ module RServiceBus2
22
22
  }
23
23
  end
24
24
 
25
- def finished; end
25
+ def finished
26
+ RServiceBus2.rlog "#{self.class.name}. Finished"
27
+ end
26
28
  end
27
29
  end
@@ -18,8 +18,8 @@ module RServiceBus2
18
18
  def commit
19
19
  @connection.commit
20
20
  end
21
- # Transaction Semantics
22
21
 
22
+ # Transaction Semantics
23
23
  def rollback
24
24
  @connection.rollback
25
25
  end
@@ -140,14 +140,17 @@ module RServiceBus2
140
140
  next if file_path.end_with?('.')
141
141
 
142
142
  msg_name = get_msg_name(file_path)
143
+ YamlSafeLoader.instance.add_permitted_class(msg_name
144
+ .gsub(/(?<=_|^)(\w)/) { Regexp.last_match(1).upcase }.gsub(/(?:_)(\w)/, '\1'))
143
145
  if File.directory?(file_path)
144
146
  load_handlers_from_second_level_path(msg_name, file_path)
145
- else
146
- # Classify
147
- handler_name = "message_handler_#{msg_name}"
148
- .gsub(/(?<=_|^)(\w)/) { Regexp.last_match(1).upcase }.gsub(/(?:_)(\w)/, '\1')
149
- load_handler(msg_name, file_path, handler_name)
147
+ next
150
148
  end
149
+
150
+ # Classify
151
+ handler_name = "message_handler_#{msg_name}"
152
+ .gsub(/(?<=_|^)(\w)/) { Regexp.last_match(1).upcase }.gsub(/(?:_)(\w)/, '\1')
153
+ load_handler(msg_name, file_path, handler_name)
151
154
  end
152
155
 
153
156
  self
@@ -1,7 +1,14 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'csv'
4
+
3
5
  # Helper functions
4
6
  module RServiceBus2
7
+ def self.safe_load(body)
8
+ # YAML.safe_load(body, permitted_classes: Module.constants)
9
+ YamlSafeLoader.instance.load(body)
10
+ end
11
+
5
12
  def self.convert_dto_to_hash(obj)
6
13
  hash = {}
7
14
  obj.instance_variables.each do |var|
@@ -162,7 +162,7 @@ module RServiceBus2
162
162
  body = @mq.pop
163
163
  begin
164
164
  @stats.inc_total_processed
165
- @msg = YAML.load(body)
165
+ @msg = RServiceBus2.safe_load(body)
166
166
  case @msg.msg.class.name
167
167
  when 'RServiceBus2::MessageSubscription'
168
168
  @subscription_manager.add(@msg.msg.event_name,
@@ -55,9 +55,9 @@ module RServiceBus2
55
55
 
56
56
  # @return [Object] The msg to be sent
57
57
  def msg
58
- return YAML.load(Zlib::Inflate.inflate(@_msg)) if @compressed == true
58
+ return RServiceBus2.safe_load(Zlib::Inflate.inflate(@_msg)) if @compressed == true
59
59
 
60
- YAML.load(@_msg)
60
+ RServiceBus2.safe_load(@_msg)
61
61
  rescue ArgumentError => e
62
62
  raise e if e.message.index('undefined class/module ').nil?
63
63
 
@@ -36,7 +36,9 @@ module RServiceBus2
36
36
  end
37
37
 
38
38
  # A notification that allows cleanup
39
- def finished; end
39
+ def finished
40
+ RServiceBus2.rlog "#{self.class.name}. Default Finished"
41
+ end
40
42
 
41
43
  # At least called in the Host rescue block, to ensure all network links
42
44
  # are healthy
@@ -9,6 +9,7 @@ module RServiceBus2
9
9
 
10
10
  # rubocop:disable Metrics/ClassLength
11
11
  class MonitorDir < Monitor
12
+ # rubocop:disable Metrics/MethodLength,Metrics/AbcSize
12
13
  def input_dir(uri)
13
14
  # Pass the path through the Dir object to check syntax on startup
14
15
  return Dir.new(uri.path) if File.writable?(uri.path)
@@ -27,42 +28,47 @@ module RServiceBus2
27
28
  "***** eg, rm #{uri.path} && mkdir #{uri.path}"
28
29
  abort
29
30
  end
31
+ # rubocop:enable Metrics/MethodLength,Metrics/AbcSize
30
32
 
31
- # rubocop:disable Metrics/MethodLength
32
- def connect(uri)
33
- @path = input_dir(uri).path
34
- @input_filter = []
35
-
36
- return if uri.query.nil?
33
+ def initialise_archive(parts)
34
+ return unless parts.key?('archive')
37
35
 
38
- parts = CGI.parse(uri.query)
39
- @querystringparts = parts
40
- if parts.key?('archive')
41
- archiveuri = URI.parse(parts['archive'][0])
42
- unless File.directory?(archiveuri.path)
43
- puts '***** Archive file name templating not yet supported.'
44
- puts "***** Directory's only."
45
- abort
46
- end
47
- @archivedir = archiveuri.path
36
+ archiveuri = URI.parse(parts['archive'][0])
37
+ unless File.directory?(archiveuri.path)
38
+ puts '***** Archive file name templating not yet supported.'
39
+ puts "***** Directory's only."
40
+ abort
48
41
  end
42
+ @archivedir = archiveuri.path
43
+ end
49
44
 
50
- return unless parts.key?('input_filter')
51
-
45
+ def initialise_input_filter(parts)
52
46
  if parts['input_filter'].count > 1
53
- puts 'Too many input_filters specified.'
54
- puts '*** ZIP, or GZ are the only valid input_filters.'
47
+ puts 'Too many input_filters specified.\n*** ZIP, or GZ are the only valid input_filters.'
55
48
  abort
56
49
  end
57
50
 
58
51
  unless %w[ZIP GZ TAR].include?(parts['input_filter'][0]).nil?
59
- puts 'Invalid input_filter specified.'
60
- puts '*** ZIP, or GZ are the only valid input_filters.'
52
+ puts 'Invalid input_filter specified.\n' \
53
+ '*** ZIP, or GZ are the only valid input_filters.'
61
54
  abort
62
55
  end
56
+
63
57
  @input_filter << parts['input_filter'][0]
64
58
  end
65
- # rubocop:enable Metrics/MethodLength
59
+
60
+ def connect(uri)
61
+ @path = input_dir(uri).path
62
+ @input_filter = []
63
+
64
+ return if uri.query.nil?
65
+
66
+ parts = CGI.parse(uri.query)
67
+ @querystringparts = parts
68
+ initialise_archive(parts)
69
+
70
+ initialise_input_filter(parts) if parts.key?('input_filter')
71
+ end
66
72
 
67
73
  def process_content(content)
68
74
  content
@@ -85,14 +91,14 @@ module RServiceBus2
85
91
  # rubocop:disable Metrics/MethodLength
86
92
  def read_content_from_file(file_path)
87
93
  content = ''
88
- if @input_filter.positive?
94
+ if @input_filter.length.positive?
89
95
  case @input_filter[0]
90
96
  when 'ZIP'
91
97
  content = read_content_from_zip_file(file_path)
92
98
  when 'GZ'
93
99
  content = read_content_from_gz_file(file_path)
94
- when 'TAR'
95
- raise 'TAR reader not implemented'
100
+ else
101
+ raise "#{@input_filter[0]} reader not implemented."
96
102
  end
97
103
 
98
104
  else
@@ -103,14 +109,6 @@ module RServiceBus2
103
109
  end
104
110
  # rubocop:enable Metrics/MethodLength
105
111
 
106
- def process_path(file_path)
107
- content = read_content_from_file(file_path)
108
- payload = process_content(content)
109
-
110
- send(payload, URI.parse(CGI.escape("file://#{file_path}")))
111
- content
112
- end
113
-
114
112
  def archive_file(file_path, content)
115
113
  basename = File.basename(file_path)
116
114
  new_file_path = "#{@archivedir}/#{basename}.#{Time.now.strftime('%Y%m%d%H%M%S%L')}.zip"
@@ -122,24 +120,34 @@ module RServiceBus2
122
120
  end
123
121
  end
124
122
 
123
+ def process_path(file_path)
124
+ if File.file?(file_path) != true
125
+ RServiceBus2.log "Skipping directory, #{file_path}"
126
+ return
127
+ end
128
+
129
+ RServiceBus2.log "Ready to process, #{file_path}"
130
+ content = read_content_from_file(file_path)
131
+ payload = process_content(content)
132
+
133
+ send(payload, URI.parse(CGI.escape("file://#{file_path}")))
134
+
135
+ archive_file(file_path, content) unless @archivedir.nil?
136
+
137
+ File.unlink(file_path)
138
+ end
139
+
125
140
  def look
126
141
  file_processed = 0
127
142
  max_files_processed = 10
128
143
 
129
144
  file_list = Dir.glob("#{@path}/*")
130
145
  file_list.each do |file_path|
131
- if File.file?(file_path) != true
132
- RServiceBus2.log "Skipping directory, #{file_path}"
133
- next
134
- end
135
- RServiceBus2.log "Ready to process, #{file_path}"
136
- content = process_path(file_path)
137
-
138
- archive_file(file_path, content) unless @archivedir.nil?
139
- File.unlink(file_path)
146
+ process_path(file_path)
140
147
 
141
148
  file_processed += 1
142
- RServiceBus2.log "Processed #{file_processed} of #{file_list.length}.\nAllow system tick #{self.class.name}"
149
+ RServiceBus2.log "Processed #{file_processed} of #{file_list.length}."
150
+ RServiceBus2.log "Allow system tick #{self.class.name}"
143
151
 
144
152
  break if file_processed >= max_files_processed
145
153
  end
@@ -9,67 +9,66 @@ module RServiceBus2
9
9
  class MonitorDirNotifier < Monitor
10
10
  attr_reader :path, :processing_folder, :filter
11
11
 
12
- def connect(uri)
13
- # Pass the path through the Dir object to check syntax on startup
14
- begin
15
- open_folder uri.path
16
- unless File.writable?(uri.path)
17
- puts "***** Directory is not writable, #{uri.path}.\n" \
18
- "***** Make the directory, #{uri.path}, writable and try again."
19
- abort
20
- end
21
- rescue Errno::ENOENT
22
- puts "***** Directory does not exist, #{uri.path}.\n" \
23
- "***** Create the directory, #{uri.path}, and try again.\n" \
24
- "***** eg, mkdir #{uri.path}"
25
- abort
26
- rescue Errno::ENOTDIR
27
- puts "***** The specified path does not point to a directory, #{uri.path}." \
28
- "***** Either repoint path to a directory, or remove, #{uri.path}, and create it as a directory." \
29
- "***** eg, rm #{uri.path} && mkdir #{uri.path}"
30
- abort
31
- end
12
+ def directory_not_writable(path, param_name)
13
+ "***** #{param_name} is not writable, #{path}.\n" \
14
+ "***** Make the directory, #{path}, writable and try again."
15
+ end
32
16
 
33
- @path = uri.path
17
+ def directory_does_not_exist(path, param_name)
18
+ "***** #{param_name} does not exist, #{path}.\n" \
19
+ "***** Create the directory, #{path}, and try again.\n" \
20
+ "***** eg, mkdir #{path}"
21
+ end
22
+
23
+ def path_is_not_a_directory(path)
24
+ "***** The specified path does not point to a directory, #{path}.\n" \
25
+ "***** Either repoint path to a directory, or remove, #{path}, and create it as a directory.\n" \
26
+ "***** eg, rm #{path} && mkdir #{path}"
27
+ end
28
+
29
+ def processing_directory_not_specified(path)
30
+ '***** Processing Directory is not specified.' \
31
+ '***** Specify the Processing Directory as a query string in the Path URI' \
32
+ "***** eg, '/#{path}?processing=*ProcessingDir*'"
33
+ end
34
34
 
35
+ def validate_directory(path, param_name)
36
+ open_folder path
37
+ return if File.writable?(path)
38
+
39
+ puts directory_not_writable(path, param_name)
40
+ abort
41
+ rescue Errno::ENOENT
42
+ puts directory_does_not_exist(path, param_name)
43
+ abort
44
+ rescue Errno::ENOTDIR
45
+ puts path_is_not_a_directory(path)
46
+ abort
47
+ end
48
+
49
+ def validate_processing_directory(uri)
35
50
  if uri.query.nil?
36
- puts '***** Processing Directory is not specified.'
37
- puts '***** Specify the Processing Directory as a query string in the
38
- Path URI'
39
- puts "***** eg, '/#{uri.path}?processing=*ProcessingDir*"
51
+ puts processing_directory_not_specified(uri.path)
40
52
  abort
41
- else
42
- parts = CGI.parse(uri.query)
43
-
44
- if parts.key? 'processing'
45
- processing_uri = URI.parse parts['processing'][0]
46
- begin
47
- open_folder processing_uri.path
48
- unless File.writable?(processing_uri.path)
49
- puts "***** 1Processing Directory is not writable,
50
- #{processing_uri.path}."
51
- puts "***** Make the directory, #{processing_uri.path},
52
- writable and try again."
53
- abort
54
- end
55
- rescue Errno::ENOENT
56
- puts "***** Processing Directory does not exist, #{processing_uri.path}." \
57
- "***** Create the directory, #{processing_uri.path}, and try again." \
58
- "***** eg, mkdir #{processing_uri.path}"
59
- abort
60
- rescue Errno::ENOTDIR
61
- puts "***** Processing Directory does not point to a directory, #{processing_uri.path}." \
62
- "***** Either repoint path to a directory, or remove, #{processing_uri.path}, and create it as a directory.\n" \
63
- "***** eg, rm #{processing_uri.path} && mkdir #{processing_uri.path}"
64
- abort
65
- end
66
-
67
- @processing_folder = processing_uri.path
68
- end
69
-
70
- @filter = '*'
71
- @filter = parts['filter'][0] if parts.key? 'filter'
72
53
  end
54
+
55
+ parts = CGI.parse(uri.query)
56
+ return unless parts.key? 'processing'
57
+
58
+ processing_uri = URI.parse parts['processing'][0]
59
+ validate_directory processing_uri.path, 'Processing Directory'
60
+ @processing_folder = processing_uri.path
61
+ end
62
+
63
+ def connect(uri)
64
+ # Pass the path through the Dir object to check syntax on startup
65
+
66
+ validate_directory uri, 'Directory'
67
+ @path = uri.path
68
+ validate_processing_directory(uri)
69
+
70
+ @filter = '*'
71
+ @filter = parts['filter'][0] if parts.key? 'filter'
73
72
  end
74
73
 
75
74
  def look
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'aws-sdk-sqs'
2
4
  require 'aws-sdk-sts'
3
5
  require 'rservicebus2/mq'
@@ -6,75 +8,59 @@ module RServiceBus2
6
8
  # Beanstalk client implementation.
7
9
  class MQAWS < MQ
8
10
  # Connect to the broker
9
- # rubocop:disable Metrics/AbcSize,Metrics/MethodLength
10
- def connect(region, port)
11
- begin
12
- @max_job_size = 4_194_304
13
- @region = region
11
+ def connect(region, _port)
12
+ @max_job_size = 4_194_304
13
+ @region = region
14
14
 
15
- sts_client = Aws::STS::Client.new(region: region)
16
- @caller_identity_account = sts_client.get_caller_identity.account
17
- rescue StandardError => e
18
- puts 'Error connecting to AWS'
19
- puts "Host string, #{region}"
20
- puts e.message
21
- puts e.backtrace
22
- abort
23
- end
15
+ sts_client = Aws::STS::Client.new(region: region)
16
+ @caller_identity_account = sts_client.get_caller_identity.account
17
+ rescue StandardError => e
18
+ puts 'Error connecting to AWS'
19
+ puts "Host string, #{region}"
20
+ puts e.message
21
+ puts e.backtrace
22
+ abort
24
23
  end
25
24
 
26
25
  def subscribe(queuename)
27
26
  # For example:
28
27
  # 'https://sqs.us-east-1.amazonaws.com/111111111111/my-queue'
29
- @queue_url = 'https://sqs.' + @region + '.amazonaws.com/' +
30
- @caller_identity_account + '/' + queuename
28
+ @queue_url = "https://sqs.#{@region}.amazonaws.com/#{@caller_identity_account}/#{queuename}"
31
29
  @sqs_client = Aws::SQS::Client.new(region: @region)
32
30
  end
33
31
 
34
32
  # Get next msg from queue
35
33
  def pop
36
- begin
37
- response = @sqs_client.receive_message(
38
- queue_url: @queue_url,
39
- max_number_of_messages: 1
40
- )
34
+ response = @sqs_client.receive_message(queue_url: @queue_url, max_number_of_messages: 1)
41
35
 
42
- if response.messages.count.zero?
43
- raise NoMsgToProcess
44
- end
36
+ raise NoMsgToProcess if response.messages.count.zero?
45
37
 
46
- response.messages.each do |message|
47
- @job = message
48
- end
49
- rescue StandardError => e
50
- raise e
38
+ response.messages.each do |message|
39
+ @job = message
51
40
  end
41
+ rescue StandardError => e
42
+ raise e
43
+ ensure
52
44
  @job.body
53
45
  end
54
46
 
55
47
  def return_to_queue
56
- # @job.release if @job
57
48
  @job = nil
58
49
  end
59
50
 
60
51
  def ack
61
- @sqs_client.delete_message({
62
- queue_url: @queue_url,
63
- receipt_handle: @job.receipt_handle
64
- })
52
+ @sqs_client.delete_message({ queue_url: @queue_url, receipt_handle: @job.receipt_handle })
65
53
  @job = nil
66
54
  end
67
55
 
68
56
  def send(queue_name, msg)
69
57
  if msg.length > @max_job_size
70
- puts '***Attempting to send a msg which will not fit on queue.'
71
- puts "***Msg size, #{msg.length}, max msg size, #{@max_job_size}."
72
- fail JobTooBigError, "Msg size, #{msg.length}, max msg size,
73
- #{@max_job_size}"
58
+ puts '***Attempting to send a msg which will not fit on queue.' \
59
+ "***Msg size, #{msg.length}, max msg size, #{@max_job_size}."
60
+ raise JobTooBigError, "Msg size, #{msg.length}, max msg size, #{@max_job_size}"
74
61
  end
75
62
 
76
- queue_url = 'https://sqs.' + @region + '.amazonaws.com/' +
77
- @caller_identity_account + '/' + queue_name
63
+ queue_url = "https://sqs.#{@region}.amazonaws.com/#{@caller_identity_account}/#{queue_name}"
78
64
 
79
65
  @sqs_client.send_message(
80
66
  queue_url: queue_url,
@@ -16,19 +16,17 @@ module RServiceBus2
16
16
  @beanstalk = Beanstalk::Pool.new([string])
17
17
  @max_job_size = @beanstalk.stats['max-job-size']
18
18
  if @max_job_size < 4_194_304
19
- puts "***WARNING: Lowest recommended.max-job-size is 4m, current
20
- max-job-size, #{@max_job_size.to_f / (1024 * 1024)}m"
21
- puts '***WARNING: Set the job size with the -z switch, eg
22
- /usr/local/bin/beanstalkd -z 4194304'
19
+ puts '***WARNING: Lowest recommended.max-job-size is 4m, current ' \
20
+ "max-job-size, #{@max_job_size.to_f / (1024 * 1024)}m"
21
+ puts '***WARNING: Set the job size with the -z switch, eg ' \
22
+ '/usr/local/bin/beanstalkd -z 4194304'
23
23
  end
24
24
  rescue StandardError => e
25
25
  puts 'Error connecting to Beanstalk'
26
26
  puts "Host string, #{string}"
27
27
  if e.message == 'Beanstalk::NotConnected'
28
- puts '***Most likely, beanstalk is not running. Start beanstalk,
29
- and try running this again.'
30
- puts "***If you still get this error, check beanstalk is running
31
- at, #{string}"
28
+ puts '***Most likely, beanstalk is not running. Start beanstalk, and try running this again.'
29
+ puts "***If you still get this error, check beanstalk is running at, #{string}"
32
30
  else
33
31
  puts e.message
34
32
  puts e.backtrace
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'fileutils'
2
4
  require 'rservicebus2/mq'
3
5
 
@@ -5,18 +7,10 @@ module RServiceBus2
5
7
  # Beanstalk client implementation.
6
8
  class MQFile < MQ
7
9
  # Connect to the broker
8
- # rubocop:disable Metrics/AbcSize,Metrics/MethodLength
9
-
10
+
10
11
  def initialize(uri)
11
- if uri.is_a? URI
12
- @uri = uri
13
- else
14
- puts 'uri must be a valid URI'
15
- abort
16
- end
12
+ super
17
13
 
18
- FileUtils.mkdir_p(@uri.path)
19
- @local_queue_name = RServiceBus2.get_value('APPNAME', 'RServiceBus')
20
14
  FileUtils.mkdir_p("#{@uri.path}/#{@local_queue_name}")
21
15
  @timeout = RServiceBus2.get_value('QUEUE_TIMEOUT', '5').to_i
22
16
  end
@@ -28,24 +22,23 @@ module RServiceBus2
28
22
  end
29
23
 
30
24
  # Get next msg from queue
25
+ # rubocop:disable Metrics/MethodLength
31
26
  def pop
32
27
  time = @timeout
33
- while time > 0
28
+ while time.positive?
34
29
  files = Dir.glob("#{@uri.path}/#{@local_queue_name}/*.msg")
35
- if files.length > 0
36
- @file_path = files[0]
30
+ if files.positive?
31
+ @file_path = files[0]
37
32
  @body = IO.read(@file_path)
38
- return @body
33
+ return @body
39
34
  end
40
- time -= 1
41
- sleep(1)
35
+ time -= 1
36
+ sleep(1)
42
37
  end
43
-
44
- raise NoMsgToProcess if files.length == 0
45
- end
46
38
 
47
- def return_to_queue
39
+ raise NoMsgToProcess if files.length.zero?
48
40
  end
41
+ # rubocop:enable Metrics/MethodLength
49
42
 
50
43
  def ack
51
44
  FileUtils.rm @file_path
@@ -53,7 +46,7 @@ module RServiceBus2
53
46
 
54
47
  def send(queue_name, msg)
55
48
  FileUtils.mkdir_p("#{@uri.path}/#{queue_name}")
56
- IO.write("#{@uri.path}/#{queue_name}/#{rand()}.msg", msg)
49
+ IO.write("#{@uri.path}/#{queue_name}/#{rand}.msg", msg)
57
50
  end
58
51
  end
59
52
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'redis'
2
4
  require 'rservicebus2/mq'
3
5
 
@@ -5,38 +7,31 @@ module RServiceBus2
5
7
  # Redis client implementation.
6
8
  class MQRedis < MQ
7
9
  # Connect to the broker
8
- # rubocop:disable Metrics/AbcSize,Metrics/MethodLength
9
10
  def connect(host, port)
10
11
  port ||= 6379
11
12
  string = "#{host}:#{port}"
12
13
 
13
- begin
14
- @redis = Redis.new(:host => host, :port => port)
15
-
16
- rescue Exception => e
17
- puts e.message
18
- puts 'Error connecting to Redis for mq'
19
- puts "Host string, #{string}"
20
- abort()
21
- end
14
+ @redis = Redis.new(host: host, port: port)
15
+ rescue StandardError => e
16
+ puts e.message
17
+ puts 'Error connecting to Redis for mq'
18
+ puts "Host string, #{string}"
19
+ abort
22
20
  end
23
21
 
24
22
  # Connect to the queue
25
- def subscribe( queuename )
23
+ def subscribe(queuename)
26
24
  @queuename = queuename
27
25
  end
28
26
 
29
27
  # Get next msg from queue
30
28
  def pop
31
- if @redis.llen( @queuename ) == 0 then
29
+ if @redis.llen(@queuename).zero?
32
30
  sleep @timeout
33
- raise NoMsgToProcess.new
31
+ raise NoMsgToProcess
34
32
  end
35
33
 
36
- return @redis.lindex @queuename, 0
37
- end
38
-
39
- def returnToQueue
34
+ @redis.lindex @queuename, 0
40
35
  end
41
36
 
42
37
  # "Commit" queue
@@ -45,9 +40,8 @@ module RServiceBus2
45
40
  end
46
41
 
47
42
  # At least called in the Host rescue block, to ensure all network links are healthy
48
- def send( queueName, msg )
49
- @redis.rpush queueName, msg
43
+ def send(queue_name, msg)
44
+ @redis.rpush queue_name, msg
50
45
  end
51
-
52
46
  end
53
47
  end
@@ -81,7 +81,7 @@ module RServiceBus2
81
81
 
82
82
  return {} if content == ''
83
83
 
84
- YAML.load(content)
84
+ RServiceBus2.safe_load(content)
85
85
  end
86
86
  end
87
87
  end
@@ -16,7 +16,7 @@ module RServiceBus2
16
16
 
17
17
  return [] if content == ''
18
18
 
19
- YAML.load(content)
19
+ RServiceBus2.safe_load(content)
20
20
  end
21
21
 
22
22
  def add(msg)
@@ -61,7 +61,7 @@ module RServiceBus2
61
61
 
62
62
  return {} if content == ''
63
63
 
64
- YAML.load(content)
64
+ RServiceBus2.safe_load(content)
65
65
  end
66
66
  end
67
67
  end
@@ -7,11 +7,11 @@ module RServiceBus2
7
7
  RServiceBus2.log 'Load subscriptions'
8
8
  return {} unless File.exist?(@uri.path)
9
9
 
10
- YAML.load(File.open(@uri.path))
10
+ RServiceBus2.safe_load(File.open(@uri.path))
11
11
  end
12
12
 
13
13
  def add(event_name, queue_name)
14
- s = File.exist?(@uri.path) ? YAML.load(File.open(@uri.path)) : {}
14
+ s = File.exist?(@uri.path) ? RServiceBus2.safe_load(File.open(@uri.path)) : {}
15
15
  s[event_name] = [] if s[event_name].nil?
16
16
 
17
17
  s[event_name] << queue_name
@@ -114,7 +114,7 @@ module RServiceBus2
114
114
  def process
115
115
  # Get the next job from the source queue
116
116
  job = @source.reserve @timeout
117
- msg = YAML.load(job.body)
117
+ msg = RServiceBus2.safe_load(job.body)
118
118
 
119
119
  connect(msg.remote_host_name)
120
120
 
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'csv'
4
+ require 'singleton'
5
+
6
+ # YamlSafeLoader
7
+ class YamlSafeLoader
8
+ include Singleton
9
+
10
+ DEFAULT_PERMITTED_CLASSES = 'RServiceBus2::Message,Time,UUIDTools::UUID,YamlSafeLoader,' \
11
+ 'URI::Generic,URI::RFC3986_Parser,Symbol,Regexp'
12
+
13
+ def init
14
+ return unless @permitted_classes.nil?
15
+
16
+ string = "#{RServiceBus2.get_value('PERMITTED_CLASSES_BASE', DEFAULT_PERMITTED_CLASSES)}," \
17
+ "#{RServiceBus2.get_value('PERMITTED_CLASSES', '')}"
18
+ @permitted_classes = CSV.parse(string)[0].reject { |c| c.to_s.rstrip.empty? }
19
+ end
20
+
21
+ def add_permitted_class(string)
22
+ init
23
+
24
+ @permitted_classes << string
25
+ @permitted_classes.uniq!
26
+ end
27
+
28
+ def load(body)
29
+ init
30
+
31
+ YAML.safe_load(body, permitted_classes: @permitted_classes)
32
+ end
33
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rservicebus2
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.17
4
+ version: 0.2.22
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-05-24 00:00:00.000000000 Z
11
+ date: 2021-06-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: beanstalk-client
@@ -165,6 +165,7 @@ files:
165
165
  - lib/rservicebus2/test/bus.rb
166
166
  - lib/rservicebus2/transporter.rb
167
167
  - lib/rservicebus2/usermessage/withpayload.rb
168
+ - lib/rservicebus2/yaml_safe_loader.rb
168
169
  homepage: http://rubygems.org/gems/rservicebus2
169
170
  licenses:
170
171
  - LGPL-3.0