kafkr 0.018.1 → 0.22.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: 1e7fd62a57849b5096b9cba1a6928ee2786a401084ca29af0bfdfb1fc2cab7c5
4
- data.tar.gz: 6ca89348304182eeabb6c4518818ad804d6b1b4c429c52ba82905cbf431bbb12
3
+ metadata.gz: 562897f55ad72dc74c2d7a50d1a604fe40bfc0629c9fd8fe828acda386ba721c
4
+ data.tar.gz: 8d8745987c793c5df150f3fb1a586648ec7b8bd519681f9368f4400f304fe016
5
5
  SHA512:
6
- metadata.gz: a2ec64c8b63a6b57860ad372ca2dc851dffca1955f7ffe0a1958aaa3a636e50f05fd581e5556761355c8515317c16f29b4d78c8d839c7c429976789d2fcfcb61
7
- data.tar.gz: f10c003cecfacbcd05376832784430262bcdb26244efe5c345a22bdf30a75ca67b335e48bc1a6b6c72476619ab984c3b4d66a10b54562b5805940ee4e9974b9a
6
+ metadata.gz: 5c1f5869497af97e702017ff175478cc1c6c4dbf32843385172dea0e6ca1447ca8c054a3f4327a6ceb66df3403918052ef03d9dd66cb5e397f8d32ab1ae72c1d
7
+ data.tar.gz: ec955aea4857a2b9824e3fabde5870ebc0a4e5c0abeeeec49c3eaff253334ff1dea89fb0bd3abaf1f03d9442e19d7a19a00ff8af59eb4ea4f0abd1dc81300cb6
data/exe/kafkr-consumer CHANGED
@@ -51,7 +51,7 @@ def start_consumer(port, host, timeout)
51
51
 
52
52
  $current_consumer = Kafkr::Consumer.new
53
53
  $current_consumer.listen do |message|
54
- # Processing of the message
54
+ Kafkr.log "Message consumed: #{message}"# Processing of the message
55
55
  end
56
56
  end
57
57
 
data/exe/kafkr-producer CHANGED
@@ -1,11 +1,12 @@
1
1
  #!/usr/bin/env ruby
2
+ require "kafkr"
2
3
  require "readline"
3
4
 
4
5
  # Parsing command line arguments for host and port
5
6
  host = ARGV[0] || "localhost"
6
7
  port = ARGV[1] || 4000
7
8
 
8
- kakf "Running on host: #{host} and port: #{port}"
9
+ Kafkr.log "Running on host: #{host} and port: #{port}"
9
10
 
10
11
  begin
11
12
  require "kafkr"
@@ -1,6 +1,8 @@
1
+ require "pry"
2
+
1
3
  class WebHandler < Kafkr::Consumer::Handler
2
4
  def handle?(message)
3
- can_handle? message, "web"
5
+ message.include?("web")
4
6
  end
5
7
 
6
8
  def handle(message)
@@ -77,23 +77,6 @@ module Kafkr
77
77
  Kafkr::Producer.send_message({reply: {payload: payload, uuid: to["sync_uid"]}})
78
78
  end
79
79
 
80
- private
81
-
82
- def can_handle?(message, name, ignore: :any)
83
- if message.is_a?(Numeric)
84
- return true if message == name.to_i
85
- elsif ignore == :hash
86
- return true if message[:message] && message[:message][:body] && message[:message][:body] == name
87
- return true if message[:message] && message[:message][:body] && message[:message][:body].start_with?(name)
88
- elsif ignore == :string
89
- return true if message.key? name
90
- else
91
- return true if message.key? name
92
- return true if message[:message] && message[:message][:body] && message[:message][:body] == name
93
- return true if message[:message] && message[:message][:body] && message[:message][:body].start_with?(name)
94
- end
95
- false
96
- end
97
80
  end
98
81
 
99
82
  def initialize(host = Consumer.configuration.host, port = Consumer.configuration.port)
@@ -113,40 +96,6 @@ module Kafkr
113
96
  /^[A-Z]\w*$/.match?(name)
114
97
  end
115
98
 
116
- def print_handler_class(name)
117
- return if name.is_a?(Numeric)
118
- name = name.keys.first if name.is_a?(Hash)
119
- handler_name = "#{name.downcase}_handler"
120
-
121
- if $loaded_handlers.key?(handler_name)
122
- return
123
- end
124
-
125
- if Kafkr::Consumer.configuration.suggest_handlers
126
- if valid_class_name?(name.capitalize)
127
- Kafkr.log "No handler for this message, you could use this one.\n\n"
128
-
129
- handler_class_string = <<~HANDLER_CLASS
130
- class #{name.capitalize}Handler < Kafkr::Consumer::Handler
131
- def handle?(message)
132
- can_handle? message, '#{name}'
133
- end
134
-
135
- def handle(message)
136
- Kafkr.log message
137
- end
138
- end
139
-
140
- # Save the file to ./handlers/#{name}_handler.rb
141
- HANDLER_CLASS
142
-
143
- Kafkr.log handler_class_string
144
- end
145
- end
146
- end
147
-
148
- require "timeout"
149
-
150
99
  def listen_for(message, send_message)
151
100
  attempt = 0
152
101
  begin
@@ -202,7 +151,8 @@ module Kafkr
202
151
  private
203
152
 
204
153
  def dispatch_to_handlers(message)
205
- message_hash = message.is_a?(String) ? {message: {body: message}} : message
154
+
155
+ message_hash = JSON.parse(message)
206
156
 
207
157
  self.class.handlers.each do |handler|
208
158
  if handler.handle?(message_hash)
@@ -210,8 +160,6 @@ module Kafkr
210
160
  end
211
161
  end
212
162
 
213
- print_handler_class(message)
214
-
215
163
  yield message_hash if block_given?
216
164
  end
217
165
  end
data/lib/kafkr/log.rb CHANGED
@@ -1,5 +1,7 @@
1
1
  require "socket"
2
2
  require "rubygems"
3
+ require "kafkr"
4
+
3
5
 
4
6
  module Kafkr
5
7
  class Log
@@ -50,13 +52,13 @@ module Kafkr
50
52
  decryptor = Kafkr::Encryptor.new
51
53
  message = decryptor.decrypt(encrypted_message.chomp) # Decrypt the message here
52
54
  uuid, message_content = extract_uuid(message)
53
- @broker.broadcast(message_content)
55
+ @broker.broadcast(JSON.dump(message_content))
54
56
  end
55
57
  rescue Errno::ECONNRESET
56
58
  client.close
57
59
  end
58
60
  rescue StandardError => exception
59
- #TODO we need to find the place for a global exception handler
61
+ Kafkr.log "Error: #{exception.message}"
60
62
  end
61
63
  end
62
64
  end
@@ -16,7 +16,7 @@ module Kafkr
16
16
  encrypted_message = Kafkr::Encryptor.new.encrypt(message)
17
17
  @subscribers.each do |subscriber|
18
18
  if !subscriber.closed?
19
- subscriber.Kafkr.log(encrypted_message)
19
+ subscriber.puts(encrypted_message)
20
20
  @last_sent[subscriber] = encrypted_message
21
21
  end
22
22
  rescue Errno::EPIPE
@@ -17,7 +17,7 @@ module Kafkr
17
17
  @configuration.queue_file = MESSAGE_QUEUE
18
18
  @configuration.message_queue = []
19
19
  load_queue_from_file
20
- @configuration.is_json = false
20
+ @configuration.is_json = true
21
21
  @configuration
22
22
  end
23
23
 
@@ -27,28 +27,6 @@ module Kafkr
27
27
  logger.error("Configuration error: #{e.message}")
28
28
  end
29
29
 
30
- def self.structured_data_to_hash(input:, sync_uid:)
31
- unless /\A\w+\s*(=>|<=>)\s*((\w+:\s*['"]?[^'",]*['"]?,\s*)*(\w+:\s*['"]?[^'",]*['"]?)\s*)\z/.match?(input)
32
- return input
33
- end
34
-
35
- if input.include?("<=>")
36
- type, key_values_str = input.split("<=>").map(&:strip)
37
- key_values = key_values_str.scan(/(\w+):\s*['"]?([^'",]*)['"]?/)
38
- hash_body = key_values.to_h do |key, value|
39
- [key.to_sym, value.strip.gsub(/\A['"]|['"]\z/, "")]
40
- end
41
- {type.to_sym => hash_body, :sync => true, :sync_uid => sync_uid}
42
- else
43
- type, key_values_str = input.split("=>").map(&:strip)
44
- key_values = key_values_str.scan(/(\w+):\s*['"]?([^'",]*)['"]?/)
45
- hash_body = key_values.to_h do |key, value|
46
- [key.to_sym, value.strip.gsub(/\A['"]|['"]\z/, "")]
47
- end
48
- {type.to_sym => hash_body}
49
- end
50
- end
51
-
52
30
  def self.send_message(message)
53
31
  return if message.nil? || message.empty?
54
32
 
@@ -59,15 +37,6 @@ module Kafkr
59
37
  json_message = JSON.parse(message)
60
38
  json_message["uuid"] = uuid
61
39
  message_with_uuid = JSON.dump(json_message)
62
- else
63
- if message.is_a? String
64
- message = structured_data_to_hash(input: message, sync_uid: uuid)
65
- message_with_uuid = "#{uuid}: #{message}"
66
- end
67
-
68
- if message.is_a?(Hash)
69
- message_with_uuid = "#{uuid}: #{JSON.generate(message)}"
70
- end
71
40
  end
72
41
 
73
42
  encrypted_message_with_uuid = Kafkr::Encryptor.new.encrypt(message_with_uuid)
@@ -75,7 +44,7 @@ module Kafkr
75
44
  begin
76
45
  socket = TCPSocket.new(@configuration.host, @configuration.port)
77
46
  send_queued_messages(socket)
78
- socket.Kafkr.log(encrypted_message_with_uuid)
47
+ socket.puts (encrypted_message_with_uuid)
79
48
  rescue Errno::ECONNREFUSED
80
49
  Kafkr.log "Connection refused. Queuing message: #{encrypted_message_with_uuid}"
81
50
  @configuration.message_queue.push(encrypted_message_with_uuid)
@@ -106,14 +75,14 @@ module Kafkr
106
75
  def self.send_queued_messages(socket)
107
76
  until @configuration.message_queue.empty?
108
77
  queued_message = @configuration.message_queue.shift
109
- socket.Kafkr.log(queued_message)
78
+ socket.puts(queued_message)
110
79
  end
111
80
  end
112
81
 
113
82
  def self.save_queue_to_file
114
83
  @@file_mutex.synchronize do
115
84
  File.open(@configuration.queue_file, "w") do |file|
116
- file.Kafkr.log(@configuration.message_queue)
85
+ file.puts(@configuration.message_queue)
117
86
  end
118
87
  end
119
88
  end
@@ -126,8 +95,5 @@ module Kafkr
126
95
  end
127
96
  end
128
97
 
129
- def self.logger
130
- @logger ||= Logger.new(STDOUT)
131
- end
132
98
  end
133
99
  end
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.018.1"
4
+ VERSION = "0.22.0"
5
5
  end
data/lib/kafkr.rb CHANGED
@@ -1,11 +1,11 @@
1
- # frozen_string_literal: true
2
-
1
+ require "pry"
3
2
  require "logger"
4
3
  require "openssl"
5
4
  require "securerandom"
6
5
  require "ostruct"
7
6
  require "gibberish"
8
7
 
8
+ # Assuming the relative paths are correct and these files exist.
9
9
  require_relative "kafkr/encryptor"
10
10
  require_relative "kafkr/message_broker"
11
11
  require_relative "kafkr/log"
@@ -14,73 +14,37 @@ require_relative "kafkr/producer"
14
14
  require_relative "kafkr/version"
15
15
 
16
16
  module Kafkr
17
+ LOG_FILE_PATH = "/var/log/kafkr.log" # Default log file path
18
+
17
19
  class << self
18
- attr_accessor :current_environment
20
+ attr_writer :log_file_path
21
+
19
22
  def logger
20
23
  @logger ||= configure_logger
21
24
  end
22
25
 
23
- def configure_logger(output = default_output)
24
- begin
25
- @logger = ::Logger.new(output)
26
- rescue Errno::EACCES, Errno::ENOENT => e
27
- @logger = ::Logger.new(STDOUT)
28
- @logger.error("Could not open log file: #{e.message}")
29
- end
30
- set_logger_level
31
- @logger
32
- end
33
-
34
- def default_output
35
- case current_environment
36
- when "production"
37
- "/var/log/kafkr.log"
38
- else
39
- STDOUT
40
- end
41
- end
42
-
43
- def set_logger_level
44
- @logger.level = case current_environment
45
- when "development"
46
- ::Logger::DEBUG
47
- when "staging"
48
- ::Logger::INFO
49
- when "production"
50
- ::Logger::WARN
51
- else
52
- ::Logger::DEBUG
53
- end
54
- end
55
-
56
- def current_environment
57
- @current_environment ||= ENV["KAFKR_ENV"] || "development"
58
- end
59
-
60
- def development?
61
- current_environment == "development"
62
- end
63
-
64
- def test?
65
- current_environment == "test"
66
- end
67
-
68
- def staging?
69
- current_environment == "staging"
26
+ def log_file_path
27
+ @log_file_path || LOG_FILE_PATH
70
28
  end
71
29
 
72
- def production?
73
- current_environment == "production"
30
+ def configure_logger
31
+ logger = ::Logger.new(log_file_path)
32
+ logger.level = ::Logger::DEBUG
33
+ logger
34
+ rescue Errno::EACCES, Errno::ENOENT => e
35
+ # Fallback to STDOUT only if file creation fails, can be removed to strictly enforce file logging
36
+ logger = ::Logger.new(STDOUT)
37
+ logger.error("Could not open log file: #{e.message}")
38
+ logger
74
39
  end
75
40
 
76
41
  def write(message, unique_id = nil)
77
- begin
78
- logger.info(message)
79
- rescue IOError => e
80
- @logger.error("Failed to write log: #{e.message}")
81
- end
42
+ logger.info(message)
43
+ rescue IOError => e
44
+ logger.error("Failed to write log: #{e.message}")
82
45
  end
83
46
 
47
+ # Maintaining existing method aliases
84
48
  alias_method :log, :write
85
49
  alias_method :output, :write
86
50
  alias_method :info, :write
@@ -99,4 +63,4 @@ module Kafkr
99
63
  rescue => e
100
64
  logger.error("Configuration error: #{e.message}")
101
65
  end
102
- end
66
+ 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.018.1
4
+ version: 0.22.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-10 00:00:00.000000000 Z
11
+ date: 2024-04-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: gibberish