rservicebus2 0.2.17 → 0.2.18

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: 70e06273be45dd1c4fb2c5611962b1b2f0bd13f8bbf1dd6f19e1f0efaac3d26f
4
+ data.tar.gz: d9efd6d11e1162a8f6d17a214d4d7e2c4d2cd9f1501a14f160ac0e9e79477b09
5
5
  SHA512:
6
- metadata.gz: e68d15cc10acdb2565ab02d6d8cfd3dc8b1a871becfe9a6b66f319d763526c16df8977456aadec9b5c2cc66595082bfde320a02458e384b2753b7be68cc01726
7
- data.tar.gz: f1c1c5ea85bf0920206067b92b58544068084b0e631c29ce6fdb00783114f87e507d59d3f2c237d1b307a9b3df7430deaf8ef74bf020ca6874c029801bfe99b0
6
+ metadata.gz: a8b7715cf19a079210e4a8d07eba4046d3742012b271c2642c4c116309fec30cf32031058dba3788081295857a51adff0fac18eb72b24df49266fed1261cce4e
7
+ data.tar.gz: a063b4320249df6cfa2ed2fad0b9bccf33581920db0b44434c5ad4de499b34d074cb535c043ed3d3cde459482fa6f78aa09084c5b241220a46f9e2ee9079f63a
@@ -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
@@ -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
@@ -91,8 +97,8 @@ module RServiceBus2
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,21 +120,30 @@ 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
149
  RServiceBus2.log "Processed #{file_processed} of #{file_list.length}.\nAllow system tick #{self.class.name}"
@@ -9,67 +9,50 @@ 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 validate_directory(path, param_name)
13
+ open_folder path
14
+ return if File.writable?(path)
32
15
 
33
- @path = uri.path
16
+ puts "***** #{param_name} is not writable, #{path}.\n
17
+ ***** Make the directory, #{path}, writable and try again."
18
+ abort
19
+ rescue Errno::ENOENT
20
+ puts "***** #{param_name} does not exist, #{path}.\n" \
21
+ "***** Create the directory, #{path}, and try again.\n" \
22
+ "***** eg, mkdir #{path}"
23
+ abort
24
+ rescue Errno::ENOTDIR
25
+ puts "***** The specified path does not point to a directory, #{path}.
26
+ ***** Either repoint path to a directory, or remove, #{path}, and create it as a directory.
27
+ ***** eg, rm #{path} && mkdir #{path}"
28
+ abort
29
+ end
34
30
 
31
+ def validate_processing_directory(uri)
35
32
  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*"
33
+ puts '***** Processing Directory is not specified.' \
34
+ '***** Specify the Processing Directory as a query string in the Path URI' \
35
+ "***** eg, '/#{uri.path}?processing=*ProcessingDir*'"
40
36
  abort
41
- else
42
- parts = CGI.parse(uri.query)
37
+ end
43
38
 
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
39
+ parts = CGI.parse(uri.query)
40
+ return unless parts.key? 'processing'
66
41
 
67
- @processing_folder = processing_uri.path
68
- end
42
+ processing_uri = URI.parse parts['processing'][0]
43
+ validate_directory processing_uri.path, 'Processing Directory'
44
+ @processing_folder = processing_uri.path
45
+ end
69
46
 
70
- @filter = '*'
71
- @filter = parts['filter'][0] if parts.key? 'filter'
72
- end
47
+ def connect(uri)
48
+ # Pass the path through the Dir object to check syntax on startup
49
+
50
+ validate_directory uri, 'Directory'
51
+ @path = uri.path
52
+ validate_processing_directory(uri)
53
+
54
+ @filter = '*'
55
+ @filter = parts['filter'][0] if parts.key? 'filter'
73
56
  end
74
57
 
75
58
  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,
@@ -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
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.18
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-05-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: beanstalk-client