nypl_ruby_util 0.0.9 → 0.1.1

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: 05c91cd9d8cd4ffd258f3a2c1f9b3cb41c2c6cf9b5df78d21f1483510e34c544
4
- data.tar.gz: 55e61c0097b9721728b147b6201e0856e815c3de8631e3f41ed78b00c12d11bb
3
+ metadata.gz: 653b400784892750f42ddb4202e68fe2935174730b8f655b086e440c1ea2c31e
4
+ data.tar.gz: 215cddb81d35593ebb0cedef604bec3577ba2cac8d9d568bc85f7845157d7421
5
5
  SHA512:
6
- metadata.gz: 6c2042cc492affeb2ae6d60d6c96fb6d0db3ea69c23f1ad6a31da3ff5025c52cd4d5f669112e174153e88b84dde7395f59b23b12182017c079f1ce6a024807f6
7
- data.tar.gz: 832f11059c1267393afb6b2f5140dd5a8f98950f9e69c495e6fa1cfa31af947c78fbf4cd740016214b4fe1dc4bce939a6f57de295039a3673aa195da0ce2be2c
6
+ metadata.gz: 7d7a49c8009dcef64ef7668e182c447214dadf91bebae23f33c4195559286dadb81d01567bd7a60d64ae3aae992f553f204c9e4df7a6bdd0fcb15f65d0b25d45
7
+ data.tar.gz: 7da7c0f9adf0eba65cdf0e653c2663f4ec2b58cc5001b04aec27170fa758bb0601c00b01c1616eab9be82d74cd54c13fe94bbbc78da5836bf15a9d554bc102f5
@@ -5,6 +5,7 @@ require_relative "errors"
5
5
  # Model representing the result message posted to Kinesis stream about everything that has gone on here -- good, bad, or otherwise.
6
6
 
7
7
  class KinesisClient
8
+ #note custom defined :failed_records method
8
9
  attr_reader :config, :avro
9
10
 
10
11
  def initialize(config)
@@ -13,8 +14,8 @@ class KinesisClient
13
14
  @avro = nil
14
15
  @batch_size = @config[:batch_size] || 1
15
16
  @client_options = set_config(config)
16
- @batch_count = 0
17
17
  @records = []
18
+ @failed_records = []
18
19
  @automatically_push = !(@config[:automatically_push] == false)
19
20
  @client = Aws::Kinesis::Client.new(@client_options)
20
21
 
@@ -52,6 +53,7 @@ class KinesisClient
52
53
  end
53
54
 
54
55
  #This method is broken
56
+ #TO DO: figure out how to determine successful or failed record, successful? is not a method on the object
55
57
  def push_record(json_message)
56
58
  record = convert_to_record(json_message)
57
59
  record[:stream_name] = @stream_name
@@ -85,13 +87,10 @@ class KinesisClient
85
87
  records: batch.to_a,
86
88
  stream_name: @stream_name
87
89
  })
88
-
89
90
  if resp.failed_record_count > 0
90
- failure_message = {
91
- failures: resp.failed_record_count,
92
- failures_data: filter_failures(resp)
93
- }
94
- $logger.warn("Batch sent to #{config[:stream_name]} with failures: #{failure_message}")
91
+ failures = filter_failures(resp, batch)
92
+ $logger.warn("Batch sent to #{config[:stream_name]} with #{failures.length} failures: #{failures}")
93
+ failures.each{|failure| @failed_records << failure[:record]}
95
94
  else
96
95
  $logger.info("Batch sent to #{config[:stream_name]} successfully")
97
96
  end
@@ -101,16 +100,26 @@ class KinesisClient
101
100
  if @records.length > 0
102
101
  @records.each_slice(@batch_size) do |slice|
103
102
  push_batch(slice)
104
- @batch_count += 1
105
103
  end
106
104
  @records = []
107
- @batch_count = 0
108
105
  end
109
106
  end
110
107
 
111
- def filter_failures(resp)
108
+ def filter_failures(resp, batch)
112
109
  resp.records.filter_map.with_index do |record, i|
113
- avro.decode(@records[i + @batch_size * @batch_count]) if record.responds_to?(:error_message)
110
+ { record: batch[i], error_message: record.error_message } if record.responds_to?(:error_message)
111
+ end
112
+ end
113
+
114
+ def retry_failed_records
115
+ unless @failed_records.empty?
116
+ @records = @failed_records
117
+ @failed_records = []
118
+ push_records
114
119
  end
115
120
  end
121
+
122
+ def failed_records
123
+ @failed_records.map { |record| avro.decode(record) }
124
+ end
116
125
  end
@@ -21,7 +21,7 @@ class PlatformApiClient
21
21
  @error_options = default_errors.merge(options[:errors] || {})
22
22
  end
23
23
 
24
- def get (path)
24
+ def get (path, transaction_data = {})
25
25
 
26
26
  authenticate! if authenticated
27
27
 
@@ -38,7 +38,7 @@ class PlatformApiClient
38
38
 
39
39
  $logger.debug "Got platform api response", { code: response.code, body: response.body }
40
40
 
41
- parse_json_response response, path
41
+ parse_json_response response, path, transaction_data
42
42
 
43
43
  rescue Exception => e
44
44
  raise StandardError.new(e), "Failed to retrieve #{path} #{e.message}"
@@ -47,12 +47,12 @@ class PlatformApiClient
47
47
 
48
48
  private
49
49
 
50
- def parse_json_response (response, path)
50
+ def parse_json_response (response, path, transaction_data = {})
51
51
  code = response.code.to_i
52
52
  if code < 400
53
53
  JSON.parse(response.body)
54
54
  elsif error_options[code]
55
- instance_exec(response, path, &error_options[code])
55
+ instance_exec(response, path, transaction_data, &error_options[code])
56
56
  else
57
57
  raise "Error interpretting response for path #{path}: (#{response.code}): #{response.body}"
58
58
  {}
@@ -92,12 +92,13 @@ class PlatformApiClient
92
92
 
93
93
  def default_errors
94
94
  {
95
- 401 => lambda do |response, path|
96
- if @try_count < 1
95
+ 401 => lambda do |response, path, transaction_data = {}|
96
+ transaction_data[:try_count] ||= 0
97
+ if transaction_data[:try_count] < 1
97
98
  # Likely an expired access-token; Wipe it for next run
98
- @try_count += 1
99
- access_token = nil
100
- get(path)
99
+ transaction_data[:try_count] += 1
100
+ self.access_token = nil
101
+ get(path, transaction_data)
101
102
  else
102
103
  raise "Error interpretting response for path #{path}: (#{response.code}): #{response.body}"
103
104
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nypl_ruby_util
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.9
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel Appel
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-09-08 00:00:00.000000000 Z
11
+ date: 2022-03-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: avro
@@ -109,7 +109,7 @@ dependencies:
109
109
  - !ruby/object:Gem::Version
110
110
  version: 1.0.3
111
111
  description: A repository of common utilities for NYPL Ruby application
112
- email:
112
+ email:
113
113
  executables: []
114
114
  extensions: []
115
115
  extra_rdoc_files: []
@@ -126,7 +126,7 @@ homepage: https://github.com/NYPL/NYPLRubyUtil
126
126
  licenses:
127
127
  - MIT
128
128
  metadata: {}
129
- post_install_message:
129
+ post_install_message:
130
130
  rdoc_options: []
131
131
  require_paths:
132
132
  - lib
@@ -141,8 +141,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
141
141
  - !ruby/object:Gem::Version
142
142
  version: '0'
143
143
  requirements: []
144
- rubygems_version: 3.1.4
145
- signing_key:
144
+ rubygems_version: 3.1.2
145
+ signing_key:
146
146
  specification_version: 4
147
147
  summary: A repository of common utilities for NYPL Ruby application
148
148
  test_files: []