kafkr 0.11.0 → 0.18.0

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: 41bbdf52172942afa5cb0758bee83eeb452f74f3d61849841fe186e44ef323a5
4
- data.tar.gz: fcf59de6aed2bc3644df69573091dbffeb9641f39bb5fbdece548de3cf509217
3
+ metadata.gz: fa40dd04299dd0592d9d917e32693c1a8cbd10d25cc9678cdca44413256a0de6
4
+ data.tar.gz: 815f89d3916c61802215e1441f4d54701a278e0715c8b66686302aa7c7334764
5
5
  SHA512:
6
- metadata.gz: 1f249925d3d2de4ca159796d88628450b78fdb5d5c00c756891202d39f7d8e34b69f282613c819c2aecc734a7fc0fe0c414ea2471955c27dda394e9f27bca1d5
7
- data.tar.gz: 8e004710cfffe4ead0df8ed181fa5f8d044ca4cb8690d6be92028473475a9b2fddea89f0c8f77fbf96ea8b3eeaa00886a8e7ec94eff338379c9797cfa8956bf7
6
+ metadata.gz: cb51cb0b42d11a6f701dae3ea46fd520c46cbd9c8af20e6040c1f577162eff484d8dd7d9c9a783de60fcfc1250650cbec2ab22aaa8ff6d2481dc5defc57d6714
7
+ data.tar.gz: 454bbcf8b06713fdac04cff7abcf0d57bbec78fc4b85747e61217d8594fc2cb445dac357c6bc89b6acb4ca047e874d6cfb86060b6cfb30ea7aee03fe68d0508a
data/exe/kafkr-consumer CHANGED
@@ -7,7 +7,7 @@ require "digest"
7
7
  # Accepting command line arguments for host and port
8
8
  host = ARGV[0] || "localhost"
9
9
  port = ARGV[1] || 4000
10
- timeout = ARGV[2] || 120
10
+ timeout = ARGV[2] || 300
11
11
 
12
12
  puts "Running on host: #{host} and port: #{port}"
13
13
 
@@ -34,8 +34,8 @@ def list_registered_handlers
34
34
  end
35
35
  end
36
36
 
37
- def start_consumer(port, host)
38
- puts "Starting consumer on port #{port}!"
37
+ def start_consumer(port, host, timeout)
38
+ puts "Starting consumer on port #{port}! timeout: #{timeout}"
39
39
  $handlers_changed = false
40
40
 
41
41
  Kafkr::Consumer.configure do |config|
@@ -97,13 +97,13 @@ end
97
97
 
98
98
  file_checksums = {}
99
99
  monitoring_thread = Thread.new { monitor_handlers(file_checksums) }
100
- start_consumer(port, host) # Pass the port here
100
+ start_consumer(port, host,timeout) # Pass the port here
101
101
 
102
102
  begin
103
103
  loop do
104
104
  if $restart_required
105
105
  stop_consumer
106
- start_consumer(port, host)
106
+ start_consumer(port, host,timeout)
107
107
  $restart_required = false
108
108
  end
109
109
  sleep 1
@@ -1,4 +1,4 @@
1
- require "socket"
1
+ require "socket"
2
2
  require "timeout"
3
3
  require "ostruct"
4
4
  require "fileutils"
@@ -19,8 +19,8 @@ module Kafkr
19
19
  FileUtils.mkdir_p "./.kafkr"
20
20
  @configuration ||= OpenStruct.new
21
21
  @configuration.host = ENV.fetch("KAFKR_HOST", "localhost")
22
- @configuration.port = ENV.fetch("KAFKR_PORT", 2000)
23
- @configuration.timeout = ENV.fetch("KAFKR_CONSUMER_TIMEOUT", 120)
22
+ @configuration.port = ENV.fetch("KAFKR_PORT", 4000)
23
+ @configuration.timeout = ENV.fetch("KAFKR_CONSUMER_TIMEOUT", 300)
24
24
  @configuration.suggest_handlers = false
25
25
  @configuration
26
26
  end
@@ -160,16 +160,13 @@ module Kafkr
160
160
  received_message = socket.gets
161
161
  raise LostConnection if received_message.nil?
162
162
  received_message = Kafkr::Encryptor.new.decrypt(received_message.chomp)
163
- if valid_json?(received_message)
164
- payload = yield JSON.parse(received_message), sync_uid if block_given?
165
- return payload if payload
166
- end
163
+ payload = yield received_message, sync_uid if block_given?
164
+ return payload
167
165
  end
168
166
  end
169
167
  rescue Timeout::Error, LostConnection, Errno::ECONNREFUSED
170
168
  attempt += 1
171
169
  wait_time = backoff_time(attempt)
172
- puts "Attempt #{attempt}: Retrying in #{wait_time} seconds..."
173
170
  sleep(wait_time)
174
171
  retry
175
172
  rescue Interrupt
@@ -182,8 +179,9 @@ module Kafkr
182
179
  def listen
183
180
  attempt = 0
184
181
  loop do
185
- listen_for("dummy", ->(msg) { puts "Listening..." }) do |message|
186
- puts "Received message: #{message}"
182
+ listen_for("dummy", ->(msg) { }) do |message|
183
+ Kafkr.log ">> #{message}"
184
+ dispatch_to_handlers(message)
187
185
  end
188
186
  end
189
187
  end
data/lib/kafkr/log.rb CHANGED
@@ -35,27 +35,28 @@ module Kafkr
35
35
  @broker.add_subscriber(client)
36
36
 
37
37
  Thread.new do
38
- loop do
39
- encrypted_message = client.gets
40
- if encrypted_message.nil?
41
- @broker.last_sent.delete(client)
42
- client.close
43
- @broker.subscribers.delete(client)
44
- puts "Client connection closed. Removed from subscribers list."
45
- break
46
- else
47
- decryptor = Kafkr::Encryptor.new
48
- message = decryptor.decrypt(encrypted_message.chomp) # Decrypt the message here
49
- uuid, message_content = extract_uuid(message)
50
- if uuid && message_content
51
- @broker.broadcast(message_content)
38
+
39
+
40
+ begin
41
+
42
+ loop do
43
+ encrypted_message = client.gets
44
+ if encrypted_message.nil?
45
+ @broker.last_sent.delete(client)
46
+ client.close
47
+ @broker.subscribers.delete(client)
48
+ break
52
49
  else
53
- puts "Received invalid message format: #{message}"
50
+ decryptor = Kafkr::Encryptor.new
51
+ message = decryptor.decrypt(encrypted_message.chomp) # Decrypt the message here
52
+ uuid, message_content = extract_uuid(message)
53
+ @broker.broadcast(message_content)
54
54
  end
55
- end
56
- rescue Errno::ECONNRESET
57
- puts "Connection reset by client. Closing connection..."
58
- client.close
55
+ rescue Errno::ECONNRESET
56
+ client.close
57
+ end
58
+ rescue StandardError => exception
59
+ #TODO we need to find the place for a global exception handler
59
60
  end
60
61
  end
61
62
  end
@@ -71,9 +72,10 @@ module Kafkr
71
72
  # Check if message is valid JSON
72
73
  begin
73
74
  message = JSON.parse(message)
75
+ Kafkr.log ">> #{message}"
74
76
  return message["uuid"], message
75
77
  rescue JSON::ParserError => e
76
- puts "Received invalid message format: #{message}"
78
+ Kafkr.log ">> #{message}"
77
79
  match_data = /^(\w{8}-\w{4}-\w{4}-\w{4}-\w{12}): (.+)$/.match(message)
78
80
  match_data ? [match_data[1], match_data[2]] : [nil, nil]
79
81
  end
@@ -13,10 +13,7 @@ module Kafkr
13
13
  end
14
14
 
15
15
  def broadcast(message)
16
- Kafkr.log message
17
-
18
- encrypted_message = Kafkr::Encryptor.new.encrypt(message)
19
-
16
+ encrypted_message = Kafkr::Encryptor.new.encrypt(message)
20
17
  @subscribers.each do |subscriber|
21
18
  if !subscriber.closed?
22
19
  subscriber.puts(encrypted_message)
@@ -25,8 +22,13 @@ module Kafkr
25
22
  rescue Errno::EPIPE
26
23
  # Optionally, handle broken pipe error
27
24
  rescue IOError
28
- @subscribers.delete(subscriber)
29
- @last_sent.delete(subscriber)
25
+ begin
26
+ @subscribers.delete(subscriber)
27
+ @last_sent.delete(subscriber)
28
+ rescue
29
+ puts "clean up subscribers"
30
+ end
31
+
30
32
  end
31
33
  end
32
34
  end
@@ -50,6 +50,8 @@ module Kafkr
50
50
  end
51
51
 
52
52
  def self.send_message(message)
53
+ return if message.nil? || message.empty?
54
+
53
55
  uuid = SecureRandom.uuid
54
56
  message_with_uuid = nil
55
57
 
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.11.0"
4
+ VERSION = "0.18.0"
5
5
  end
data/lib/kafkr.rb CHANGED
@@ -75,16 +75,7 @@ module Kafkr
75
75
 
76
76
  def write(message, unique_id = nil)
77
77
  begin
78
- unique_id ||= SecureRandom.uuid
79
- rescue => e
80
- unique_id = "unknown"
81
- @logger.error("Failed to generate UUID: #{e.message}")
82
- end
83
- formatted_message = "[#{unique_id}] #{message}"
84
-
85
- begin
86
- puts formatted_message if development?
87
- logger.info(formatted_message)
78
+ logger.info(message)
88
79
  rescue IOError => e
89
80
  @logger.error("Failed to write log: #{e.message}")
90
81
  end
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.11.0
4
+ version: 0.18.0
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-04-05 00:00:00.000000000 Z
11
+ date: 2024-04-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: gibberish