istox 0.1.156.2 → 0.1.157.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,9 +1,10 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- istox (0.1.156.2)
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"
@@ -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
@@ -179,13 +182,7 @@ module Istox
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)
187
- end
188
- =end
185
+
189
186
  if properties[:headers] && properties[:headers][:sender]
190
187
  @error = Hash.new if @error.nil?
191
188
  tid = properties[:headers][:sender]
@@ -204,7 +201,7 @@ module Istox
204
201
  end
205
202
 
206
203
  # Handle republish: check and increase retry count
207
- def republish(ex, options = {}, message)
204
+ def republish(ex, options = {}, message, error)
208
205
  options[:headers] = {} if options[:headers].nil?
209
206
 
210
207
  if options[:headers][:republish].nil?
@@ -213,11 +210,12 @@ module Istox
213
210
  else
214
211
  options[:headers][:republish_count] += 1
215
212
  if options[:headers][:republish_count] > 10
216
- 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"
217
214
  log.info "Publish options: #{options.inspect}"
218
215
  log.info "Publish to exchange: #{ex.name}"
219
216
  log.info "Publish payload: #{message.inspect}"
220
- return false
217
+
218
+ raise RabbitMQPublishError, "Publish fails after retries(10): #{error}"
221
219
  end
222
220
  end
223
221
 
@@ -250,13 +248,13 @@ module Istox
250
248
  success = ex.channel.wait_for_confirms
251
249
  if success
252
250
  log.debug("Message confirm success: remove message for #{channel_id}")
253
- # ::Istox::BunnyBoot.del_tracker_on_channel channel_id
254
- # republish(ex, options, message)
255
251
  if @error.nil? || @error[Thread.current.object_id].nil?
252
+ # republish(ex, options, message, 'simulate errors')
256
253
  true
257
254
  else
255
+ error = @error[Thread.current.object_id][:return_info][:reply_text]
258
256
  @error[Thread.current.object_id] = nil
259
- republish(ex,options,message)
257
+ republish(ex, options, message, error)
260
258
  end
261
259
  else
262
260
  ex.channel.nacked_set.each do |n|
@@ -264,18 +262,20 @@ module Istox
264
262
  # options = ::Istox::BunnyBoot.find_tracker_on_channel(channel_id, n, 'options')
265
263
  # options = JSON.parse(options, :symbolize_names => true)
266
264
  # options[:delivery_tag] = n
267
- republish(ex, options, message)
265
+ republish(ex, options, message, 'broker processing fails')
268
266
  end
269
267
  end
270
268
  end
271
269
  rescue Bunny::ConnectionClosedError => e
272
- log.debug "Publish fails due to #{e}"
270
+ log.error "Publish fails due to #{e}"
273
271
  # For network related retry, sleep 2 second before retry
274
272
  # Because it may take some seconds for automatic recovery of network
275
273
  sleep 2
276
- republish(ex,options,message)
274
+ republish(ex, options, message, e.message)
275
+ rescue RabbitMQPublishError => e
276
+ raise RabbitMQPublishError, e.message
277
277
  rescue => e
278
- log.debug "Publish error happening: #{e.message}"
278
+ log.error "Publish error happening: #{e.message}"
279
279
 
280
280
  # If the error indicates that the channel is already closed
281
281
  # then clear hash @channel and @exchange
@@ -287,7 +287,7 @@ module Istox
287
287
  end
288
288
 
289
289
  # Republish msg
290
- republish(ex, options, message)
290
+ republish(ex, options, message, e.message)
291
291
  end
292
292
  end
293
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(ENV.fetch('AWS_STS_ACCESS_KEY_ID'), ENV.fetch('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.2'.freeze
2
+ VERSION = '0.1.157.1'.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.2
4
+ version: 0.1.157.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Siong Leng
8
- autorequire:
8
+ autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-07-15 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
@@ -524,7 +539,7 @@ files:
524
539
  homepage: http://www.abc.com
525
540
  licenses: []
526
541
  metadata: {}
527
- post_install_message:
542
+ post_install_message:
528
543
  rdoc_options: []
529
544
  require_paths:
530
545
  - lib
@@ -539,8 +554,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
539
554
  - !ruby/object:Gem::Version
540
555
  version: '0'
541
556
  requirements: []
542
- rubygems_version: 3.0.8
543
- signing_key:
557
+ rubygems_version: 3.0.6
558
+ signing_key:
544
559
  specification_version: 4
545
560
  summary: istox backend shared gem
546
561
  test_files: []