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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 916ae5ff763b93c94e0e76ff62bb7026eb82e40570033ba05574d6c88d414568
4
- data.tar.gz: f00ba9eb63a20bbbeadd9c64845131517c3866160f4ef2b5f73bf90ebc17a5c5
3
+ metadata.gz: c15f14feed8ee398816eb4454850798f764f10cafdf12f27aca353ac2412fa1b
4
+ data.tar.gz: 77c4facf9f2290d087d2b5b40aa63714e0c01f5d0f4a33cce4c85349264dfe63
5
5
  SHA512:
6
- metadata.gz: 9a22fddb509c7ffe907650bb5150bee4f7288c9b091af583c8668937b60bf1299059839a2b2d154ff0f11f22b2192f9495313c7d62a60abbdb6343f92148f12e
7
- data.tar.gz: dc9afae5d45522f79422ae4b03b0fcb3cb5a64e4cfa7ff1a311aa827be662e7cf4698a87be115c748f66513000750b14072838c95378acdd269a591f9d2d5aa1
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
@@ -1,5 +1,5 @@
1
1
  #!/usr/bin/env ruby
2
- require 'readline'
2
+ require "readline"
3
3
 
4
4
  # Parsing command line arguments for host and port
5
5
  host = ARGV[0] || "localhost"
@@ -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['sync_uid']}},acknowledge: false)
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 valid_class_name? name.capitalize
138
- puts "No handler for this message, you could use this one."
139
- puts ""
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
- handler_class_string = <<~HANDLER_CLASS
142
+ handler_class_string = <<~HANDLER_CLASS
142
143
 
143
- class #{name.capitalize}Handler < Kafkr::Consumer::Handler
144
- def handle?(message)
145
- can_handle? message, '#{name}'
146
- end
144
+ class #{name.capitalize}Handler < Kafkr::Consumer::Handler
145
+ def handle?(message)
146
+ can_handle? message, '#{name}'
147
+ end
147
148
 
148
- def handle(message)
149
- puts message
149
+ def handle(message)
150
+ puts message
151
+ end
150
152
  end
151
- end
152
153
 
153
- save the file to ./handlers/#{name}_handler.rb
154
+ save the file to ./handlers/#{name}_handler.rb
154
155
 
155
- HANDLER_CLASS
156
+ HANDLER_CLASS
156
157
 
157
- puts handler_class_string
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( Consumer.configuration.host, Consumer.configuration.port)
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
- payload =yield JSON.parse(received_message),sync_uid if block_given?
182
- return payload if payload
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, Consumer.configuration.port)
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
- match_data = /^(\w{8}-\w{4}-\w{4}-\w{4}-\w{12}): (.+)$/.match(message)
95
- match_data ? [match_data[1], match_data[2]] : [nil, nil]
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)
@@ -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 message.is_a? String
77
- message = structured_data_to_hash(input: message, sync_uid: uuid)
78
- message_with_uuid = "#{uuid}: #{message}"
79
- end
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
- if message.is_a?(Hash)
82
- message_with_uuid = "#{uuid}: #{JSON.generate(message)}"
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
- payload = Consumer.new.listen_for(message, self.method(:send_message)) do |received_message,sync_uid|
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('uuid') == sync_uid
123
- received_message["reply"].dig('payload')
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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Kafkr
4
- VERSION = "0.5.6"
4
+ VERSION = "0.9.1"
5
5
  end
data/lib/kafkr.rb CHANGED
@@ -11,6 +11,7 @@ require_relative "kafkr/message_broker"
11
11
  require_relative "kafkr/log"
12
12
  require_relative "kafkr/consumer"
13
13
  require_relative "kafkr/producer"
14
+ require_relative "kafkr/version"
14
15
 
15
16
  module Kafkr
16
17
  class << self
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.5.6
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-19 00:00:00.000000000 Z
11
+ date: 2024-01-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: gibberish