ckafka 0.1.0

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: 4fa112b0a733b5b518f4b2d54afd70fc57e0f9ea
4
+ data.tar.gz: 97d7c6a47eaf01ea6311a3896e466b27bd78bd8c
5
+ SHA512:
6
+ metadata.gz: ee9766436b07562781774f3f164328264c392171268c89497181e58932887ef4422c81043153817786c9f2d470577222630a01793c630804073dad14cb89f1cc
7
+ data.tar.gz: da5735597e5929985a715f9774d64555d7506b47bccb0f7222526dff5c8ad4ca75b125e7df55a13b0d76e39a84dfa4969a34167b7835b9cee0e7e82756865f86
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.3
4
+ before_install: gem install bundler -v 1.11.2
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in ckafka.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Ian Quick
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,41 @@
1
+ # Ckafka
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/ckafka`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'ckafka'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install ckafka
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/ckafka.
36
+
37
+
38
+ ## License
39
+
40
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
41
+
data/Rakefile ADDED
@@ -0,0 +1,11 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+ require 'rake/extensiontask'
4
+
5
+ RSpec::Core::RakeTask.new(:spec)
6
+
7
+ task :default => :spec
8
+
9
+ Rake::ExtensionTask.new 'ckafka' do |ext|
10
+ ext.lib_dir = "lib/ckafka"
11
+ end
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "ckafka"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/ckafka.gemspec ADDED
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'ckafka/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "ckafka"
8
+ spec.version = Ckafka::VERSION
9
+ spec.authors = ["Ian Quick"]
10
+ spec.email = ["ian.quick@gmail.com"]
11
+
12
+ spec.summary = "Simple Kafka Producer"
13
+ spec.description = "interface to the rdkafka C library"
14
+ spec.homepage = "https://github.com/ibawt/ckafka"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+ spec.extensions = %w(ext/ckafka/extconf.rb)
22
+ spec.add_development_dependency "bundler", "~> 1.11"
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+ spec.add_development_dependency "rspec", "~> 3.0"
25
+ spec.add_development_dependency 'rake-compile'
26
+ end
@@ -0,0 +1,144 @@
1
+ #include <stdio.h>
2
+ #include <assert.h>
3
+ #include <stdlib.h>
4
+ #include <ruby.h>
5
+
6
+ #include "librdkafka/rdkafka.h"
7
+
8
+ #define MAX_SHUTDOWN_TRIES 5
9
+
10
+ static rd_kafka_t *rk = NULL;
11
+
12
+ static void logger(const rd_kafka_t *rk, int level,
13
+ const char *fac, const char *buf)
14
+ {
15
+ fprintf(stderr, "%s: %s\n", fac, buf);
16
+ }
17
+
18
+
19
+ static VALUE kafka_send(VALUE self, VALUE topic_value, VALUE key, VALUE message)
20
+ {
21
+ rd_kafka_topic_conf_t *topic_conf;
22
+ rd_kafka_topic_t *topic;
23
+ char *topic_name;
24
+ void *message_bytes;
25
+ size_t message_len;
26
+ void *key_buf;
27
+ size_t key_len;
28
+ int res;
29
+
30
+ if (NIL_P(key)) {
31
+ key_buf = NULL;
32
+ key_len = 0;
33
+ } else {
34
+ key_buf = RSTRING_PTR(key);
35
+ key_len = RSTRING_LEN(key);
36
+ }
37
+
38
+ topic_name = StringValueCStr(topic_value);
39
+ if (!topic_name) {
40
+ rb_raise(rb_eStandardError, "topic is not a string!");
41
+ }
42
+
43
+ if(NIL_P(message)) {
44
+ message = NULL;
45
+ message_len = 0;
46
+ } else {
47
+ message_bytes = RSTRING_PTR(message);
48
+ if(!message_bytes) {
49
+ rb_raise(rb_eStandardError, "failed to get message ptr");
50
+ }
51
+ message_len = RSTRING_LEN(message);
52
+ }
53
+
54
+
55
+ topic_conf = rd_kafka_topic_conf_new();
56
+ if(!topic_conf) {
57
+ rb_raise(rb_eStandardError, "failed to create kafka topic configuration");
58
+ }
59
+
60
+ topic = rd_kafka_topic_new(rk, topic_name, topic_conf);
61
+ if(!topic) {
62
+ rb_raise(rb_eStandardError, "failed to create topic");
63
+ }
64
+
65
+ res = rd_kafka_produce(topic, RD_KAFKA_PARTITION_UA, RD_KAFKA_MSG_F_COPY, message_bytes, message_len,
66
+ key_buf, key_len, NULL);
67
+
68
+ rd_kafka_topic_destroy(topic);
69
+
70
+ if (res) {
71
+ rb_raise(rb_eStandardError, "rd_kafka_produce failed: %d", res);
72
+ }
73
+
74
+ return Qnil;
75
+ }
76
+
77
+ static VALUE kafka_destroy()
78
+ {
79
+ if(rk) {
80
+ int i;
81
+
82
+ for ( i = 0 ; i < MAX_SHUTDOWN_TRIES ; ++i ) {
83
+ if (rd_kafka_outq_len(rk) <= 0 ) {
84
+ break;
85
+ } else {
86
+ rd_kafka_poll(rk, 100);
87
+ }
88
+ }
89
+
90
+ rd_kafka_destroy(rk);
91
+ rk = NULL;
92
+ }
93
+ return Qnil;
94
+ }
95
+
96
+ static VALUE kafka_add_broker(VALUE self, VALUE broker)
97
+ {
98
+ char *value = StringValueCStr(broker);
99
+ int res;
100
+
101
+ if(!value) {
102
+ rb_raise(rb_eArgError, "invalid string");
103
+ }
104
+
105
+ res = rd_kafka_brokers_add(rk, value);
106
+ if (res == 0) {
107
+ rb_raise(rb_eStandardError, "failed to add any brokers!");
108
+ }
109
+
110
+ return Qnil;
111
+ }
112
+
113
+ static VALUE kafka_init()
114
+ {
115
+ rd_kafka_conf_t *conf;
116
+ char errstr[512];
117
+
118
+ if(!rk) {
119
+ kafka_destroy();
120
+ }
121
+ conf = rd_kafka_conf_new();
122
+
123
+ rd_kafka_conf_set_log_cb(conf, logger);
124
+
125
+ rk = rd_kafka_new(RD_KAFKA_PRODUCER, conf, errstr, sizeof(errstr));
126
+ if (!rk) {
127
+ rb_raise(rb_eStandardError, "failed to create kafka producer: %s\n", errstr);
128
+ }
129
+ return Qnil;
130
+ }
131
+
132
+ VALUE Init_ckafka()
133
+ {
134
+ VALUE kafka_module = rb_define_module("Ckafka");
135
+
136
+ rb_define_singleton_method(kafka_module, "init", kafka_init, 0);
137
+ rb_define_singleton_method(kafka_module, "produce", kafka_send, 3);
138
+ rb_define_singleton_method(kafka_module, "add_broker", kafka_add_broker, 1);
139
+ rb_define_singleton_method(kafka_module, "close", kafka_destroy, 0);
140
+
141
+ kafka_init();
142
+
143
+ return Qnil;
144
+ }
@@ -0,0 +1,9 @@
1
+ require 'mkmf'
2
+
3
+ dir_config('ckafka')
4
+
5
+ unless find_library('rdkafka', 'rd_kafka_topic_new')
6
+ abort "Cannot find librdkafka"
7
+ end
8
+
9
+ create_makefile('ckafka/ckafka')
@@ -0,0 +1,3 @@
1
+ module Ckafka
2
+ VERSION = "0.1.0"
3
+ end
data/lib/ckafka.rb ADDED
@@ -0,0 +1,5 @@
1
+ require "ckafka/version"
2
+ require 'ckafka/ckafka.bundle'
3
+
4
+ module Ckafka
5
+ end
metadata ADDED
@@ -0,0 +1,115 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ckafka
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Ian Quick
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-04-23 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.11'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.11'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake-compile
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: interface to the rdkafka C library
70
+ email:
71
+ - ian.quick@gmail.com
72
+ executables: []
73
+ extensions:
74
+ - ext/ckafka/extconf.rb
75
+ extra_rdoc_files: []
76
+ files:
77
+ - ".gitignore"
78
+ - ".rspec"
79
+ - ".travis.yml"
80
+ - Gemfile
81
+ - LICENSE.txt
82
+ - README.md
83
+ - Rakefile
84
+ - bin/console
85
+ - bin/setup
86
+ - ckafka.gemspec
87
+ - ext/ckafka/ckafka.c
88
+ - ext/ckafka/extconf.rb
89
+ - lib/ckafka.rb
90
+ - lib/ckafka/version.rb
91
+ homepage: https://github.com/ibawt/ckafka
92
+ licenses:
93
+ - MIT
94
+ metadata: {}
95
+ post_install_message:
96
+ rdoc_options: []
97
+ require_paths:
98
+ - lib
99
+ required_ruby_version: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ required_rubygems_version: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - ">="
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
109
+ requirements: []
110
+ rubyforge_project:
111
+ rubygems_version: 2.4.5.1
112
+ signing_key:
113
+ specification_version: 4
114
+ summary: Simple Kafka Producer
115
+ test_files: []