rks 0.3.2
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 +7 -0
- data/.gitignore +12 -0
- data/Gemfile +19 -0
- data/Gemfile.lock +78 -0
- data/LICENSE.txt +21 -0
- data/README.md +43 -0
- data/Rakefile +9 -0
- data/bin/console +14 -0
- data/bin/rks +4 -0
- data/bin/setup +8 -0
- data/lib/cli.rb +11 -0
- data/lib/rks.rb +31 -0
- data/lib/rks/application.rb +37 -0
- data/lib/rks/command.rb +4 -0
- data/lib/rks/command/base.rb +15 -0
- data/lib/rks/command/handler.rb +14 -0
- data/lib/rks/config/sidekiq.rb +5 -0
- data/lib/rks/event.rb +4 -0
- data/lib/rks/event/base.rb +15 -0
- data/lib/rks/event/handler.rb +23 -0
- data/lib/rks/event/processor.rb +43 -0
- data/lib/rks/kafka.rb +46 -0
- data/lib/rks/logger.rb +66 -0
- data/lib/rks/sidekiq.rb +10 -0
- data/lib/rks/support/concern.rb +10 -0
- data/lib/rks/support/configurable.rb +27 -0
- data/lib/rks/support/routable.rb +46 -0
- data/lib/rks/version.rb +3 -0
- data/lib/rks/worker.rb +6 -0
- data/rks.gemspec +49 -0
- metadata +243 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: eb3f2b451a062d1b9fe8b55a082ca359753480ee31cb4239ae4c37891dd0736a
|
|
4
|
+
data.tar.gz: 1462b96896ad291c7fd58d12e72f1b23c2a466d665b5ace10f0b62da556a8c2d
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 1107c7530647cb204df3ed5dac1773bcc07f482573dd9e7b202af3958e3f1c6c231e667f044cc469bd2704a2f9867542523c6532745ccf98087d40b8d2f1c918
|
|
7
|
+
data.tar.gz: 140bfb396c488e678d484678e5663ffd811713ac9b22a5bb86c93d7eb596897e8753daa1805da861d0449db1f58aa059dcdaa05788460f4a1a765b93753a65fb
|
data/.gitignore
ADDED
data/Gemfile
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
source "https://rubygems.org"
|
|
2
|
+
|
|
3
|
+
git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
|
|
4
|
+
source 'https://46617433:56bec501@gems.contribsys.com/' do
|
|
5
|
+
gem 'sidekiq-pro', '4.0.4'
|
|
6
|
+
end
|
|
7
|
+
|
|
8
|
+
gem 'byebug'
|
|
9
|
+
gem 'minitest'
|
|
10
|
+
gem 'concurrent-ruby', require: 'concurrent'
|
|
11
|
+
gem 'ruby-kafka'
|
|
12
|
+
gem 'avro_turf'
|
|
13
|
+
gem 'sidekiq'
|
|
14
|
+
gem 'logstash-logger'
|
|
15
|
+
gem 'poseidon'
|
|
16
|
+
gem 'simplecov', require: false
|
|
17
|
+
|
|
18
|
+
# Specify your gem's dependencies in rks.gemspec
|
|
19
|
+
gemspec
|
data/Gemfile.lock
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
PATH
|
|
2
|
+
remote: .
|
|
3
|
+
specs:
|
|
4
|
+
rks (0.3.1)
|
|
5
|
+
avro_turf (~> 0.8.1)
|
|
6
|
+
byebug (~> 10.0.2)
|
|
7
|
+
concurrent-ruby (~> 1.1.3)
|
|
8
|
+
logstash-logger (~> 0.26.1)
|
|
9
|
+
minitest (~> 5.11.3)
|
|
10
|
+
poseidon (~> 0.0.5)
|
|
11
|
+
ruby-kafka (~> 0.7.4)
|
|
12
|
+
sidekiq (~> 5.2.3)
|
|
13
|
+
sidekiq-pro (~> 4.0.4)
|
|
14
|
+
thor
|
|
15
|
+
|
|
16
|
+
GEM
|
|
17
|
+
remote: https://rubygems.org/
|
|
18
|
+
remote: https://46617433:56bec501@gems.contribsys.com/
|
|
19
|
+
specs:
|
|
20
|
+
avro (1.8.2)
|
|
21
|
+
multi_json
|
|
22
|
+
avro_turf (0.8.1)
|
|
23
|
+
avro (>= 1.7.7, < 1.9)
|
|
24
|
+
excon (~> 0.45)
|
|
25
|
+
byebug (10.0.2)
|
|
26
|
+
concurrent-ruby (1.1.3)
|
|
27
|
+
connection_pool (2.2.2)
|
|
28
|
+
digest-crc (0.4.1)
|
|
29
|
+
docile (1.3.1)
|
|
30
|
+
excon (0.62.0)
|
|
31
|
+
json (2.1.0)
|
|
32
|
+
logstash-event (1.2.02)
|
|
33
|
+
logstash-logger (0.26.1)
|
|
34
|
+
logstash-event (~> 1.2)
|
|
35
|
+
minitest (5.11.3)
|
|
36
|
+
multi_json (1.13.1)
|
|
37
|
+
poseidon (0.0.5)
|
|
38
|
+
rack (2.0.6)
|
|
39
|
+
rack-protection (2.0.4)
|
|
40
|
+
rack
|
|
41
|
+
rake (10.5.0)
|
|
42
|
+
redis (4.0.3)
|
|
43
|
+
ruby-kafka (0.7.4)
|
|
44
|
+
digest-crc
|
|
45
|
+
sidekiq (5.2.3)
|
|
46
|
+
connection_pool (~> 2.2, >= 2.2.2)
|
|
47
|
+
rack-protection (>= 1.5.0)
|
|
48
|
+
redis (>= 3.3.5, < 5)
|
|
49
|
+
sidekiq-pro (4.0.4)
|
|
50
|
+
concurrent-ruby (>= 1.0.5)
|
|
51
|
+
sidekiq (>= 5.2.1)
|
|
52
|
+
simplecov (0.16.1)
|
|
53
|
+
docile (~> 1.1)
|
|
54
|
+
json (>= 1.8, < 3)
|
|
55
|
+
simplecov-html (~> 0.10.0)
|
|
56
|
+
simplecov-html (0.10.2)
|
|
57
|
+
thor (0.20.3)
|
|
58
|
+
|
|
59
|
+
PLATFORMS
|
|
60
|
+
ruby
|
|
61
|
+
|
|
62
|
+
DEPENDENCIES
|
|
63
|
+
avro_turf
|
|
64
|
+
bundler (~> 1.16)
|
|
65
|
+
byebug
|
|
66
|
+
concurrent-ruby
|
|
67
|
+
logstash-logger
|
|
68
|
+
minitest
|
|
69
|
+
poseidon
|
|
70
|
+
rake (~> 10.0)
|
|
71
|
+
rks!
|
|
72
|
+
ruby-kafka
|
|
73
|
+
sidekiq
|
|
74
|
+
sidekiq-pro (= 4.0.4)!
|
|
75
|
+
simplecov
|
|
76
|
+
|
|
77
|
+
BUNDLED WITH
|
|
78
|
+
1.17.1
|
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2018 Sittitep Tosuwan
|
|
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,43 @@
|
|
|
1
|
+
# Rks
|
|
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/rks`. 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 'rks'
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
And then execute:
|
|
16
|
+
|
|
17
|
+
$ bundle
|
|
18
|
+
|
|
19
|
+
Or install it yourself as:
|
|
20
|
+
|
|
21
|
+
$ gem install rks
|
|
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]/rks. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
|
36
|
+
|
|
37
|
+
## License
|
|
38
|
+
|
|
39
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
|
40
|
+
|
|
41
|
+
## Code of Conduct
|
|
42
|
+
|
|
43
|
+
Everyone interacting in the Rks project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/rks/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
data/bin/console
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
#!/usr/bin/env ruby
|
|
2
|
+
|
|
3
|
+
require "bundler/setup"
|
|
4
|
+
require "rks"
|
|
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/rks
ADDED
data/bin/setup
ADDED
data/lib/cli.rb
ADDED
data/lib/rks.rb
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
require 'rubygems'
|
|
2
|
+
require 'bundler/setup'
|
|
3
|
+
require 'benchmark'
|
|
4
|
+
|
|
5
|
+
Bundler.require(:default, :development)
|
|
6
|
+
|
|
7
|
+
require 'rks/support/concern'
|
|
8
|
+
require 'rks/support/routable'
|
|
9
|
+
require 'rks/support/configurable'
|
|
10
|
+
|
|
11
|
+
require 'rks/command/base'
|
|
12
|
+
require 'rks/command/handler'
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
require 'rks/event/base'
|
|
16
|
+
require 'rks/event/handler'
|
|
17
|
+
require 'rks/event/processor'
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
require 'rks/logger'
|
|
21
|
+
require "rks/version"
|
|
22
|
+
require 'rks/kafka'
|
|
23
|
+
require 'rks/sidekiq'
|
|
24
|
+
|
|
25
|
+
require 'rks/application'
|
|
26
|
+
|
|
27
|
+
require 'rks/command'
|
|
28
|
+
require 'rks/event'
|
|
29
|
+
require 'rks/worker'
|
|
30
|
+
|
|
31
|
+
require 'rks/config/sidekiq'
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
require "avro_turf/messaging"
|
|
2
|
+
|
|
3
|
+
class Application
|
|
4
|
+
include RKS::Support::Configurable
|
|
5
|
+
|
|
6
|
+
config_attr name: "regular-rks-app", env: "dev"
|
|
7
|
+
|
|
8
|
+
class << self
|
|
9
|
+
def logger
|
|
10
|
+
@logger ||= RKS::Logger.init
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def avro_registry
|
|
14
|
+
@avro ||= AvroTurf::Messaging.new(
|
|
15
|
+
schemas_path: "./app/schemas",
|
|
16
|
+
registry_url: Application.config.avro_registry_url
|
|
17
|
+
)
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def run
|
|
21
|
+
Application.logger.info message: "Application started"
|
|
22
|
+
|
|
23
|
+
RKS::Event::Handler.router.routes.keys.each do |event_name|
|
|
24
|
+
topic = [config.env,event_name].join("-")
|
|
25
|
+
Kafka.consumer.subscribe(topic)
|
|
26
|
+
end
|
|
27
|
+
# This will loop indefinitely, yielding each message in turn.
|
|
28
|
+
Kafka.consumer.each_message do |message|
|
|
29
|
+
RKS::Event::Processor.process(correlation_id: message.key, event: sanitized_event_name(message.topic), payload: message.value)
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def sanitized_event_name(topic)
|
|
34
|
+
topic.gsub("#{config.env}-", "")
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
end
|
data/lib/rks/command.rb
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
module RKS
|
|
2
|
+
module Command
|
|
3
|
+
class Base
|
|
4
|
+
include RKS::Command
|
|
5
|
+
include RKS::Support::Configurable
|
|
6
|
+
|
|
7
|
+
attr_accessor :correlation_id, :args
|
|
8
|
+
|
|
9
|
+
def initialize(_args)
|
|
10
|
+
@correlation_id = _args[:correlation_id]
|
|
11
|
+
@args = _args[:args]
|
|
12
|
+
end
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
end
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
module RKS
|
|
2
|
+
module Command
|
|
3
|
+
module Handler
|
|
4
|
+
class << self
|
|
5
|
+
def call(correlation_id:, klass:, action:, args: nil)
|
|
6
|
+
Application.logger.with_rescue_and_duration_command(correlation_id, "#{klass}##{action}", args) do
|
|
7
|
+
instance = klass.new(correlation_id: correlation_id, args: args)
|
|
8
|
+
instance.send(action)
|
|
9
|
+
end
|
|
10
|
+
end
|
|
11
|
+
end
|
|
12
|
+
end
|
|
13
|
+
end
|
|
14
|
+
end
|
data/lib/rks/event.rb
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
module RKS
|
|
2
|
+
module Event
|
|
3
|
+
class Base
|
|
4
|
+
include RKS::Event
|
|
5
|
+
# include RKS::Support::Routable::Endpoint
|
|
6
|
+
|
|
7
|
+
attr_accessor :correlation_id, :payload
|
|
8
|
+
|
|
9
|
+
def initialize(args)
|
|
10
|
+
@correlation_id = args[:correlation_id]
|
|
11
|
+
@payload = args[:payload]
|
|
12
|
+
end
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
end
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
module RKS
|
|
2
|
+
module Event
|
|
3
|
+
module Handler
|
|
4
|
+
include RKS::Support::Routable
|
|
5
|
+
|
|
6
|
+
class << self
|
|
7
|
+
def call(correlation_id:, event:, payload:)
|
|
8
|
+
route = router.find(event)
|
|
9
|
+
decoded_payload = decode(payload: payload, options: route[:options])
|
|
10
|
+
route[:block].call(correlation_id, decoded_payload)
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def decode(payload:, options: {})
|
|
14
|
+
if options[:type] == "AVRO"
|
|
15
|
+
Application.avro_registry.decode(payload, schema_name: options[:avro][:schema_name], namespace: options[:avro][:namespace])
|
|
16
|
+
else
|
|
17
|
+
JSON.parse(payload)
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
require 'concurrent'
|
|
2
|
+
|
|
3
|
+
module RKS
|
|
4
|
+
module Event
|
|
5
|
+
class Processor
|
|
6
|
+
class ProcessorNotInitialized < StandardError; end;
|
|
7
|
+
|
|
8
|
+
attr_accessor :correlation_id, :event, :payload
|
|
9
|
+
|
|
10
|
+
def initialize(correlation_id:, event:, payload:)
|
|
11
|
+
@correlation_id = correlation_id
|
|
12
|
+
@event = event
|
|
13
|
+
@payload = payload
|
|
14
|
+
|
|
15
|
+
self.class.set_current_processor(self)
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def process
|
|
19
|
+
Application.logger.with_rescue_and_duration_event(@correlation_id, @event, @payload) do
|
|
20
|
+
RKS::Event::Handler.call(correlation_id: @correlation_id, event: @event, payload: @payload)
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
class << self
|
|
25
|
+
def set_current_processor(processor)
|
|
26
|
+
@current = Concurrent::ThreadLocalVar.new(processor)
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def current
|
|
30
|
+
if @current
|
|
31
|
+
@current.value
|
|
32
|
+
else
|
|
33
|
+
raise ProcessorNotInitialized, "#set_current_processor is needed"
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def process(args)
|
|
38
|
+
new(args).process
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
end
|
data/lib/rks/kafka.rb
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
require 'kafka'
|
|
2
|
+
|
|
3
|
+
Kafka.module_eval do
|
|
4
|
+
include RKS::Support::Configurable
|
|
5
|
+
|
|
6
|
+
class << self
|
|
7
|
+
def client(args = {})
|
|
8
|
+
if args[:new]
|
|
9
|
+
new(config.brokers)
|
|
10
|
+
else
|
|
11
|
+
@client ||= new(config.brokers)
|
|
12
|
+
end
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def producer
|
|
16
|
+
@producer ||= client.producer
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def consumer
|
|
20
|
+
@consumer ||= client.consumer(group_id: config.consumer_group_id)
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
Kafka::Producer.class_eval do
|
|
26
|
+
alias_method 'original_produce', 'produce'
|
|
27
|
+
|
|
28
|
+
def produce(*args)
|
|
29
|
+
encoding = args[1][:encoding] == true || args[1][:encoding] == nil
|
|
30
|
+
payload, topic, encoding = JSON.parse(JSON.dump(args[0])), args[1][:topic]
|
|
31
|
+
args[1][:topic] = [Application.config.env, topic].join("-")
|
|
32
|
+
args[1].delete(:encoding)
|
|
33
|
+
|
|
34
|
+
payload = if encoding
|
|
35
|
+
Application.avro_registry.encode(payload, schema_name: camelize(topic))
|
|
36
|
+
else
|
|
37
|
+
JSON.dump(payload)
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
original_produce(payload, **args[1])
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def camelize(str)
|
|
44
|
+
str.split('-').collect(&:capitalize).join
|
|
45
|
+
end
|
|
46
|
+
end
|
data/lib/rks/logger.rb
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
require 'logstash-logger'
|
|
2
|
+
|
|
3
|
+
RKS::Logger = LogStashLogger
|
|
4
|
+
RKS::Logger.module_eval do
|
|
5
|
+
class << self
|
|
6
|
+
def init(args = {})
|
|
7
|
+
new_args = {
|
|
8
|
+
buffer_max_items: 5000,
|
|
9
|
+
buffer_max_interval: 1,
|
|
10
|
+
type: :multi_logger,
|
|
11
|
+
outputs: [
|
|
12
|
+
{type: :stdout}
|
|
13
|
+
# {type: :file, path: "log/#{ENV['RKS_ENV']}.log"} if ENV["LOG_FILE"]
|
|
14
|
+
]
|
|
15
|
+
}.merge!(args)
|
|
16
|
+
new(new_args)
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
RKS::Logger.configure do |config|
|
|
22
|
+
config.customize_event do |event|
|
|
23
|
+
event["app"] = Application.config.name
|
|
24
|
+
event["env"] = Application.config.env
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
LogStashLogger::MultiLogger.class_eval do
|
|
29
|
+
def with_rescue_and_duration_event(correlation_id, event, payload)
|
|
30
|
+
begin
|
|
31
|
+
info correlation_id: correlation_id, status: "started", event: event, payload: payload
|
|
32
|
+
rescue Encoding::UndefinedConversionError
|
|
33
|
+
info correlation_id: correlation_id, status: "started", event: event
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
duration = Benchmark.measure { @result = yield }
|
|
37
|
+
info correlation_id: correlation_id, status: "finished", event: event, duration: duration.real.round(3)
|
|
38
|
+
|
|
39
|
+
@result
|
|
40
|
+
rescue Exception => e
|
|
41
|
+
Application.logger.fatal correlation_id: correlation_id, status: "failed", event: event, error_name: e.class.to_s, error_message: e.message, error_detail: e.backtrace
|
|
42
|
+
nil
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def with_rescue_and_duration_command(correlation_id, actor, args)
|
|
46
|
+
info correlation_id: correlation_id, status: "started", command: actor, args: args
|
|
47
|
+
duration = Benchmark.measure { @result = yield }
|
|
48
|
+
info correlation_id: correlation_id, status: "finished", command: actor, duration: duration.real.round(3)
|
|
49
|
+
|
|
50
|
+
@result
|
|
51
|
+
rescue Exception => e
|
|
52
|
+
Application.logger.fatal correlation_id: correlation_id, status: "failed", command: actor, error_name: e.class.to_s, error_message: e.message, error_detail: e.backtrace
|
|
53
|
+
nil
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def with_rescue_and_duration_worker(correlation_id, worker, args, jid)
|
|
57
|
+
info correlation_id: correlation_id, status: "started", worker: worker, jid: jid, args: args
|
|
58
|
+
duration = Benchmark.measure { @result = yield }
|
|
59
|
+
info correlation_id: correlation_id, status: "finished", worker: worker, jid: jid, duration: duration.real.round(3)
|
|
60
|
+
|
|
61
|
+
@result
|
|
62
|
+
rescue Exception => e
|
|
63
|
+
Application.logger.fatal correlation_id: correlation_id, status: "failed", worker: worker, jid: jid, error_name: e.class.to_s, error_message: e.message, error_detail: e.backtrace
|
|
64
|
+
nil
|
|
65
|
+
end
|
|
66
|
+
end
|
data/lib/rks/sidekiq.rb
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
require 'sidekiq/processor'
|
|
2
|
+
|
|
3
|
+
Sidekiq::Processor.class_eval do
|
|
4
|
+
def execute_job(worker, cloned_args)
|
|
5
|
+
correlation_id = cloned_args[0]["correlation_id"]
|
|
6
|
+
Application.logger.with_rescue_and_duration_worker(correlation_id, worker.class.name, cloned_args[0], worker.jid) do
|
|
7
|
+
worker.perform(*cloned_args)
|
|
8
|
+
end
|
|
9
|
+
end
|
|
10
|
+
end
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
module RKS
|
|
2
|
+
module Support
|
|
3
|
+
module Configurable
|
|
4
|
+
extend RKS::Support::Concern
|
|
5
|
+
|
|
6
|
+
class InvalidConfugurationName < StandardError; end;
|
|
7
|
+
|
|
8
|
+
module ClassMethods
|
|
9
|
+
def config
|
|
10
|
+
@config ||= OpenStruct.new
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def config_attr(configs)
|
|
14
|
+
if configs.class.to_s == "Hash"
|
|
15
|
+
configs.each { |k,v| config.send("#{k.to_s}=", v) }
|
|
16
|
+
else
|
|
17
|
+
raise InvalidConfugurationName, "#{configs.class.to_s} is not allowed to be a configuration"
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def configure
|
|
22
|
+
yield config
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
end
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
module RKS
|
|
2
|
+
module Support
|
|
3
|
+
module Routable
|
|
4
|
+
class NoMethodFound < StandardError; end;
|
|
5
|
+
|
|
6
|
+
extend RKS::Support::Concern
|
|
7
|
+
|
|
8
|
+
module ClassMethods
|
|
9
|
+
def router
|
|
10
|
+
@router ||= Router.new(owner: self.to_s)
|
|
11
|
+
end
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
class Router
|
|
15
|
+
class RouteNotFound < StandardError; end;
|
|
16
|
+
|
|
17
|
+
attr_accessor :owner, :routes
|
|
18
|
+
|
|
19
|
+
def initialize(owner:)
|
|
20
|
+
@owner = owner
|
|
21
|
+
@routes = {}
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def find(name)
|
|
25
|
+
if route = routes[name]
|
|
26
|
+
route
|
|
27
|
+
else
|
|
28
|
+
raise RouteNotFound, "#{name} is not found in #{owner} routes"
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def draw
|
|
33
|
+
yield(self)
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def on(name, to:, options: {})
|
|
37
|
+
klass_name, action = to.split('#')
|
|
38
|
+
klass = Object.const_get(klass_name)
|
|
39
|
+
|
|
40
|
+
block = Proc.new { |correlation_id, payload| klass.new(correlation_id: correlation_id, payload: payload).send(action.to_sym) }
|
|
41
|
+
routes.merge!({name => {block: block, options: options}})
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
end
|
data/lib/rks/version.rb
ADDED
data/lib/rks/worker.rb
ADDED
data/rks.gemspec
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
|
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
4
|
+
require "rks/version"
|
|
5
|
+
|
|
6
|
+
Gem::Specification.new do |spec|
|
|
7
|
+
spec.name = "rks"
|
|
8
|
+
spec.version = RKS::VERSION
|
|
9
|
+
spec.authors = ["Sittitep Tosuwan"]
|
|
10
|
+
spec.email = ["sittitep.tosuwan@gmail.com"]
|
|
11
|
+
|
|
12
|
+
spec.summary = "Ruby Kafka Sidekiq"
|
|
13
|
+
spec.description = "Ruby Kafka Sidekiq"
|
|
14
|
+
spec.homepage = "http://not.now"
|
|
15
|
+
spec.license = "MIT"
|
|
16
|
+
|
|
17
|
+
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
|
|
18
|
+
# to allow pushing to a single host or delete this section to allow pushing to any host.
|
|
19
|
+
# if spec.respond_to?(:metadata)
|
|
20
|
+
# spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
|
|
21
|
+
# else
|
|
22
|
+
# raise "RubyGems 2.0 or newer is required to protect against " \
|
|
23
|
+
# "public gem pushes."
|
|
24
|
+
# end
|
|
25
|
+
|
|
26
|
+
# Specify which files should be added to the gem when it is released.
|
|
27
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
|
28
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
|
29
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
|
30
|
+
end
|
|
31
|
+
spec.bindir = "bin"
|
|
32
|
+
spec.executables = ["rks"]
|
|
33
|
+
spec.require_paths = ["lib", "test"]
|
|
34
|
+
|
|
35
|
+
spec.add_development_dependency "bundler", "~> 1.16"
|
|
36
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
|
37
|
+
|
|
38
|
+
spec.add_dependency "avro_turf", "~> 0.8.1"
|
|
39
|
+
spec.add_dependency "byebug", "~> 10.0.2"
|
|
40
|
+
spec.add_dependency "concurrent-ruby", "~> 1.1.3"
|
|
41
|
+
spec.add_dependency "logstash-logger", "~> 0.26.1"
|
|
42
|
+
spec.add_dependency "minitest", "~> 5.11.3"
|
|
43
|
+
spec.add_dependency "poseidon", "~> 0.0.5"
|
|
44
|
+
spec.add_dependency "ruby-kafka", "~> 0.7.4"
|
|
45
|
+
spec.add_dependency "sidekiq", "~> 5.2.3"
|
|
46
|
+
spec.add_dependency "sidekiq-pro", "~> 4.0.4"
|
|
47
|
+
|
|
48
|
+
spec.add_runtime_dependency "thor"
|
|
49
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,243 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: rks
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.3.2
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Sittitep Tosuwan
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2018-11-27 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.16'
|
|
20
|
+
type: :development
|
|
21
|
+
prerelease: false
|
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
23
|
+
requirements:
|
|
24
|
+
- - "~>"
|
|
25
|
+
- !ruby/object:Gem::Version
|
|
26
|
+
version: '1.16'
|
|
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: avro_turf
|
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
|
44
|
+
requirements:
|
|
45
|
+
- - "~>"
|
|
46
|
+
- !ruby/object:Gem::Version
|
|
47
|
+
version: 0.8.1
|
|
48
|
+
type: :runtime
|
|
49
|
+
prerelease: false
|
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
51
|
+
requirements:
|
|
52
|
+
- - "~>"
|
|
53
|
+
- !ruby/object:Gem::Version
|
|
54
|
+
version: 0.8.1
|
|
55
|
+
- !ruby/object:Gem::Dependency
|
|
56
|
+
name: byebug
|
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
|
58
|
+
requirements:
|
|
59
|
+
- - "~>"
|
|
60
|
+
- !ruby/object:Gem::Version
|
|
61
|
+
version: 10.0.2
|
|
62
|
+
type: :runtime
|
|
63
|
+
prerelease: false
|
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
65
|
+
requirements:
|
|
66
|
+
- - "~>"
|
|
67
|
+
- !ruby/object:Gem::Version
|
|
68
|
+
version: 10.0.2
|
|
69
|
+
- !ruby/object:Gem::Dependency
|
|
70
|
+
name: concurrent-ruby
|
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
|
72
|
+
requirements:
|
|
73
|
+
- - "~>"
|
|
74
|
+
- !ruby/object:Gem::Version
|
|
75
|
+
version: 1.1.3
|
|
76
|
+
type: :runtime
|
|
77
|
+
prerelease: false
|
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
79
|
+
requirements:
|
|
80
|
+
- - "~>"
|
|
81
|
+
- !ruby/object:Gem::Version
|
|
82
|
+
version: 1.1.3
|
|
83
|
+
- !ruby/object:Gem::Dependency
|
|
84
|
+
name: logstash-logger
|
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
|
86
|
+
requirements:
|
|
87
|
+
- - "~>"
|
|
88
|
+
- !ruby/object:Gem::Version
|
|
89
|
+
version: 0.26.1
|
|
90
|
+
type: :runtime
|
|
91
|
+
prerelease: false
|
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
93
|
+
requirements:
|
|
94
|
+
- - "~>"
|
|
95
|
+
- !ruby/object:Gem::Version
|
|
96
|
+
version: 0.26.1
|
|
97
|
+
- !ruby/object:Gem::Dependency
|
|
98
|
+
name: minitest
|
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
|
100
|
+
requirements:
|
|
101
|
+
- - "~>"
|
|
102
|
+
- !ruby/object:Gem::Version
|
|
103
|
+
version: 5.11.3
|
|
104
|
+
type: :runtime
|
|
105
|
+
prerelease: false
|
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
107
|
+
requirements:
|
|
108
|
+
- - "~>"
|
|
109
|
+
- !ruby/object:Gem::Version
|
|
110
|
+
version: 5.11.3
|
|
111
|
+
- !ruby/object:Gem::Dependency
|
|
112
|
+
name: poseidon
|
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
|
114
|
+
requirements:
|
|
115
|
+
- - "~>"
|
|
116
|
+
- !ruby/object:Gem::Version
|
|
117
|
+
version: 0.0.5
|
|
118
|
+
type: :runtime
|
|
119
|
+
prerelease: false
|
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
121
|
+
requirements:
|
|
122
|
+
- - "~>"
|
|
123
|
+
- !ruby/object:Gem::Version
|
|
124
|
+
version: 0.0.5
|
|
125
|
+
- !ruby/object:Gem::Dependency
|
|
126
|
+
name: ruby-kafka
|
|
127
|
+
requirement: !ruby/object:Gem::Requirement
|
|
128
|
+
requirements:
|
|
129
|
+
- - "~>"
|
|
130
|
+
- !ruby/object:Gem::Version
|
|
131
|
+
version: 0.7.4
|
|
132
|
+
type: :runtime
|
|
133
|
+
prerelease: false
|
|
134
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
135
|
+
requirements:
|
|
136
|
+
- - "~>"
|
|
137
|
+
- !ruby/object:Gem::Version
|
|
138
|
+
version: 0.7.4
|
|
139
|
+
- !ruby/object:Gem::Dependency
|
|
140
|
+
name: sidekiq
|
|
141
|
+
requirement: !ruby/object:Gem::Requirement
|
|
142
|
+
requirements:
|
|
143
|
+
- - "~>"
|
|
144
|
+
- !ruby/object:Gem::Version
|
|
145
|
+
version: 5.2.3
|
|
146
|
+
type: :runtime
|
|
147
|
+
prerelease: false
|
|
148
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
149
|
+
requirements:
|
|
150
|
+
- - "~>"
|
|
151
|
+
- !ruby/object:Gem::Version
|
|
152
|
+
version: 5.2.3
|
|
153
|
+
- !ruby/object:Gem::Dependency
|
|
154
|
+
name: sidekiq-pro
|
|
155
|
+
requirement: !ruby/object:Gem::Requirement
|
|
156
|
+
requirements:
|
|
157
|
+
- - "~>"
|
|
158
|
+
- !ruby/object:Gem::Version
|
|
159
|
+
version: 4.0.4
|
|
160
|
+
type: :runtime
|
|
161
|
+
prerelease: false
|
|
162
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
163
|
+
requirements:
|
|
164
|
+
- - "~>"
|
|
165
|
+
- !ruby/object:Gem::Version
|
|
166
|
+
version: 4.0.4
|
|
167
|
+
- !ruby/object:Gem::Dependency
|
|
168
|
+
name: thor
|
|
169
|
+
requirement: !ruby/object:Gem::Requirement
|
|
170
|
+
requirements:
|
|
171
|
+
- - ">="
|
|
172
|
+
- !ruby/object:Gem::Version
|
|
173
|
+
version: '0'
|
|
174
|
+
type: :runtime
|
|
175
|
+
prerelease: false
|
|
176
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
177
|
+
requirements:
|
|
178
|
+
- - ">="
|
|
179
|
+
- !ruby/object:Gem::Version
|
|
180
|
+
version: '0'
|
|
181
|
+
description: Ruby Kafka Sidekiq
|
|
182
|
+
email:
|
|
183
|
+
- sittitep.tosuwan@gmail.com
|
|
184
|
+
executables:
|
|
185
|
+
- rks
|
|
186
|
+
extensions: []
|
|
187
|
+
extra_rdoc_files: []
|
|
188
|
+
files:
|
|
189
|
+
- ".gitignore"
|
|
190
|
+
- Gemfile
|
|
191
|
+
- Gemfile.lock
|
|
192
|
+
- LICENSE.txt
|
|
193
|
+
- README.md
|
|
194
|
+
- Rakefile
|
|
195
|
+
- bin/console
|
|
196
|
+
- bin/rks
|
|
197
|
+
- bin/setup
|
|
198
|
+
- lib/cli.rb
|
|
199
|
+
- lib/rks.rb
|
|
200
|
+
- lib/rks/application.rb
|
|
201
|
+
- lib/rks/command.rb
|
|
202
|
+
- lib/rks/command/base.rb
|
|
203
|
+
- lib/rks/command/handler.rb
|
|
204
|
+
- lib/rks/config/sidekiq.rb
|
|
205
|
+
- lib/rks/event.rb
|
|
206
|
+
- lib/rks/event/base.rb
|
|
207
|
+
- lib/rks/event/handler.rb
|
|
208
|
+
- lib/rks/event/processor.rb
|
|
209
|
+
- lib/rks/kafka.rb
|
|
210
|
+
- lib/rks/logger.rb
|
|
211
|
+
- lib/rks/sidekiq.rb
|
|
212
|
+
- lib/rks/support/concern.rb
|
|
213
|
+
- lib/rks/support/configurable.rb
|
|
214
|
+
- lib/rks/support/routable.rb
|
|
215
|
+
- lib/rks/version.rb
|
|
216
|
+
- lib/rks/worker.rb
|
|
217
|
+
- rks.gemspec
|
|
218
|
+
homepage: http://not.now
|
|
219
|
+
licenses:
|
|
220
|
+
- MIT
|
|
221
|
+
metadata: {}
|
|
222
|
+
post_install_message:
|
|
223
|
+
rdoc_options: []
|
|
224
|
+
require_paths:
|
|
225
|
+
- lib
|
|
226
|
+
- test
|
|
227
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
228
|
+
requirements:
|
|
229
|
+
- - ">="
|
|
230
|
+
- !ruby/object:Gem::Version
|
|
231
|
+
version: '0'
|
|
232
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
233
|
+
requirements:
|
|
234
|
+
- - ">="
|
|
235
|
+
- !ruby/object:Gem::Version
|
|
236
|
+
version: '0'
|
|
237
|
+
requirements: []
|
|
238
|
+
rubyforge_project:
|
|
239
|
+
rubygems_version: 2.7.7
|
|
240
|
+
signing_key:
|
|
241
|
+
specification_version: 4
|
|
242
|
+
summary: Ruby Kafka Sidekiq
|
|
243
|
+
test_files: []
|