istox 0.1.154.3 → 0.1.156.2

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: 053b63276dabbddf05c55e64702b00ac4b955bd3310ed393cf05735521bdd303
4
- data.tar.gz: cc58992757946c5ba0da3a53c23eb29078f3efb8c6618b731c66e45cc8fdfe8b
3
+ metadata.gz: 5daed90ec2d88ac9c15ab6829e93b1d91ceec8b3bbf0447e18a322bc23782676
4
+ data.tar.gz: 2b952122ec1ecad78de28d15e3bb239d521a7bbfbdf2323dff7407d70538548a
5
5
  SHA512:
6
- metadata.gz: 63b917905c5b587f6c2e6a37cb000a82ccf25966efb4b43d03b1e6395981b12e9502435a261d177e6d2d0262b135ef31ac703966e7c7055c8b6570b8929767f2
7
- data.tar.gz: 8e6278d06d5cd86e1e7d4aa2f385bad894258de48a6c74a9141d4f583a0be4db2494a16d73783320b332337ade2c482364dbd546110c83ef407d32081ae61f9a
6
+ metadata.gz: 848726c06a306e5dfc451769f9392bc9c94716b8959da247e71681d4d932cc8fca2e1f8dd5cdded582c198c7bc53cc96dbd3e636c96943152213df4e1184e886
7
+ data.tar.gz: 717fe201a3068be826b51cfa008a2d9ecb57f2ceb25bcabf470458d0ed841378c695f2add1a2bcf20a74bb7a37a5721ebbcd6b7ef0006c64f1e31c56a470f944
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- istox (0.1.154)
4
+ istox (0.1.156.2)
5
5
  amazing_print
6
6
  awesome_print
7
7
  binding_of_caller
@@ -63,7 +63,7 @@ GEM
63
63
  i18n (>= 0.7, < 2)
64
64
  minitest (~> 5.1)
65
65
  tzinfo (~> 1.1)
66
- amazing_print (1.1.0)
66
+ amazing_print (1.2.0)
67
67
  amq-protocol (2.3.1)
68
68
  arel (9.0.0)
69
69
  awesome_print (1.8.0)
@@ -112,7 +112,7 @@ GEM
112
112
  multipart-post (>= 1.2, < 3)
113
113
  faraday_middleware (1.0.0)
114
114
  faraday (~> 1.0)
115
- ffi (1.13.0)
115
+ ffi (1.13.1)
116
116
  globalid (0.4.2)
117
117
  activesupport (>= 4.2.0)
118
118
  google-protobuf (3.12.2-universal-darwin)
@@ -7,7 +7,8 @@ import os
7
7
 
8
8
  # HOW TO RUN: python bulk-update-version.py <version updating to, eg. 0.1.150.2>
9
9
 
10
- SERVICES = ["client-api", "admin-api", "message-api", "auth", "admin-auth"]
10
+ SERVICES = ["client-api", "admin-api", "message-api",
11
+ "auth", "admin-auth", "banking-api", "trading-api"]
11
12
 
12
13
 
13
14
  def replace(file_path, pattern, subst):
@@ -36,6 +36,7 @@ module Istox
36
36
  require 'istox/helpers/redis_manager'
37
37
  require 'istox/helpers/dlm'
38
38
  require 'istox/helpers/remote_model_cache'
39
+ require 'istox/helpers/formatter'
39
40
 
40
41
  require 'istox/helpers/xray/grpc_client_xray_interceptor'
41
42
  require 'istox/helpers/xray/grpc_server_xray_interceptor'
@@ -56,6 +56,10 @@ module Istox
56
56
  ch
57
57
  end
58
58
 
59
+ def channel_confirm?(ch)
60
+ ch.using_publisher_confirmations?
61
+ end
62
+
59
63
  def exchange(eid)
60
64
  type = data[:exchanges][eid][:type]
61
65
  name = eid
@@ -254,29 +258,38 @@ module Istox
254
258
 
255
259
  def publish(e, message, options = {})
256
260
  eid = eid e
261
+ # By default:
262
+ # For persistence, if exchange is durable, persistent is enabled
263
+ # For mandatory. if channel is confirmed mode, mandatory is enabled
257
264
  persistent = e.durable?
258
- mandatory = false
265
+ mandatory = channel_confirm? e.channel
266
+ options_dup = options.clone
267
+
259
268
  # Set Mandatory & Persistent flag for non-DLX and non-manual msg
260
- unless %w[dlx manual].include? options[:type]
261
- if options[:routing_key].present?
269
+ unless %w[dlx manual].include? options_dup[:type]
270
+ if options_dup[:routing_key].present?
262
271
  v1 = data['publish'][eid]
263
- v1 = v1[options[:routing_key]] unless v1.nil?
264
- persistent = v1['persistent'] unless v1.nil?
265
- mandatory = v1['mandatory'] unless v1.nil?
272
+ v1 = v1[options_dup[:routing_key]] unless v1.nil? || v1[options_dup[:routing_key]].nil?
273
+ persistent = v1['persistent'] unless v1.nil? || v1['persistent'].nil?
274
+ mandatory = v1['mandatory'] unless v1.nil? || v1['mandatory'].nil?
266
275
  end
267
276
  end
268
- options.merge!(persistent: persistent)
269
- options.merge!(mandatory: mandatory)
277
+ options_dup.merge!(persistent: persistent)
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
+
270
283
  # message.merge!(locale: I18n.locale)
271
284
 
272
285
  publisher_interceptors.each do |interceptor|
273
- interceptor.call(message, options)
286
+ interceptor.call(message, options_dup)
274
287
  end
275
288
 
276
289
  message = JSON.dump message
277
- log.debug "Publish options are: #{options}"
290
+ log.debug "Publish options are: #{options_dup}"
278
291
  log.debug "Publish message payload #{message}"
279
- e.publish(message, options)
292
+ e.publish(message, options_dup)
280
293
 
281
294
  options[:message_id]
282
295
  end
@@ -0,0 +1,27 @@
1
+ module Istox
2
+ class Formatter
3
+ class << self
4
+ # format a number, eg. 20000.134 > 20,000.14
5
+ def number(input, round_mode: :half_up, precision: 2)
6
+ BigDecimal.mode(BigDecimal::ROUND_MODE, round_mode)
7
+
8
+ input = 0 if input.blank?
9
+
10
+ input = ::BigDecimal.new(input.to_s).round(precision, round_mode)
11
+
12
+ result = format("%.#{precision}f", input)
13
+
14
+ result.to_s.reverse.gsub(/(\d{3})(?=\d)/, '\\1,').reverse
15
+ end
16
+
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)
20
+
21
+ return currency + ' ' + result if position == :front
22
+
23
+ result + ' ' + currency
24
+ end
25
+ end
26
+ end
27
+ end
@@ -104,7 +104,7 @@ module Istox
104
104
  def channel
105
105
  return @channel[Thread.current.object_id] if @channel.present? && @channel[Thread.current.object_id].present?
106
106
 
107
- log.info "#{Thread.current.object_id} No channel yet, create 2 channels confirm-mode and non-confirm-mode ... ..."
107
+ log.info "Thread<#{Thread.current.object_id}> No channel yet, create 3 channels confirm-mode and non-confirm-mode ... ..."
108
108
  @channel = Hash.new if @channel.nil?
109
109
  @channel[Thread.current.object_id] = Hash.new
110
110
  @channel[Thread.current.object_id]['confirm-0'] = ::Istox::BunnyBoot.channel(connection, confirm: true)
@@ -174,38 +174,66 @@ module Istox
174
174
  # when an unroutable message is returned, the BasicReturn is fired first and then an ack is sent, firing the BasicAck second.
175
175
  # So if the current status is unroutable then we need to make sure that we don't overwrite that status with Success (ack).
176
176
  ex.on_return do |return_info, properties, content|
177
- # log.debug "Got a returned message info: #{return_info}"
178
- # log.debug "Got a returned message properties: #{properties}"
179
- # log.debug "Got a returned message content: #{content}"
180
-
181
- # ::Istox::BunnyBoot.find_trackers("message_id:#{properties[:message_id]}").each do |key|
182
- # log.debug("On_return, update key #{key} in redis on status and retry counter")
183
- # ::Istox::BunnyBoot.update_tracker_by_status(key, 2)
184
- # ::Istox::BunnyBoot.update_tracker_incr_retry(key)
185
- # end
186
-
187
- # publish(return_info[:exchange], return_info[:routing_key], content)
177
+ return_info = return_info.to_hash.deep_symbolize_keys
178
+ properties = properties.to_hash.deep_symbolize_keys
179
+ log.debug "Got a returned message info: #{return_info}"
180
+ log.debug "Got a returned message properties: #{properties}"
181
+ 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
189
+ if properties[:headers] && properties[:headers][:sender]
190
+ @error = Hash.new if @error.nil?
191
+ tid = properties[:headers][:sender]
192
+ @error[tid] = Hash.new if @error[Thread.current.object_id].nil?
193
+ @error[tid][:return_info] = return_info
194
+ @error[tid][:properties] = properties
195
+ @error[tid][:content] = content
196
+ end
188
197
  end
189
198
 
190
199
  exchanges[id] = ex
191
200
  end
192
201
 
193
- def channel_confirm?(ch)
194
- ch.using_publisher_confirmations?
195
- end
196
-
197
202
  def channel_next_tag(ch)
198
203
  ch.next_publish_seq_no
199
204
  end
200
205
 
206
+ # Handle republish: check and increase retry count
207
+ def republish(ex, options = {}, message)
208
+ options[:headers] = {} if options[:headers].nil?
209
+
210
+ if options[:headers][:republish].nil?
211
+ options[:headers][:republish] = true
212
+ options[:headers][:republish_count] = 1
213
+ else
214
+ options[:headers][:republish_count] += 1
215
+ if options[:headers][:republish_count] > 10
216
+ log.info "Already retry to publish for 10 times, and give up retry"
217
+ log.info "Publish options: #{options.inspect}"
218
+ log.info "Publish to exchange: #{ex.name}"
219
+ log.info "Publish payload: #{message.inspect}"
220
+ return false
221
+ end
222
+ end
223
+
224
+ log.info "Retry to publish for <#{options[:headers][:republish_count]}> times"
225
+ do_publish(ex, options, message)
226
+ end
227
+
201
228
  def do_publish(ex, options = {}, message)
202
229
  eid = ::Istox::BunnyBoot.eid ex
203
230
  ch = ex.channel
204
231
  channel_id = ch.id
205
- confirm = channel_confirm? ch
232
+ confirm = ::Istox::BunnyBoot.channel_confirm? ch
206
233
  mode = confirm_mode eid
207
234
 
208
235
  ::Istox::BunnyBoot.publish(ex, message, options)
236
+ =begin
209
237
  if confirm
210
238
  delivery_tag = channel_next_tag ch
211
239
  if options[:message_id].nil?
@@ -216,36 +244,50 @@ module Istox
216
244
  ::Istox::BunnyBoot.rename_tracker channel_id, options[:delivery_tag], delivery_tag
217
245
  end
218
246
  end
247
+ =end
219
248
 
220
249
  if confirm && mode == 0
221
250
  success = ex.channel.wait_for_confirms
222
251
  if success
223
- ::Istox::BunnyBoot.del_tracker_on_channel channel_id
252
+ 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
+ if @error.nil? || @error[Thread.current.object_id].nil?
256
+ true
257
+ else
258
+ @error[Thread.current.object_id] = nil
259
+ republish(ex,options,message)
260
+ end
224
261
  else
225
262
  ex.channel.nacked_set.each do |n|
226
263
  log.debug("Publish Error: UNACK delivery tag is #{n}, republish message")
227
- options = ::Istox::BunnyBoot.find_tracker_on_channel(channel_id, n, 'options')
228
- options = JSON.parse(options, :symbolize_names => true)
229
- options[:delivery_tag] = n
230
- do_publish(ex, options, message)
264
+ # options = ::Istox::BunnyBoot.find_tracker_on_channel(channel_id, n, 'options')
265
+ # options = JSON.parse(options, :symbolize_names => true)
266
+ # options[:delivery_tag] = n
267
+ republish(ex, options, message)
231
268
  end
232
269
  end
233
270
  end
234
271
  rescue Bunny::ConnectionClosedError => e
235
272
  log.debug "Publish fails due to #{e}"
236
- sleep 1
237
- do_publish(ex,options,message)
273
+ # For network related retry, sleep 2 second before retry
274
+ # Because it may take some seconds for automatic recovery of network
275
+ sleep 2
276
+ republish(ex,options,message)
238
277
  rescue => e
239
- log.debug "Error happens: #{e.message}"
278
+ log.debug "Publish error happening: #{e.message}"
240
279
 
241
280
  # If the error indicates that the channel is already closed
242
281
  # then clear hash @channel and @exchange
282
+ # re-create channel and re-declare exchange on that channel
243
283
  if e.message.include? "cannot use a closed channel"
244
284
  @channel.delete Thread.current.object_id
245
285
  @exchanges.delete Thread.current.object_id
246
286
  ex = exchange(eid)
247
- do_publish(ex, options, message)
248
287
  end
288
+
289
+ # Republish msg
290
+ republish(ex, options, message)
249
291
  end
250
292
  end
251
293
  end
@@ -1,3 +1,3 @@
1
1
  module Istox
2
- VERSION = '0.1.154.3'.freeze
2
+ VERSION = '0.1.156.2'.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.154.3
4
+ version: 0.1.156.2
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-06-17 00:00:00.000000000 Z
11
+ date: 2020-07-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: amazing_print
@@ -493,6 +493,7 @@ files:
493
493
  - lib/istox/helpers/common_helper.rb
494
494
  - lib/istox/helpers/dlm.rb
495
495
  - lib/istox/helpers/f_math.rb
496
+ - lib/istox/helpers/formatter.rb
496
497
  - lib/istox/helpers/graphql_client.rb
497
498
  - lib/istox/helpers/grpc_client.rb
498
499
  - lib/istox/helpers/gruf_listener_hook.rb
@@ -523,7 +524,7 @@ files:
523
524
  homepage: http://www.abc.com
524
525
  licenses: []
525
526
  metadata: {}
526
- post_install_message:
527
+ post_install_message:
527
528
  rdoc_options: []
528
529
  require_paths:
529
530
  - lib
@@ -539,7 +540,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
539
540
  version: '0'
540
541
  requirements: []
541
542
  rubygems_version: 3.0.8
542
- signing_key:
543
+ signing_key:
543
544
  specification_version: 4
544
545
  summary: istox backend shared gem
545
546
  test_files: []