kafkr 0.5.6 → 0.9.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/exe/kafkr +3 -1
- data/exe/kafkr-consumer +3 -3
- data/exe/kafkr-producer +1 -1
- data/lib/kafkr/consumer.rb +23 -23
- data/lib/kafkr/log.rb +14 -3
- data/lib/kafkr/producer.rb +19 -13
- data/lib/kafkr/version.rb +1 -1
- data/lib/kafkr.rb +1 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c15f14feed8ee398816eb4454850798f764f10cafdf12f27aca353ac2412fa1b
|
4
|
+
data.tar.gz: 77c4facf9f2290d087d2b5b40aa63714e0c01f5d0f4a33cce4c85349264dfe63
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3e1089f1fbe70c98be8c9ec09fe9d9967c20594c120857fa486bde44bb3ab2ef32cb02df7ae485e383ac1d802243a9468019bf9e6bfd6e057ce57fe14e465e4a
|
7
|
+
data.tar.gz: e929ce1c00207dd3f63be0294f73d4b885a3786a0748e84650baedd3a01e22e9755de5acf85d111d8e0ea29ce0ba1d4e51906f5a5228c0b5b2acbb3de56857a8
|
data/exe/kafkr
CHANGED
@@ -1,4 +1,6 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
|
+
require_relative "../lib/kafkr.rb"
|
3
|
+
|
2
4
|
PORT = ENV["KAFKR_PORT"] || 4000
|
3
5
|
|
4
6
|
begin
|
@@ -10,7 +12,7 @@ end
|
|
10
12
|
|
11
13
|
begin
|
12
14
|
server = Kafkr::Log.new(PORT.to_i)
|
13
|
-
puts "Log started on port #{PORT}!"
|
15
|
+
puts "Kafkr Log version #{Kafkr::VERSION} started on port #{PORT}!"
|
14
16
|
server.start
|
15
17
|
rescue => e
|
16
18
|
puts "An error occurred: #{e.message}"
|
data/exe/kafkr-consumer
CHANGED
@@ -33,7 +33,7 @@ def list_registered_handlers
|
|
33
33
|
end
|
34
34
|
end
|
35
35
|
|
36
|
-
def start_consumer(port,host)
|
36
|
+
def start_consumer(port, host)
|
37
37
|
puts "Starting consumer on port #{port}!"
|
38
38
|
$handlers_changed = false
|
39
39
|
|
@@ -95,13 +95,13 @@ end
|
|
95
95
|
|
96
96
|
file_checksums = {}
|
97
97
|
monitoring_thread = Thread.new { monitor_handlers(file_checksums) }
|
98
|
-
start_consumer(port,host) # Pass the port here
|
98
|
+
start_consumer(port, host) # Pass the port here
|
99
99
|
|
100
100
|
begin
|
101
101
|
loop do
|
102
102
|
if $restart_required
|
103
103
|
stop_consumer
|
104
|
-
start_consumer(port,host)
|
104
|
+
start_consumer(port, host)
|
105
105
|
$restart_required = false
|
106
106
|
end
|
107
107
|
sleep 1
|
data/exe/kafkr-producer
CHANGED
data/lib/kafkr/consumer.rb
CHANGED
@@ -18,6 +18,7 @@ module Kafkr
|
|
18
18
|
def configuration
|
19
19
|
FileUtils.mkdir_p "./.kafkr"
|
20
20
|
@configuration ||= OpenStruct.new
|
21
|
+
@configuration.suggest_handlers = false
|
21
22
|
@configuration
|
22
23
|
end
|
23
24
|
|
@@ -72,13 +73,12 @@ module Kafkr
|
|
72
73
|
protected
|
73
74
|
|
74
75
|
def reply to:, payload:
|
75
|
-
|
76
76
|
Kafkr::Producer.configure do |config|
|
77
77
|
config.host = Consumer.configuration.host
|
78
78
|
config.port = Consumer.configuration.port
|
79
79
|
end
|
80
80
|
|
81
|
-
Kafkr::Producer.send_message({reply: {payload: payload, uuid: to[
|
81
|
+
Kafkr::Producer.send_message({reply: {payload: payload, uuid: to["sync_uid"]}}, acknowledge: false)
|
82
82
|
end
|
83
83
|
|
84
84
|
private
|
@@ -134,27 +134,29 @@ module Kafkr
|
|
134
134
|
return
|
135
135
|
end
|
136
136
|
|
137
|
-
if
|
138
|
-
|
139
|
-
|
137
|
+
if Kafkr::Consumer.configuration.suggest_handlers
|
138
|
+
if valid_class_name? name.capitalize
|
139
|
+
puts "No handler for this message, you could use this one."
|
140
|
+
puts ""
|
140
141
|
|
141
|
-
|
142
|
+
handler_class_string = <<~HANDLER_CLASS
|
142
143
|
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
144
|
+
class #{name.capitalize}Handler < Kafkr::Consumer::Handler
|
145
|
+
def handle?(message)
|
146
|
+
can_handle? message, '#{name}'
|
147
|
+
end
|
147
148
|
|
148
|
-
|
149
|
-
|
149
|
+
def handle(message)
|
150
|
+
puts message
|
151
|
+
end
|
150
152
|
end
|
151
|
-
end
|
152
153
|
|
153
|
-
|
154
|
+
save the file to ./handlers/#{name}_handler.rb
|
154
155
|
|
155
|
-
|
156
|
+
HANDLER_CLASS
|
156
157
|
|
157
|
-
|
158
|
+
puts handler_class_string
|
159
|
+
end
|
158
160
|
end
|
159
161
|
end
|
160
162
|
|
@@ -163,8 +165,7 @@ module Kafkr
|
|
163
165
|
def listen_for(message, send_message)
|
164
166
|
attempt = 0
|
165
167
|
begin
|
166
|
-
socket = TCPSocket.new(
|
167
|
-
puts "Connected to server. #{Consumer.configuration.host} #{Consumer.configuration.port} " if attempt == 0
|
168
|
+
socket = TCPSocket.new(Consumer.configuration.host, Consumer.configuration.port)
|
168
169
|
attempt = 0
|
169
170
|
|
170
171
|
Timeout.timeout(20) do
|
@@ -175,11 +176,11 @@ module Kafkr
|
|
175
176
|
received_message = socket.gets
|
176
177
|
raise LostConnection if received_message.nil?
|
177
178
|
# Assuming Kafkr::Encryptor is defined elsewhere
|
178
|
-
received_message = Kafkr::Encryptor.new.decrypt(received_message.chomp)
|
179
|
+
received_message = Kafkr::Encryptor.new.decrypt(received_message.chomp)
|
179
180
|
# Yield every received message to the given block
|
180
181
|
if valid_json?(received_message)
|
181
|
-
|
182
|
-
|
182
|
+
payload = yield JSON.parse(received_message), sync_uid if block_given?
|
183
|
+
return payload if payload
|
183
184
|
end
|
184
185
|
end
|
185
186
|
end
|
@@ -206,8 +207,7 @@ module Kafkr
|
|
206
207
|
def listen
|
207
208
|
attempt = 0
|
208
209
|
loop do
|
209
|
-
socket = TCPSocket.new(Consumer.configuration.host,
|
210
|
-
puts "Connected to server. #{Consumer.configuration.host} #{Consumer.configuration.port} " if attempt == 0
|
210
|
+
socket = TCPSocket.new(Consumer.configuration.host, Consumer.configuration.port)
|
211
211
|
attempt = 0
|
212
212
|
|
213
213
|
loop do
|
data/lib/kafkr/log.rb
CHANGED
@@ -74,7 +74,7 @@ module Kafkr
|
|
74
74
|
end
|
75
75
|
|
76
76
|
def load_whitelist
|
77
|
-
whitelist = ["localhost", "::1","127.0.0.1"]
|
77
|
+
whitelist = ["localhost", "::1", "127.0.0.1"]
|
78
78
|
if File.exist?("whitelist.txt")
|
79
79
|
File.readlines("whitelist.txt").each do |line|
|
80
80
|
ip = line.strip.sub(/^::ffff:/, "")
|
@@ -91,8 +91,19 @@ module Kafkr
|
|
91
91
|
private
|
92
92
|
|
93
93
|
def extract_uuid(message)
|
94
|
-
|
95
|
-
|
94
|
+
|
95
|
+
#check if message if valid json
|
96
|
+
begin
|
97
|
+
message = JSON.parse(message)
|
98
|
+
|
99
|
+
return message["uuid"], message
|
100
|
+
|
101
|
+
rescue JSON::ParserError => e
|
102
|
+
puts "Received invalid message format: #{message}"
|
103
|
+
match_data = /^(\w{8}-\w{4}-\w{4}-\w{4}-\w{12}): (.+)$/.match(message)
|
104
|
+
match_data ? [match_data[1], match_data[2]] : [nil, nil]
|
105
|
+
end
|
106
|
+
|
96
107
|
end
|
97
108
|
|
98
109
|
def acknowledge_message(uuid, client)
|
data/lib/kafkr/producer.rb
CHANGED
@@ -21,6 +21,7 @@ module Kafkr
|
|
21
21
|
@configuration.acknowledged_messages = []
|
22
22
|
@configuration.acknowledged_messages = load_acknowledged_messages
|
23
23
|
load_queue_from_file
|
24
|
+
@configuration.is_json = false
|
24
25
|
@configuration
|
25
26
|
end
|
26
27
|
|
@@ -72,14 +73,22 @@ module Kafkr
|
|
72
73
|
|
73
74
|
def self.send_message(message, acknowledge: true)
|
74
75
|
uuid = SecureRandom.uuid
|
76
|
+
|
77
|
+
message_with_uuid = nil
|
75
78
|
|
76
|
-
if
|
77
|
-
|
78
|
-
|
79
|
-
|
79
|
+
if Kafkr::Producer.configuration.is_json
|
80
|
+
json_message = JSON.parse(message)
|
81
|
+
json_message["uuid"] = uuid
|
82
|
+
message_with_uuid = JSON.dump(json_message)
|
83
|
+
else
|
84
|
+
if message.is_a? String
|
85
|
+
message = structured_data_to_hash(input: message, sync_uid: uuid)
|
86
|
+
message_with_uuid = "#{uuid}: #{message}"
|
87
|
+
end
|
80
88
|
|
81
|
-
|
82
|
-
|
89
|
+
if message.is_a?(Hash)
|
90
|
+
message_with_uuid = "#{uuid}: #{JSON.generate(message)}"
|
91
|
+
end
|
83
92
|
end
|
84
93
|
|
85
94
|
# Encrypt the message here
|
@@ -116,17 +125,14 @@ module Kafkr
|
|
116
125
|
|
117
126
|
def self.send_message_and_wait(message)
|
118
127
|
# Using method(:send_message) to pass the send_message method as a callable object
|
119
|
-
|
120
|
-
|
128
|
+
|
129
|
+
Consumer.new.listen_for(message, method(:send_message)) do |received_message, sync_uid|
|
121
130
|
if received_message.key? "reply"
|
122
|
-
if received_message["reply"].dig(
|
123
|
-
received_message["reply"].dig(
|
131
|
+
if received_message["reply"].dig("uuid") == sync_uid
|
132
|
+
received_message["reply"].dig("payload")
|
124
133
|
end
|
125
134
|
end
|
126
|
-
|
127
135
|
end
|
128
|
-
|
129
|
-
payload
|
130
136
|
end
|
131
137
|
|
132
138
|
private
|
data/lib/kafkr/version.rb
CHANGED
data/lib/kafkr.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: kafkr
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.9.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Delaney Kuldvee Burke
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-01-
|
11
|
+
date: 2024-01-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: gibberish
|