codeclimate-kafka 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 819817bf067f7aa8de2c99aeeed13a72e83f76c6
4
+ data.tar.gz: d24a7b691c331a90b77747162d6753d3b0679709
5
+ SHA512:
6
+ metadata.gz: 1bbbf6cc5e3a7abdbeaab47bc2431569b5b5a184ef9e0e02eb10258e2aae3259c44b0f944a86b39c1f018a43ef935edfff1c99f20923edf51e5a20ca84e2fd20
7
+ data.tar.gz: 26fe00dcd28113be6568552bb623d2839c3fc7c8de1a082682d01cc45cc9409b17fceb8e40c3db6c41c4edfd99b1fee270a55eb5a4b7af8977ab544e1505da5d
data/lib/cc/kafka.rb ADDED
@@ -0,0 +1,35 @@
1
+ require "logger"
2
+ require "cc/kafka/consumer"
3
+ require "cc/kafka/producer"
4
+
5
+ module CC
6
+ module Kafka
7
+ ConfigurationError = Class.new(StandardError)
8
+
9
+ class DummyStatsd
10
+ def method_missing(*)
11
+ yield if block_given?
12
+ end
13
+ end
14
+
15
+ class << self
16
+ attr_writer :offset_model, :logger, :statsd
17
+
18
+ def logger
19
+ @logger ||= Logger.new(STDOUT)
20
+ end
21
+
22
+ def offset_model
23
+ if @offset_model.nil?
24
+ raise ConfigurationError, "Kafka.offset_model not set"
25
+ end
26
+
27
+ @offset_model
28
+ end
29
+
30
+ def statsd
31
+ @statsd ||= DummyStatsd.new
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,67 @@
1
+ require "poseidon"
2
+
3
+ module CC
4
+ module Kafka
5
+ class Consumer
6
+ def initialize(client_id, seed_brokers, topic, partition)
7
+ @offset = Kafka.offset_model.find_or_create!(
8
+ topic: topic,
9
+ partition: partition,
10
+ )
11
+
12
+ Kafka.logger.debug("offset: #{@offset.topic}/#{@offset.partition} #{@offset.current}")
13
+
14
+ @consumer = Poseidon::PartitionConsumer.consumer_for_partition(
15
+ client_id,
16
+ seed_brokers,
17
+ @offset.topic,
18
+ @offset.partition,
19
+ @offset.current
20
+ )
21
+ end
22
+
23
+ def on_message(&block)
24
+ @on_message = block
25
+ end
26
+
27
+ def start
28
+ trap(:TERM) { stop }
29
+
30
+ @running = true
31
+
32
+ while @running do
33
+ fetch_messages
34
+ end
35
+
36
+ Kafka.logger.info("shutting down due to TERM signal")
37
+
38
+ ensure
39
+ @consumer.close
40
+ end
41
+
42
+ def stop
43
+ @running = false
44
+ end
45
+
46
+ private
47
+
48
+ def fetch_messages
49
+ @consumer.fetch.each do |message|
50
+ Kafka.statsd.increment("messages.received")
51
+ Kafka.statsd.time("messages.processing") do
52
+ @offset.set(current: message.offset + 1)
53
+
54
+ Kafka.offset_model.transaction do
55
+ @on_message.call(BSON.deserialize(message.value))
56
+ end
57
+ end
58
+ Kafka.statsd.increment("messages.processed")
59
+ end
60
+ rescue Poseidon::Errors::UnknownTopicOrPartition
61
+ Kafka.logger.debug("topic #{@topic} not created yet")
62
+
63
+ sleep 1
64
+ end
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,50 @@
1
+ require "bson"
2
+ require "poseidon"
3
+
4
+ module CC
5
+ module Kafka
6
+ class Producer
7
+ def initialize(client_id, url)
8
+ @client_id = client_id
9
+ @url = url
10
+ end
11
+
12
+ def send_message(data, key = nil)
13
+ serialized = BSON.serialize(data).to_s
14
+ message = Poseidon::MessageToSend.new(topic, serialized, key)
15
+
16
+ producer.send_messages([message])
17
+ rescue
18
+ close
19
+
20
+ raise
21
+ end
22
+
23
+ def close
24
+ producer.close
25
+ end
26
+
27
+ private
28
+
29
+ def producer
30
+ @producer ||= Poseidon::Producer.new(
31
+ broker,
32
+ @client_id,
33
+ compression_codec: :gzip
34
+ )
35
+ end
36
+
37
+ def broker
38
+ ["#{uri.host}:#{uri.port}"]
39
+ end
40
+
41
+ def topic
42
+ uri.path.split("/")[1]
43
+ end
44
+
45
+ def uri
46
+ @uri ||= URI.parse(@url)
47
+ end
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,5 @@
1
+ module CC
2
+ module Kafka
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
metadata ADDED
@@ -0,0 +1,103 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: codeclimate-kafka
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Code Climate
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-06-29 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bson
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 1.12.2
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 1.12.2
27
+ - !ruby/object:Gem::Dependency
28
+ name: bson_ext
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 1.12.2
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 1.12.2
41
+ - !ruby/object:Gem::Dependency
42
+ name: poseidon
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 0.0.5
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 0.0.5
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: Code Climate Kafka Client
70
+ email: hello@codeclimate.com
71
+ executables: []
72
+ extensions: []
73
+ extra_rdoc_files: []
74
+ files:
75
+ - lib/cc/kafka.rb
76
+ - lib/cc/kafka/consumer.rb
77
+ - lib/cc/kafka/producer.rb
78
+ - lib/cc/kafka/version.rb
79
+ homepage: https://codeclimate.com
80
+ licenses:
81
+ - MIT
82
+ metadata: {}
83
+ post_install_message:
84
+ rdoc_options: []
85
+ require_paths:
86
+ - lib
87
+ required_ruby_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ required_rubygems_version: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ requirements: []
98
+ rubyforge_project:
99
+ rubygems_version: 2.4.5
100
+ signing_key:
101
+ specification_version: 4
102
+ summary: Code Climate Kafka Client
103
+ test_files: []