artery 1.4.2 → 1.4.4

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: 66026b7e5dd81edcbb4fc03956531d781f06387846c23cc6a88c7047da24000d
4
- data.tar.gz: 8177b56b29b22a7c09324c4d5a8915b9c697a73aba69f937d4870f1664a0dcb2
3
+ metadata.gz: aa2d1f9a4366e5ea58b9dcdc0fda7b91101b02ceabd8ca4948619e1c6d4cc0d3
4
+ data.tar.gz: 1d4e5d15fa3a58b6b5d665df071b37045b62d755496d8ab6c542499127dab7ea
5
5
  SHA512:
6
- metadata.gz: e33b8a5fafbf2f56651a85b43636ad62ad68a4e34c219979a171cdf2dcdef1b68372ff17f4f07be256ea1524f5a5b3d999cd8685687ecfdc4eb4afbf7143f40d
7
- data.tar.gz: 39b1c29831cef207fabf34c0c52d8889c4695c0a99ea81f13565538c65a733f4d3aa19cdf5625e4d67a5ef549c02ce3999b19893b696afb5bd5d6493bc4b0ffa
6
+ metadata.gz: 14ae02e57a7a9a5b68349495323375eaa761f7e00f4e23bb9919a09492b6c23de58df05084ecdd26c3ac0c5ece7de32f2fe7ff70a727ba7c5028cbe5f9348d3f
7
+ data.tar.gz: ae17e20112edb94fa148a9cd20c3d23796999a662f6af9edf71118ce84a112e87c7b7d4b1b5330297ae0139732ea558109c93614b9a84c08d1a45221b628f4b9
@@ -20,7 +20,10 @@ module Artery
20
20
  end
21
21
 
22
22
  def latest_index(model)
23
- where(model: model).maximum(:id).to_i
23
+ last_message_id = where(model: model).maximum(:id).to_i
24
+ return last_message_id unless last_message_id.zero?
25
+
26
+ Artery.model_info_class.last_published_id_for(model)
24
27
  end
25
28
 
26
29
  def delete_old
@@ -31,6 +31,10 @@ module Artery
31
31
 
32
32
  row
33
33
  end
34
+
35
+ def self.last_published_id_for(model_name)
36
+ where(model: model_name).limit(1).pluck(:last_published_id).first.to_i
37
+ end
34
38
  end
35
39
  end
36
40
  end
@@ -16,7 +16,9 @@ module Artery
16
16
  client = ::NATS.connect(options)
17
17
 
18
18
  client.on_reconnect do
19
- Artery::Instrumentation.instrument(:connection, state: :reconnected, server: client.connected_server)
19
+ Artery::Instrumentation.instrument(
20
+ :connection, state: :reconnected, server: sanitized_server(client.connected_server)
21
+ )
20
22
  end
21
23
 
22
24
  client.on_disconnect do
@@ -29,7 +31,9 @@ module Artery
29
31
  client
30
32
  end
31
33
 
32
- Artery::Instrumentation.instrument(:connection, state: :connected, server: @client.connected_server)
34
+ Artery::Instrumentation.instrument(
35
+ :connection, state: :connected, server: sanitized_server(@client.connected_server)
36
+ )
33
37
  @client.connect unless @client.connected?
34
38
 
35
39
  @client
@@ -64,6 +68,18 @@ module Artery
64
68
 
65
69
  private
66
70
 
71
+ # Returns server address without credentials, so they never reach instrumentation subscribers (logs).
72
+ def sanitized_server(server)
73
+ return server if server.nil?
74
+
75
+ uri = server.is_a?(URI::Generic) ? server.dup : URI.parse(server.to_s)
76
+ uri.password = nil
77
+ uri.user = nil
78
+ uri
79
+ rescue URI::InvalidURIError
80
+ nil
81
+ end
82
+
67
83
  def options
68
84
  options = {}
69
85
 
@@ -48,7 +48,22 @@ module Artery
48
48
  model ||= artery_model_name
49
49
  action ||= '*'
50
50
 
51
- artery_add_subscription Routing.uri(service: service, model: model, action: action), kwargs, &blk
51
+ artery_add_subscription Routing.uri(service: service, model: model, action: action), **kwargs, client: true,
52
+ &blk
53
+ end
54
+
55
+ def artery_rewind_message_counter!(service: nil) # rubocop:disable Naming/PredicateMethod
56
+ return false if artery_source_model?
57
+
58
+ subscriptions = artery[:subscriptions]&.select do |subscription|
59
+ next false unless subscription.rewindable?
60
+
61
+ service.nil? || subscription.uri.service.to_sym == service.to_sym
62
+ end
63
+ return false if subscriptions.blank?
64
+
65
+ subscriptions.each(&:rewind_message_counter!)
66
+ true
52
67
  end
53
68
 
54
69
  # rubocop:disable Metrics/AbcSize,Metrics/MethodLength,Metrics/CyclomaticComplexity,Metrics/PerceivedComplexity
@@ -97,8 +97,54 @@ module Artery
97
97
  end
98
98
  end
99
99
 
100
+ def rewind_message_counter! # rubocop:disable Naming/PredicateMethod
101
+ return false unless rewindable?
102
+
103
+ local_index = latest_message_index
104
+ return false unless local_index.positive?
105
+
106
+ remote_index = request_publisher_latest_index!
107
+ return false unless remote_index.positive?
108
+ return false if remote_index >= local_index
109
+
110
+ Artery.logger.info(
111
+ "Rewinding #{uri} latest_index from #{local_index} to #{remote_index}"
112
+ )
113
+ info.update!(latest_index: remote_index)
114
+ true
115
+ end
116
+
100
117
  private
101
118
 
119
+ def request_publisher_latest_index!
120
+ route = Routing.uri(
121
+ service: uri.service,
122
+ model: uri.model,
123
+ plural: true,
124
+ action: :get_all
125
+ ).to_route
126
+
127
+ remote_index = nil
128
+
129
+ Artery.request(
130
+ route,
131
+ representation: representation_name,
132
+ scope: synchronize_updates_scope,
133
+ page: 0,
134
+ per_page: 1
135
+ ) do |on|
136
+ on.success do |data|
137
+ remote_index = data[:_index].to_i
138
+ end
139
+
140
+ on.error do |error|
141
+ raise Error, "Failed to fetch artery index for #{uri}: #{error.message}"
142
+ end
143
+ end
144
+
145
+ remote_index.to_i
146
+ end
147
+
102
148
  def run_synchronization_heartbeat
103
149
  return if @heartbeat_thread
104
150
 
@@ -28,6 +28,11 @@ module Artery
28
28
  @info ||= Artery.subscription_info_class.find_for_subscription(self)
29
29
  end
30
30
 
31
+ def reset_info!
32
+ @info = nil
33
+ self
34
+ end
35
+
31
36
  def representation_name
32
37
  options[:representation]
33
38
  end
@@ -40,6 +45,14 @@ module Artery
40
45
  @subscriber.artery[:source]
41
46
  end
42
47
 
48
+ def client?
49
+ options[:client]
50
+ end
51
+
52
+ def rewindable?
53
+ client? && synchronize_updates?
54
+ end
55
+
43
56
  def latest_outgoing_message_index
44
57
  return unless source?
45
58
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Artery
4
- VERSION = '1.4.2'
4
+ VERSION = '1.4.4'
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: artery
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.4.2
4
+ version: 1.4.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sergey Gnuskov