kafka_event_hub 0.1.2 → 1.1.0
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/README.md +129 -14
- data/kafka_event_hub.gemspec +6 -4
- data/lib/kafka_event_hub/consumer.rb +35 -0
- data/lib/kafka_event_hub/version.rb +1 -1
- data/lib/kafka_event_hub.rb +1 -0
- metadata +6 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 376525f485f73ea729e460154f69178ed5c62eab2255dd746386e5bff0e4e7f6
|
4
|
+
data.tar.gz: bcc93865915d1ec526cbf70032030e0a7ceca7a5aa163ba86942e42348ae3ca6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2013e2c844dd9346c1e076470967aad13621db009679c77e0873ce317c91355bcd66fe68ee887272f6b1a3d4a2c800373dc6048e26beace2e8355c0e5d600f2b
|
7
|
+
data.tar.gz: d0d77508e2e0c7fdbf4418e7ce4e71c0e2d00e562c04fa3e89e4e693eafe8de46e92a1abe7918fc374d20755aabc848c7849ffcb1cec40a76286d67b7beb7941
|
data/README.md
CHANGED
@@ -1,43 +1,158 @@
|
|
1
1
|
# KafkaEventHub
|
2
2
|
|
3
|
-
|
3
|
+
[](https://github.com/marryam-shahzad/kafka_event_hub/actions)
|
4
|
+
[](https://badge.fury.io/rb/kafka_event_hub)
|
4
5
|
|
5
|
-
|
6
|
+
KafkaEventHub is a Ruby gem providing a clean, generic, and configurable interface for producing and consuming Kafka messages in Ruby and Rails applications. It abstracts Kafka connection management and offers flexible producer and consumer classes for easy event streaming integration.
|
7
|
+
|
8
|
+
---
|
9
|
+
|
10
|
+
## Features
|
11
|
+
|
12
|
+
- Generic Kafka Producer with topic, key, payload, and partition support.
|
13
|
+
- Generic Kafka Consumer supporting message polling and subscription.
|
14
|
+
- Easily configurable via environment variables or programmatically.
|
15
|
+
- Lightweight and reusable across multiple Ruby or Rails projects.
|
16
|
+
- Handles message serialization/deserialization seamlessly.
|
17
|
+
- Retry mechanisms can be implemented externally in jobs or services.
|
18
|
+
|
19
|
+
---
|
6
20
|
|
7
21
|
## Installation
|
8
22
|
|
9
23
|
Add this line to your application's Gemfile:
|
10
24
|
|
11
|
-
|
12
|
-
gem 'kafka_event_hub'
|
13
|
-
```
|
25
|
+
gem 'kafka_event_hub'
|
14
26
|
|
15
|
-
|
27
|
+
Then execute:
|
16
28
|
|
17
|
-
|
29
|
+
bundle install
|
18
30
|
|
19
31
|
Or install it yourself as:
|
20
32
|
|
21
|
-
|
33
|
+
gem install kafka_event_hub
|
34
|
+
|
35
|
+
---
|
36
|
+
|
37
|
+
## Configuration
|
38
|
+
|
39
|
+
Before using the gem, set the following environment variables (adjust as per your Kafka setup):
|
40
|
+
|
41
|
+
```bash
|
42
|
+
DEFAULT_TOPIC=default # Default Kafka topic
|
43
|
+
EVENT_HUB_NAMESPACE=your-namespace.servicebus.windows.net:9093
|
44
|
+
EVENT_HUB_URL="Endpoint=sb://your-namespace.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=your-access-key"
|
45
|
+
EVENT_HUB_CONSUMER_GROUP=Default
|
46
|
+
APP_NAME=your-app-name
|
47
|
+
KAFKA_POLL_TIMEOUT=120000 # in milliseconds
|
48
|
+
```
|
49
|
+
---
|
22
50
|
|
23
51
|
## Usage
|
24
52
|
|
25
|
-
|
53
|
+
### Producer
|
54
|
+
|
55
|
+
Create a producer instance and send messages to Kafka topics easily:
|
56
|
+
|
57
|
+
producer = KafkaEventHub::Producer.new('your-topic')
|
58
|
+
|
59
|
+
producer.produce(
|
60
|
+
key: 'Entity#123',
|
61
|
+
payload: { event: 'event_name', data: 'your data here', timestamp: Time.now }
|
62
|
+
)
|
63
|
+
|
64
|
+
### Consumer
|
65
|
+
|
66
|
+
Create a consumer instance and process incoming messages with a block:
|
67
|
+
|
68
|
+
consumer = KafkaEventHub::Consumer.new('your-topic')
|
69
|
+
|
70
|
+
consumer.each_message do |message|
|
71
|
+
puts "Received message: #{message.payload}"
|
72
|
+
# Your message processing logic here
|
73
|
+
end
|
74
|
+
|
75
|
+
Alternatively, use `poll_messages` with a timeout (in milliseconds):
|
76
|
+
|
77
|
+
consumer.poll_messages(1000) do |message|
|
78
|
+
# Process each message here
|
79
|
+
end
|
80
|
+
|
81
|
+
---
|
82
|
+
|
83
|
+
## Integration with Rails Jobs
|
84
|
+
|
85
|
+
You can wrap your Kafka producer and consumer in Rails ActiveJobs for background processing and retries.
|
86
|
+
|
87
|
+
### Example Producer Job
|
88
|
+
|
89
|
+
class GenericKafkaProducerJob < ActiveJob::Base
|
90
|
+
queue_as :kafka_stream
|
91
|
+
|
92
|
+
def perform(key, payload, topic = 'default-topic', partition = 0)
|
93
|
+
KafkaEventHub::Producer.new(topic).produce(
|
94
|
+
key: key,
|
95
|
+
payload: payload,
|
96
|
+
partition: partition
|
97
|
+
)
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
Customize the job code to suit your app’s background job system or retry logic.
|
102
|
+
|
103
|
+
### Example Consumer Job
|
104
|
+
|
105
|
+
class GenericKafkaConsumerJob < ActiveJob::Base
|
106
|
+
queue_as :kafka_consumer
|
107
|
+
|
108
|
+
def perform(topic = 'default-topic')
|
109
|
+
consumer = KafkaEventHub::Consumer.new(topic)
|
110
|
+
|
111
|
+
consumer.each_message do |message|
|
112
|
+
payload = JSON.parse(message.payload)
|
113
|
+
# Process your payload here
|
114
|
+
end
|
115
|
+
end
|
116
|
+
end
|
117
|
+
|
118
|
+
---
|
26
119
|
|
27
120
|
## Development
|
28
121
|
|
29
|
-
|
122
|
+
To set up the development environment:
|
123
|
+
|
124
|
+
bundle install
|
125
|
+
rake spec
|
126
|
+
|
127
|
+
To build and install the gem locally:
|
128
|
+
|
129
|
+
gem build kafka_event_hub.gemspec
|
130
|
+
gem install ./kafka_event_hub-0.1.0.gem
|
131
|
+
|
132
|
+
To release a new version:
|
30
133
|
|
31
|
-
|
134
|
+
1. Update the version in `lib/kafka_event_hub/version.rb`.
|
135
|
+
2. Run:
|
136
|
+
|
137
|
+
bundle exec rake release
|
138
|
+
|
139
|
+
This will create a git tag, push commits and tags, and push the gem to [RubyGems.org](https://rubygems.org).
|
140
|
+
|
141
|
+
---
|
32
142
|
|
33
143
|
## Contributing
|
34
144
|
|
35
|
-
Bug reports and pull requests are welcome on GitHub
|
145
|
+
Bug reports and pull requests are welcome on [GitHub](https://github.com/marryam-shahzad/kafka_event_hub).
|
146
|
+
Please follow the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
147
|
+
|
148
|
+
---
|
36
149
|
|
37
150
|
## License
|
38
151
|
|
39
|
-
|
152
|
+
This gem is licensed under the [MIT License](https://opensource.org/licenses/MIT).
|
153
|
+
|
154
|
+
---
|
40
155
|
|
41
156
|
## Code of Conduct
|
42
157
|
|
43
|
-
Everyone
|
158
|
+
Everyone participating in this project is expected to follow the [code of conduct](https://github.com/marryam-shahzad/kafka_event_hub/blob/master/CODE_OF_CONDUCT.md).
|
data/kafka_event_hub.gemspec
CHANGED
@@ -15,14 +15,16 @@ Gem::Specification.new do |spec|
|
|
15
15
|
Centralizes ENV-based Rd-kafka setup into a single gem.
|
16
16
|
DESC
|
17
17
|
|
18
|
-
#
|
19
|
-
spec.homepage = "https://
|
18
|
+
# Homepage and source links
|
19
|
+
spec.homepage = "https://github.com/marryam-shahzad/kafka_event_hub"
|
20
20
|
spec.license = "MIT"
|
21
21
|
|
22
|
-
# Only allow pushing to RubyGems.org
|
22
|
+
# Only allow pushing to RubyGems.org and link to GitHub
|
23
23
|
if spec.respond_to?(:metadata)
|
24
24
|
spec.metadata["allowed_push_host"] = "https://rubygems.org"
|
25
25
|
spec.metadata["homepage_uri"] = spec.homepage
|
26
|
+
spec.metadata["source_code_uri"] = "https://github.com/marryam-shahzad/kafka_event_hub"
|
27
|
+
spec.metadata["documentation_uri"] = "https://github.com/marryam-shahzad/kafka_event_hub/blob/master/README.md"
|
26
28
|
end
|
27
29
|
|
28
30
|
# Files to include in the gem
|
@@ -36,5 +38,5 @@ Gem::Specification.new do |spec|
|
|
36
38
|
spec.add_development_dependency "bundler", "~> 1.17"
|
37
39
|
spec.add_development_dependency "rake", "~> 10.0"
|
38
40
|
spec.add_development_dependency "rspec", "~> 3.0"
|
39
|
-
spec.add_dependency
|
41
|
+
spec.add_dependency "uri", "~> 1.0.3"
|
40
42
|
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
# lib/kafka_event_hub/consumer.rb
|
2
|
+
module KafkaEventHub
|
3
|
+
class Consumer < Config
|
4
|
+
def initialize(topic = nil)
|
5
|
+
super('consumer' => true, 'topic' => topic)
|
6
|
+
end
|
7
|
+
|
8
|
+
# Generic message consuming loop - pass a block to process messages
|
9
|
+
def each_message
|
10
|
+
consumer = @kafka.consumer
|
11
|
+
consumer.subscribe(@topic)
|
12
|
+
|
13
|
+
consumer.each do |message|
|
14
|
+
yield message
|
15
|
+
end
|
16
|
+
ensure
|
17
|
+
consumer&.close
|
18
|
+
end
|
19
|
+
|
20
|
+
# Optional: poll with timeout
|
21
|
+
def poll_messages(timeout = POLL_TIMEOUT)
|
22
|
+
consumer = @kafka.consumer
|
23
|
+
consumer.subscribe(@topic)
|
24
|
+
|
25
|
+
loop do
|
26
|
+
message = consumer.poll(timeout)
|
27
|
+
break unless message
|
28
|
+
|
29
|
+
yield message
|
30
|
+
end
|
31
|
+
ensure
|
32
|
+
consumer&.close
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
data/lib/kafka_event_hub.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: kafka_event_hub
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Marryam Shahzad
|
@@ -106,14 +106,17 @@ files:
|
|
106
106
|
- kafka_event_hub.gemspec
|
107
107
|
- lib/kafka_event_hub.rb
|
108
108
|
- lib/kafka_event_hub/config.rb
|
109
|
+
- lib/kafka_event_hub/consumer.rb
|
109
110
|
- lib/kafka_event_hub/producer.rb
|
110
111
|
- lib/kafka_event_hub/version.rb
|
111
|
-
homepage: https://
|
112
|
+
homepage: https://github.com/marryam-shahzad/kafka_event_hub
|
112
113
|
licenses:
|
113
114
|
- MIT
|
114
115
|
metadata:
|
115
116
|
allowed_push_host: https://rubygems.org
|
116
|
-
homepage_uri: https://
|
117
|
+
homepage_uri: https://github.com/marryam-shahzad/kafka_event_hub
|
118
|
+
source_code_uri: https://github.com/marryam-shahzad/kafka_event_hub
|
119
|
+
documentation_uri: https://github.com/marryam-shahzad/kafka_event_hub/blob/master/README.md
|
117
120
|
post_install_message:
|
118
121
|
rdoc_options: []
|
119
122
|
require_paths:
|