istox 0.1.154.2 → 0.1.156.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: dc27873608ff702f609c372920ae7fe0f887c32f7379113c2e22dd0cd1e27d70
4
- data.tar.gz: 9517caed4f791e620781f726fdd4f1952c9b06949d03a681857d185408e757a1
3
+ metadata.gz: ff41ac998f85a03dba3cf04311aa1a32a77d74296257a60ff118e45fcf45d850
4
+ data.tar.gz: 6d693142d844ac5b4b2cbd749aee72d4d9840ae8d7bb07f638f433423d535fc7
5
5
  SHA512:
6
- metadata.gz: 71fe3e68f49caa983c607952552314abc2b40437a4d6b3f4a51fc82135502ae97d5f289db292b1cff2f25761058d9da6f57cf1bf03222ac2ecaf97d949475a1d
7
- data.tar.gz: c564f7093ca3484d6285cf4c77cd98569b5b99f44c27713a127c4e1c4995f37f807ef8d0d8acf2799b06c951bbc05a0625a90d519e5d20ce7c72fa2a4fc21df6
6
+ metadata.gz: 007a55882ae2e7c0e83fd2ddf9d7a9eb9ce8631fdd9265754e68a4463faebfdf2bebcd62012b1a49f99395b198a456109f20c17a2dd39854a944bc049a1fb035
7
+ data.tar.gz: 674b7cdd25bfc8a78416823cc5e9fdc206324c1e08f980a6592ca12a1e8667e833af973834258cd70b4c8c413162aa4a91ba699bdffc591f08fdd345975da08a
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- istox (0.1.154)
4
+ istox (0.1.156)
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,34 @@ 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)
270
279
  # message.merge!(locale: I18n.locale)
271
280
 
272
281
  publisher_interceptors.each do |interceptor|
273
- interceptor.call(message, options)
282
+ interceptor.call(message, options_dup)
274
283
  end
275
284
 
276
285
  message = JSON.dump message
277
- log.debug "Publish options are: #{options}"
286
+ log.debug "Publish options are: #{options_dup}"
278
287
  log.debug "Publish message payload #{message}"
279
- e.publish(message, options)
288
+ e.publish(message, options_dup)
280
289
 
281
290
  options[:message_id]
282
291
  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
@@ -15,6 +15,10 @@ module Istox
15
15
  @interceptors.push(interceptor)
16
16
  end
17
17
 
18
+ def configure_grpc_logger
19
+ Gruf.grpc_logger = log
20
+ end
21
+
18
22
  def call(host_type, service, method, grpc_retries_count: 1, **keyword_args)
19
23
  execute(host_type, service, method, **keyword_args)
20
24
  rescue Gruf::Client::Errors::Unavailable => e
@@ -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,61 @@ 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
178
+ properties = properties.to_hash
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
+ options = properties.clone
190
+ options[:routing_key] = return_info[:routing_key]
191
+ republish(ex, options, content)
188
192
  end
189
193
 
190
194
  exchanges[id] = ex
191
195
  end
192
196
 
193
- def channel_confirm?(ch)
194
- ch.using_publisher_confirmations?
195
- end
196
-
197
197
  def channel_next_tag(ch)
198
198
  ch.next_publish_seq_no
199
199
  end
200
200
 
201
+ # Handle republish: check and increase retry count
202
+ def republish(ex, options = {}, message)
203
+ options[:headers] = {} if options[:headers].nil?
204
+
205
+ if options[:headers][:republish].nil?
206
+ options[:headers][:republish] = true
207
+ options[:headers][:republish_count] = 1
208
+ else
209
+ options[:headers][:republish_count] += 1
210
+ if options[:headers][:republish_count] > 10
211
+ log.info "Already retry to publish for 10 times, and give up retry"
212
+ log.info "Publish options: #{options.inspect}"
213
+ log.info "Publish to exchange: #{ex.name}"
214
+ log.info "Publish payload: #{message.inspect}"
215
+ return false
216
+ end
217
+ end
218
+
219
+ log.info "Retry to publish for <#{options[:headers][:republish_count]}> times"
220
+ do_publish(ex, options, message)
221
+ end
222
+
201
223
  def do_publish(ex, options = {}, message)
202
224
  eid = ::Istox::BunnyBoot.eid ex
203
225
  ch = ex.channel
204
226
  channel_id = ch.id
205
- confirm = channel_confirm? ch
227
+ confirm = ::Istox::BunnyBoot.channel_confirm? ch
206
228
  mode = confirm_mode eid
207
229
 
208
230
  ::Istox::BunnyBoot.publish(ex, message, options)
231
+ =begin
209
232
  if confirm
210
233
  delivery_tag = channel_next_tag ch
211
234
  if options[:message_id].nil?
@@ -216,36 +239,45 @@ module Istox
216
239
  ::Istox::BunnyBoot.rename_tracker channel_id, options[:delivery_tag], delivery_tag
217
240
  end
218
241
  end
242
+ =end
219
243
 
220
244
  if confirm && mode == 0
221
245
  success = ex.channel.wait_for_confirms
222
246
  if success
223
- ::Istox::BunnyBoot.del_tracker_on_channel channel_id
247
+ 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
224
251
  else
225
252
  ex.channel.nacked_set.each do |n|
226
253
  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)
254
+ # options = ::Istox::BunnyBoot.find_tracker_on_channel(channel_id, n, 'options')
255
+ # options = JSON.parse(options, :symbolize_names => true)
256
+ # options[:delivery_tag] = n
257
+ republish(ex, options, message)
231
258
  end
232
259
  end
233
260
  end
234
261
  rescue Bunny::ConnectionClosedError => e
235
262
  log.debug "Publish fails due to #{e}"
236
- sleep 1
237
- do_publish(ex,options,message)
263
+ # For network related retry, sleep 2 second before retry
264
+ # Because it may take some seconds for automatic recovery of network
265
+ sleep 2
266
+ republish(ex,options,message)
238
267
  rescue => e
239
- log.debug "Error happens: #{e.message}"
268
+ log.debug "Publish error happening: #{e.message}"
240
269
 
241
270
  # If the error indicates that the channel is already closed
242
271
  # then clear hash @channel and @exchange
272
+ # re-create channel and re-declare exchange on that channel
243
273
  if e.message.include? "cannot use a closed channel"
244
274
  @channel.delete Thread.current.object_id
245
275
  @exchanges.delete Thread.current.object_id
246
276
  ex = exchange(eid)
247
- do_publish(ex, options, message)
248
277
  end
278
+
279
+ # Republish msg
280
+ republish(ex, options, message)
249
281
  end
250
282
  end
251
283
  end
@@ -1,3 +1,3 @@
1
1
  module Istox
2
- VERSION = '0.1.154.2'.freeze
2
+ VERSION = '0.1.156.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.154.2
4
+ version: 0.1.156.1
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-06-11 00:00:00.000000000 Z
11
+ date: 2020-07-09 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