istox 0.1.156.1 → 0.1.157

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: ff41ac998f85a03dba3cf04311aa1a32a77d74296257a60ff118e45fcf45d850
4
- data.tar.gz: 6d693142d844ac5b4b2cbd749aee72d4d9840ae8d7bb07f638f433423d535fc7
3
+ metadata.gz: f51fbf74138fecbca4e019e075b3c6331cf78d20a5b274c8222c333430e66f68
4
+ data.tar.gz: 68a475a848f0118566ec6ea8bb8369ffb80ad90781441fc01d357bcd49f7ed78
5
5
  SHA512:
6
- metadata.gz: 007a55882ae2e7c0e83fd2ddf9d7a9eb9ce8631fdd9265754e68a4463faebfdf2bebcd62012b1a49f99395b198a456109f20c17a2dd39854a944bc049a1fb035
7
- data.tar.gz: 674b7cdd25bfc8a78416823cc5e9fdc206324c1e08f980a6592ca12a1e8667e833af973834258cd70b4c8c413162aa4a91ba699bdffc591f08fdd345975da08a
6
+ metadata.gz: 07becada18723f399368b68194ec4ae9e5f42184fb33296f3358034920e41859865024a1926a51acbde84eb0f2897c5c40d60e5180ed78c7a65b0d54aed8947b
7
+ data.tar.gz: f9b7aff484d33556e91311e31a0c9a6d4e1166105d80335e6923e9b89511e8fb2873180598ca4e6e06b2e3b871bb43aafcf2c130b2a1d02739be6987b811f307
@@ -1,9 +1,10 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- istox (0.1.156)
4
+ istox (0.1.157)
5
5
  amazing_print
6
6
  awesome_print
7
+ aws-sdk-sns (~> 1)
7
8
  binding_of_caller
8
9
  bunny (>= 2.12.0)
9
10
  exponential-backoff (= 0.0.4)
@@ -63,8 +64,8 @@ GEM
63
64
  i18n (>= 0.7, < 2)
64
65
  minitest (~> 5.1)
65
66
  tzinfo (~> 1.1)
66
- amazing_print (1.2.0)
67
- amq-protocol (2.3.1)
67
+ amazing_print (1.2.1)
68
+ amq-protocol (2.3.2)
68
69
  arel (9.0.0)
69
70
  awesome_print (1.8.0)
70
71
  aws-eventstream (1.1.0)
@@ -74,6 +75,9 @@ GEM
74
75
  aws-partitions (~> 1, >= 1.239.0)
75
76
  aws-sigv4 (~> 1.1)
76
77
  jmespath (~> 1.0)
78
+ aws-sdk-sns (1.24.0)
79
+ aws-sdk-core (~> 3, >= 3.71.0)
80
+ aws-sigv4 (~> 1.1)
77
81
  aws-sdk-xray (1.4.0)
78
82
  aws-sdk-core (~> 3)
79
83
  aws-sigv4 (~> 1.0)
@@ -82,13 +86,14 @@ GEM
82
86
  aws-xray-sdk (0.11.4)
83
87
  aws-sdk-xray (~> 1.4.0)
84
88
  multi_json (~> 1)
89
+ bigdecimal (2.0.0)
85
90
  binding_of_caller (0.8.0)
86
91
  debug_inspector (>= 0.0.1)
87
92
  builder (3.2.4)
88
93
  bullet (5.7.6)
89
94
  activesupport (>= 3.0.0)
90
95
  uniform_notifier (~> 1.11.0)
91
- bunny (2.15.0)
96
+ bunny (2.16.1)
92
97
  amq-protocol (~> 2.3, >= 2.3.1)
93
98
  byebug (11.1.3)
94
99
  coderay (1.1.2)
@@ -115,21 +120,21 @@ GEM
115
120
  ffi (1.13.1)
116
121
  globalid (0.4.2)
117
122
  activesupport (>= 4.2.0)
118
- google-protobuf (3.12.2-universal-darwin)
123
+ google-protobuf (3.13.0-universal-darwin)
119
124
  googleapis-common-protos-types (1.0.5)
120
125
  google-protobuf (~> 3.11)
121
126
  graphlient (0.4.0)
122
127
  faraday (>= 1.0)
123
128
  faraday_middleware
124
129
  graphql-client
125
- graphql (1.10.10)
130
+ graphql (1.11.3)
126
131
  graphql-client (0.16.0)
127
132
  activesupport (>= 3.0)
128
133
  graphql (~> 1.8)
129
- grpc (1.28.0-universal-darwin)
130
- google-protobuf (~> 3.11)
134
+ grpc (1.31.1-universal-darwin)
135
+ google-protobuf (~> 3.12)
131
136
  googleapis-common-protos-types (~> 1.0)
132
- grpc-tools (1.28.0)
137
+ grpc-tools (1.31.1)
133
138
  gruf (2.8.1)
134
139
  activesupport (> 4)
135
140
  concurrent-ruby (> 1)
@@ -166,7 +171,8 @@ GEM
166
171
  nio4r (2.3.1)
167
172
  nokogiri (1.10.9)
168
173
  mini_portile2 (~> 2.4.0)
169
- oj (3.10.6)
174
+ oj (3.10.9)
175
+ bigdecimal (~> 2.0)
170
176
  ougai (1.8.5)
171
177
  oj (~> 3.10)
172
178
  paranoia (2.4.2)
@@ -218,16 +224,16 @@ GEM
218
224
  redis-activesupport (5.2.0)
219
225
  activesupport (>= 3, < 7)
220
226
  redis-store (>= 1.3, < 2)
221
- redis-namespace (1.7.0)
227
+ redis-namespace (1.8.0)
222
228
  redis (>= 3.0.4)
223
- redis-rack (2.1.2)
229
+ redis-rack (2.1.3)
224
230
  rack (>= 2.0.8, < 3)
225
231
  redis-store (>= 1.2, < 2)
226
232
  redis-rails (5.0.2)
227
233
  redis-actionpack (>= 5.0, < 6)
228
234
  redis-activesupport (>= 5.0, < 6)
229
235
  redis-store (>= 1.2, < 2)
230
- redis-store (1.8.2)
236
+ redis-store (1.9.0)
231
237
  redis (>= 4, < 5)
232
238
  rspec (3.8.0)
233
239
  rspec-core (~> 3.8.0)
@@ -250,7 +256,7 @@ GEM
250
256
  rspec-mocks (~> 3.8.0)
251
257
  rspec-support (~> 3.8.0)
252
258
  rspec-support (3.8.0)
253
- slop (4.8.1)
259
+ slop (4.8.2)
254
260
  sprockets (3.7.2)
255
261
  concurrent-ruby (~> 1.0)
256
262
  rack (> 1, < 3)
@@ -265,7 +271,7 @@ GEM
265
271
  tzinfo (1.2.6)
266
272
  thread_safe (~> 0.1)
267
273
  uniform_notifier (1.11.0)
268
- vault (0.14.0)
274
+ vault (0.15.0)
269
275
  aws-sigv4
270
276
  websocket-driver (0.7.0)
271
277
  websocket-extensions (>= 0.1.0)
@@ -32,6 +32,7 @@ Gem::Specification.new do |spec|
32
32
 
33
33
  spec.add_dependency 'amazing_print'
34
34
  spec.add_dependency 'awesome_print'
35
+ spec.add_dependency 'aws-sdk-sns', '~> 1'
35
36
  spec.add_dependency 'binding_of_caller'
36
37
  spec.add_dependency 'bunny', '>= 2.12.0'
37
38
  spec.add_dependency 'exponential-backoff', '0.0.4'
@@ -16,6 +16,7 @@ module Istox
16
16
  require 'istox/helpers/logger'
17
17
  require 'istox/interfaces/chainhub/transaction'
18
18
  require 'istox/helpers/publisher'
19
+ require 'istox/helpers/sns_publisher'
19
20
  require 'istox/helpers/subscriber'
20
21
  require 'istox/helpers/bunny_boot'
21
22
  require 'istox/helpers/rate_limit'
@@ -88,7 +88,7 @@ module Istox
88
88
  end
89
89
 
90
90
  def mark_resource_handled(receipt)
91
- receipt.update(resource_handled: true)
91
+ receipt.reload.update(resource_handled: true)
92
92
  rescue StandardError => e
93
93
  log.warn "Unable to update resource_handled for receipt #{receipt.inspect},
94
94
  but will silently ignore the error"
@@ -276,6 +276,10 @@ module Istox
276
276
  end
277
277
  options_dup.merge!(persistent: persistent)
278
278
  options_dup.merge!(mandatory: mandatory)
279
+
280
+ options_dup[:headers] = {} if options[:headers].nil?
281
+ options_dup[:headers][:sender] = Thread.current.object_id
282
+
279
283
  # message.merge!(locale: I18n.locale)
280
284
 
281
285
  publisher_interceptors.each do |interceptor|
@@ -2,21 +2,23 @@ module Istox
2
2
  class Formatter
3
3
  class << self
4
4
  # format a number, eg. 20000.134 > 20,000.14
5
- def number(input, round_mode: :half_up, precision: 2)
5
+ def number(input, round_mode: :half_up, precision: 2, abs_num: false)
6
6
  BigDecimal.mode(BigDecimal::ROUND_MODE, round_mode)
7
7
 
8
8
  input = 0 if input.blank?
9
9
 
10
10
  input = ::BigDecimal.new(input.to_s).round(precision, round_mode)
11
11
 
12
+ input = input.abs if abs_num
13
+
12
14
  result = format("%.#{precision}f", input)
13
15
 
14
16
  result.to_s.reverse.gsub(/(\d{3})(?=\d)/, '\\1,').reverse
15
17
  end
16
18
 
17
- # format a money, eg. 20000.134 > SGD 20,000.14, position can be :front or :behind
18
- def money(input, round_mode: :half_up, precision: 2, currency:, position: :front)
19
- result = number(input, round_mode: round_mode, precision: precision)
19
+ # format a money, eg. 20000.134 > SGD 20,000.14, position can be :front or :behind, abs_num: whether to absolute the number
20
+ def money(input, round_mode: :half_up, precision: 2, currency:, position: :front, abs_num: false)
21
+ result = number(input, round_mode: round_mode, precision: precision, abs_num: abs_num)
20
22
 
21
23
  return currency + ' ' + result if position == :front
22
24
 
@@ -1,6 +1,9 @@
1
1
  require 'securerandom'
2
2
  require 'istox/helpers/logger'
3
3
 
4
+ class RabbitMQPublishError < StandardError
5
+ end
6
+
4
7
  module Istox
5
8
  # Publisher is relying on BunnyBoot to publish message, please make sure BunnyBoot is initalised properly first during runtime.
6
9
  class Publisher
@@ -174,21 +177,20 @@ module Istox
174
177
  # when an unroutable message is returned, the BasicReturn is fired first and then an ack is sent, firing the BasicAck second.
175
178
  # So if the current status is unroutable then we need to make sure that we don't overwrite that status with Success (ack).
176
179
  ex.on_return do |return_info, properties, content|
177
- return_info = return_info.to_hash
178
- properties = properties.to_hash
180
+ return_info = return_info.to_hash.deep_symbolize_keys
181
+ properties = properties.to_hash.deep_symbolize_keys
179
182
  log.debug "Got a returned message info: #{return_info}"
180
183
  log.debug "Got a returned message properties: #{properties}"
181
184
  log.debug "Got a returned message content: #{content}"
182
- =begin
183
- ::Istox::BunnyBoot.find_trackers("message_id:#{properties[:message_id]}").each do |key|
184
- log.debug("On_return, update key #{key} in redis on status and retry counter")
185
- ::Istox::BunnyBoot.update_tracker_by_status(key, 2)
186
- ::Istox::BunnyBoot.update_tracker_incr_retry(key)
185
+
186
+ if properties[:headers] && properties[:headers][:sender]
187
+ @error = Hash.new if @error.nil?
188
+ tid = properties[:headers][:sender]
189
+ @error[tid] = Hash.new if @error[Thread.current.object_id].nil?
190
+ @error[tid][:return_info] = return_info
191
+ @error[tid][:properties] = properties
192
+ @error[tid][:content] = content
187
193
  end
188
- =end
189
- options = properties.clone
190
- options[:routing_key] = return_info[:routing_key]
191
- republish(ex, options, content)
192
194
  end
193
195
 
194
196
  exchanges[id] = ex
@@ -199,7 +201,7 @@ module Istox
199
201
  end
200
202
 
201
203
  # Handle republish: check and increase retry count
202
- def republish(ex, options = {}, message)
204
+ def republish(ex, options = {}, message, error)
203
205
  options[:headers] = {} if options[:headers].nil?
204
206
 
205
207
  if options[:headers][:republish].nil?
@@ -208,11 +210,12 @@ module Istox
208
210
  else
209
211
  options[:headers][:republish_count] += 1
210
212
  if options[:headers][:republish_count] > 10
211
- log.info "Already retry to publish for 10 times, and give up retry"
213
+ log.fatal "Already retry to publish for 10 times, and give up retry"
212
214
  log.info "Publish options: #{options.inspect}"
213
215
  log.info "Publish to exchange: #{ex.name}"
214
216
  log.info "Publish payload: #{message.inspect}"
215
- return false
217
+
218
+ raise RabbitMQPublishError, "Publish fails after retries(10): #{error}"
216
219
  end
217
220
  end
218
221
 
@@ -245,27 +248,34 @@ module Istox
245
248
  success = ex.channel.wait_for_confirms
246
249
  if success
247
250
  log.debug("Message confirm success: remove message for #{channel_id}")
248
- # ::Istox::BunnyBoot.del_tracker_on_channel channel_id
249
- # republish(ex, options, message)
250
- true
251
+ if @error.nil? || @error[Thread.current.object_id].nil?
252
+ # republish(ex, options, message, 'simulate errors')
253
+ true
254
+ else
255
+ error = @error[Thread.current.object_id][:return_info][:reply_text]
256
+ @error[Thread.current.object_id] = nil
257
+ republish(ex, options, message, error)
258
+ end
251
259
  else
252
260
  ex.channel.nacked_set.each do |n|
253
261
  log.debug("Publish Error: UNACK delivery tag is #{n}, republish message")
254
262
  # options = ::Istox::BunnyBoot.find_tracker_on_channel(channel_id, n, 'options')
255
263
  # options = JSON.parse(options, :symbolize_names => true)
256
264
  # options[:delivery_tag] = n
257
- republish(ex, options, message)
265
+ republish(ex, options, message, 'broker processing fails')
258
266
  end
259
267
  end
260
268
  end
261
269
  rescue Bunny::ConnectionClosedError => e
262
- log.debug "Publish fails due to #{e}"
270
+ log.error "Publish fails due to #{e}"
263
271
  # For network related retry, sleep 2 second before retry
264
272
  # Because it may take some seconds for automatic recovery of network
265
273
  sleep 2
266
- republish(ex,options,message)
274
+ republish(ex, options, message, e.message)
275
+ rescue RabbitMQPublishError => e
276
+ raise RabbitMQPublishError, e.message
267
277
  rescue => e
268
- log.debug "Publish error happening: #{e.message}"
278
+ log.error "Publish error happening: #{e.message}"
269
279
 
270
280
  # If the error indicates that the channel is already closed
271
281
  # then clear hash @channel and @exchange
@@ -277,7 +287,7 @@ module Istox
277
287
  end
278
288
 
279
289
  # Republish msg
280
- republish(ex, options, message)
290
+ republish(ex, options, message, e.message)
281
291
  end
282
292
  end
283
293
  end
@@ -0,0 +1,38 @@
1
+ require 'aws-sdk-sns'
2
+
3
+ module Istox
4
+ class SnsPublisher
5
+ class << self
6
+ # exception will not be thrown if throw_exception is set to false, exception will only be printed out and swallowed
7
+ def publish(message, topic_arn: nil, throw_exception: false)
8
+ if message.nil?
9
+ log.info('No message to publish, skipping SNS publish now...')
10
+ return
11
+ end
12
+
13
+ log.info('Publishing to SNS...')
14
+
15
+ message = JSON.generate(message) unless message.is_a? String
16
+
17
+ sns_client.publish({ topic_arn: topic_arn || ENV.fetch('PUSHER_TOPIC_ARN', ''), message: message })
18
+
19
+ log.info('Publish to SNS successfully.')
20
+ rescue StandardError => e
21
+ log.fatal 'Unable to publish SNS message.'
22
+ log.fatal(e)
23
+ log.info('Failed to publish SNS, ignoring...') unless throw_exception
24
+
25
+ raise e if throw_exception
26
+ end
27
+
28
+ def sns_client
29
+ return @sns_client if @sns_client.present?
30
+
31
+ Aws.config.update({ region: ENV.fetch('AWS_REGION', 'ap-southeast-1'),
32
+ credentials: Aws::Credentials.new('AWS_STS_ACCESS_KEY_ID', 'AWS_STS_SECRET_ACCESS_KEY') })
33
+
34
+ @sns_client = Aws::SNS::Client.new(region: ENV.fetch('AWS_REGION', 'ap-southeast-1'))
35
+ end
36
+ end
37
+ end
38
+ end
@@ -1,3 +1,3 @@
1
1
  module Istox
2
- VERSION = '0.1.156.1'.freeze
2
+ VERSION = '0.1.157'.freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: istox
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.156.1
4
+ version: 0.1.157
5
5
  platform: ruby
6
6
  authors:
7
7
  - Siong Leng
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-07-09 00:00:00.000000000 Z
11
+ date: 2020-08-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: amazing_print
@@ -38,6 +38,20 @@ dependencies:
38
38
  - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: aws-sdk-sns
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1'
41
55
  - !ruby/object:Gem::Dependency
42
56
  name: binding_of_caller
43
57
  requirement: !ruby/object:Gem::Requirement
@@ -509,6 +523,7 @@ files:
509
523
  - lib/istox/helpers/regex_helper.rb
510
524
  - lib/istox/helpers/remote_model_cache.rb
511
525
  - lib/istox/helpers/result_handler.rb
526
+ - lib/istox/helpers/sns_publisher.rb
512
527
  - lib/istox/helpers/subscriber.rb
513
528
  - lib/istox/helpers/vault.rb
514
529
  - lib/istox/helpers/xray/grpc_client_xray_interceptor.rb