eventsourcer 0.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: ac058eea62551794983eb02cd440f9bb2c77b5ee
4
+ data.tar.gz: bed17d5c7ce1cb3e825cd50dc0f9c39bf70d3b40
5
+ SHA512:
6
+ metadata.gz: 77577db2ed19afd3b27ef743a989ab0a3abb4b95faaea333213689b9afda8d7785c9bcd0bf20921abf563ce2824511ec04e71b0d959e27395a61a91c819044ea
7
+ data.tar.gz: 6ac71cb73df6a931d73fe5a5d173494d2836abe3082cd73495301c7c8c07f787f6baa225ea919c5afe102aa3e17a0e5196160a9656a54475097388639c12f744
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.4.0
5
+ before_install: gem install bundler -v 1.16.1
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,37 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ eventsourcer (0.1.0)
5
+ ruby-kafka (~> 0.5)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ diff-lcs (1.3)
11
+ rake (10.5.0)
12
+ rspec (3.7.0)
13
+ rspec-core (~> 3.7.0)
14
+ rspec-expectations (~> 3.7.0)
15
+ rspec-mocks (~> 3.7.0)
16
+ rspec-core (3.7.0)
17
+ rspec-support (~> 3.7.0)
18
+ rspec-expectations (3.7.0)
19
+ diff-lcs (>= 1.2.0, < 2.0)
20
+ rspec-support (~> 3.7.0)
21
+ rspec-mocks (3.7.0)
22
+ diff-lcs (>= 1.2.0, < 2.0)
23
+ rspec-support (~> 3.7.0)
24
+ rspec-support (3.7.0)
25
+ ruby-kafka (0.5.1)
26
+
27
+ PLATFORMS
28
+ ruby
29
+
30
+ DEPENDENCIES
31
+ bundler (~> 1.16)
32
+ eventsourcer!
33
+ rake (~> 10.0)
34
+ rspec (~> 3.0)
35
+
36
+ BUNDLED WITH
37
+ 1.16.1
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Brian Miller
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,45 @@
1
+ # Eventsourcer
2
+
3
+ A helper library for event sourcing Rails ActiveRecord model changes to Kafka(more brokers coming soon).
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'eventsourcer'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ ## Usage
18
+
19
+ In initializer configure Eventsourcer:
20
+ ```ruby
21
+ Eventsourcer.configure do |config|
22
+ config.broker = :kafka #required
23
+ config.kafka_seed_brokers = ["localhost:9092"] #optional
24
+ config.kafka_max_queue_size = 10000 #optional
25
+ end
26
+ ```
27
+
28
+ Add Eventsourcer to a model you would like to track:
29
+ ```ruby
30
+ require "eventsourcer"
31
+ class User < ApplicationRecord
32
+ include Eventsourcer
33
+ end
34
+ ```
35
+
36
+
37
+ ## Development
38
+
39
+ 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.
40
+
41
+ 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).
42
+
43
+ ## Contributing
44
+
45
+ Bug reports and pull requests are welcome on GitHub at https://github.com/samdvr/eventsourcer.
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "eventsourcer"
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(__FILE__)
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
@@ -0,0 +1,24 @@
1
+ lib = File.expand_path("../lib", __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require "eventsourcer/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "eventsourcer"
7
+ spec.version = Eventsourcer::VERSION
8
+ spec.authors = ["Sam Davarnia"]
9
+
10
+ spec.summary = %q{Library to event source ActiveRecord changes to a broker.}
11
+ spec.description = %q{This library publishes tracked ActiveRecord changes to a persistence layer such as Apache Kafka.}
12
+ spec.homepage = "https://github.com/samdvr/eventsourcer"
13
+ spec.license = "MIT"
14
+
15
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
16
+ spec.bindir = "exe"
17
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
18
+ spec.require_paths = ["lib"]
19
+ spec.add_dependency "ruby-kafka", "~> 0.5"
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.16"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_development_dependency "rspec", "~> 3.0"
24
+ end
@@ -0,0 +1,36 @@
1
+ require 'eventsourcer/version'
2
+ require 'eventsourcer/broker_factory'
3
+ require 'eventsourcer/errors'
4
+ require 'eventsourcer/brokers/base'
5
+ require 'eventsourcer/brokers/kafka_broker'
6
+
7
+ module Eventsourcer
8
+ class << self
9
+ attr_accessor :configuration
10
+ end
11
+
12
+ def self.configure
13
+ self.configuration ||= Configuration.new
14
+ yield(configuration)
15
+ end
16
+
17
+ class Configuration
18
+ attr_accessor :broker, :logger, :kafka_seed_brokers, :kafka_max_queue_size
19
+
20
+ def initialize
21
+ @broker = nil
22
+ @logger = nil
23
+ @kafka_seed_brokers = nil
24
+ @kafka_max_queue_size = nil
25
+ end
26
+ end
27
+
28
+ def self.included(base)
29
+ base.send :after_commit, :publish if base.respond_to?(:after_commit)
30
+ end
31
+
32
+ def publish
33
+ @publisher ||= Eventsourcer::BrokerFactory.build
34
+ @publisher.publish(table_name: self.class.table_name, previous_changes: previous_changes)
35
+ end
36
+ end
@@ -0,0 +1,17 @@
1
+ module Eventsourcer
2
+ class BrokerFactory
3
+ def self.build
4
+ raise Eventsourcer::Errors::BrokerNotConfiguredError unless Eventsourcer.configuration
5
+ return fetch_broker_klass(Eventsourcer.configuration.broker)
6
+ end
7
+
8
+ def self.fetch_broker_klass(configured_broker)
9
+ if configured_broker == :kafka
10
+ return Eventsourcer::Brokers::KafkaBroker
11
+ else
12
+ raise Eventsourcer::Errors::BrokerNotFound
13
+ end
14
+ end
15
+ end
16
+
17
+ end
@@ -0,0 +1,9 @@
1
+ module Eventsourcer
2
+ module Brokers
3
+ class Base
4
+ def self.publish(table_name:, previous_changes:)
5
+ raise NotImplementedError
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,41 @@
1
+ require "securerandom"
2
+ require "json"
3
+ require "kafka"
4
+
5
+ module Eventsourcer
6
+ module Brokers
7
+ class KafkaBroker < Base
8
+ @mutex = Mutex.new
9
+
10
+ def self.publish(table_name:, previous_changes:)
11
+ producer = client.async_producer(max_queue_size: Eventsourcer.configuration.kafka_max_queue_size || 10000)
12
+ begin
13
+ produce_to_kafka(producer, table_name, previous_changes.to_json)
14
+ rescue Kafka::BufferOverflow
15
+ producer.deliver_messages
16
+ produce_to_kafka(producer, table_name, previous_changes.to_json)
17
+ end
18
+
19
+ producer.deliver_messages
20
+ producer.shutdown
21
+ end
22
+
23
+ private
24
+
25
+ def self.produce_to_kafka(producer, table_name, previous_changes_json)
26
+ producer.produce(previous_changes_json,
27
+ topic: table_name,
28
+ key: SecureRandom.uuid)
29
+ end
30
+
31
+ def self.client
32
+ @mutex.synchronize do
33
+ @client ||= Kafka.new(
34
+ seed_brokers: Eventsourcer.configuration.kafka_seed_brokers || ["localhost:9092"],
35
+ logger: Eventsourcer.configuration.logger || Rails.logger
36
+ )
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,12 @@
1
+ module Eventsourcer
2
+ module Errors
3
+ class Base < StandardError
4
+ end
5
+
6
+ class BrokerNotConfiguredError < Base
7
+ end
8
+
9
+ class BrokerNotFound < Base
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,3 @@
1
+ module Eventsourcer
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,117 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: eventsourcer
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Sam Davarnia
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-12-30 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: ruby-kafka
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.5'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.5'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.16'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.16'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ description: This library publishes tracked ActiveRecord changes to a persistence
70
+ layer such as Apache Kafka.
71
+ email:
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".rspec"
78
+ - ".travis.yml"
79
+ - Gemfile
80
+ - Gemfile.lock
81
+ - LICENSE.txt
82
+ - README.md
83
+ - Rakefile
84
+ - bin/console
85
+ - bin/setup
86
+ - eventsourcer.gemspec
87
+ - lib/eventsourcer.rb
88
+ - lib/eventsourcer/broker_factory.rb
89
+ - lib/eventsourcer/brokers/base.rb
90
+ - lib/eventsourcer/brokers/kafka_broker.rb
91
+ - lib/eventsourcer/errors.rb
92
+ - lib/eventsourcer/version.rb
93
+ homepage: https://github.com/samdvr/eventsourcer
94
+ licenses:
95
+ - MIT
96
+ metadata: {}
97
+ post_install_message:
98
+ rdoc_options: []
99
+ require_paths:
100
+ - lib
101
+ required_ruby_version: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - ">="
104
+ - !ruby/object:Gem::Version
105
+ version: '0'
106
+ required_rubygems_version: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ requirements: []
112
+ rubyforge_project:
113
+ rubygems_version: 2.6.8
114
+ signing_key:
115
+ specification_version: 4
116
+ summary: Library to event source ActiveRecord changes to a broker.
117
+ test_files: []