nypl_ruby_util 0.0.6 → 0.0.7
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 +4 -4
- data/lib/kinesis_client.rb +72 -18
- data/lib/nypl_avro.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5ff3e8b400c7989c57e1398a642631a8c415a1bf982feadef5e95a813d7ad971
|
4
|
+
data.tar.gz: 7818da3d0d4fc85ddd6f10b259375463bc23a6f17b34a57026d60b4d1d03f057
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a2a0a99e281a95583fbe84f69fdbd066556f911167527be86dbdea2cffda000d3bb132edb6c90da4846e2b4d8129f7939594ee57b1a646f167955b2d5d1ed324
|
7
|
+
data.tar.gz: 0126f82acca76fd1e6c77afecce13a30c500766fd57f1ebecba075fcf85961c5c4b1c4e01d1641803cb23d86a9adadd21aed2f01d6e174f182b364e30dcc8fc6
|
data/lib/kinesis_client.rb
CHANGED
@@ -9,40 +9,94 @@ class KinesisClient
|
|
9
9
|
|
10
10
|
def initialize(config)
|
11
11
|
@config = config
|
12
|
+
@stream_name = @config[:stream_name]
|
12
13
|
@avro = nil
|
14
|
+
@batch_size = @config[:batch_size] || 1
|
15
|
+
@batch = []
|
16
|
+
@automatically_push = @config[:automatically_push] == false ? false : true
|
17
|
+
@client_options = config[:profile] ? { profile: config[:profile] } : {}
|
18
|
+
@client = Aws::Kinesis::Client.new @client_options
|
13
19
|
|
14
20
|
if config[:schema_string]
|
15
21
|
@avro = NYPLAvro.by_name(config[:schema_string])
|
16
22
|
end
|
17
23
|
|
24
|
+
@shovel_method = @batch_size > 1 ? :push_to_batch : :push_record
|
25
|
+
|
18
26
|
end
|
19
27
|
|
20
|
-
def
|
28
|
+
def convert_to_record(json_message)
|
21
29
|
if config[:schema_string]
|
22
30
|
message = avro.encode(json_message, false)
|
23
31
|
else
|
24
32
|
message = json_message
|
25
33
|
end
|
26
34
|
|
27
|
-
client = Aws::Kinesis::Client.new
|
28
35
|
partition_key = (config[:partition_key] ? json_message[config[:partition_key]] : SecureRandom.hex(20)).hash.to_s
|
29
|
-
|
30
|
-
resp = client.put_record({
|
31
|
-
stream_name: config[:stream_name],
|
36
|
+
{
|
32
37
|
data: message,
|
33
38
|
partition_key: partition_key
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
39
|
+
}
|
40
|
+
|
41
|
+
end
|
42
|
+
|
43
|
+
def <<(json_message)
|
44
|
+
send(@shovel_method, json_message)
|
45
|
+
end
|
46
|
+
|
47
|
+
def push_record(json_message)
|
48
|
+
record = convert_to_record json_message
|
49
|
+
record[:stream_name] = @stream_name
|
50
|
+
|
51
|
+
@client.put_record record
|
52
|
+
|
53
|
+
return_hash = {}
|
54
|
+
|
55
|
+
if resp.successful?
|
56
|
+
return_hash["code"] = "200"
|
57
|
+
return_hash["message"] = json_message, resp
|
58
|
+
$logger.info("Message sent to #{config[:stream_name]} #{json_message}, #{resp}") if $logger
|
59
|
+
else
|
60
|
+
$logger.error("message" => "FAILED to send message to HoldRequestResult #{json_message}, #{resp}.") if $logger
|
61
|
+
raise NYPLError.new json_message, resp
|
62
|
+
end
|
63
|
+
return_hash
|
64
|
+
end
|
65
|
+
|
66
|
+
def push_to_batch(json_message)
|
67
|
+
begin
|
68
|
+
@batch << convert_to_record(json_message)
|
69
|
+
rescue AvroError => e
|
70
|
+
$logger.error("message" => "Avro encoding error #{e.message} for #{json_message}")
|
71
|
+
end
|
72
|
+
if @automatically_push && @batch.length >= @batch_size
|
73
|
+
push_records
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
def push_batch(batch)
|
78
|
+
resp = @client.put_records({
|
79
|
+
records: batch.to_a,
|
80
|
+
stream_name: @stream_name
|
81
|
+
})
|
82
|
+
|
83
|
+
$logger.debug("Received #{resp} from #{@stream_name}")
|
84
|
+
|
85
|
+
return_message = {
|
86
|
+
failures: resp.failed_record_count,
|
87
|
+
error_messages: resp.records.map {|record| record.error_message }.compact
|
88
|
+
}
|
89
|
+
|
90
|
+
$logger.info("Message sent to #{config[:stream_name]} #{return_message}") if $logger
|
91
|
+
|
92
|
+
return {
|
93
|
+
"code": "200",
|
94
|
+
"message": return_message.to_json
|
95
|
+
}
|
96
|
+
end
|
97
|
+
|
98
|
+
def push_records
|
99
|
+
@batch.each_slice(@batch_size) {|slice| push_batch slice}
|
100
|
+
@batch = []
|
47
101
|
end
|
48
102
|
end
|
data/lib/nypl_avro.rb
CHANGED
@@ -38,7 +38,7 @@ class NYPLAvro
|
|
38
38
|
begin
|
39
39
|
bin_encoder.write(decoded_data, encoder)
|
40
40
|
rescue Avro::IO::AvroTypeError => e
|
41
|
-
raise AvroError.new(e), "Error encoding data using #{@schema.name} schema due to #{e.message}"
|
41
|
+
raise AvroError.new(e), "Error encoding data #{decoded_data} using #{@schema.name} schema due to #{e.message}"
|
42
42
|
end
|
43
43
|
|
44
44
|
buffer.rewind
|