karafka 1.4.10 → 2.0.0.alpha2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- checksums.yaml.gz.sig +0 -0
- data/.github/FUNDING.yml +3 -0
- data/.github/workflows/ci.yml +79 -26
- data/.ruby-version +1 -1
- data/CHANGELOG.md +46 -0
- data/CONTRIBUTING.md +6 -6
- data/Gemfile +6 -0
- data/Gemfile.lock +45 -53
- data/LICENSE +17 -0
- data/LICENSE-COMM +89 -0
- data/LICENSE-LGPL +165 -0
- data/README.md +16 -48
- data/bin/benchmarks +85 -0
- data/bin/create_token +28 -0
- data/bin/integrations +160 -0
- data/bin/karafka +4 -0
- data/bin/stress +13 -0
- data/certs/karafka-pro.pem +11 -0
- data/config/errors.yml +5 -38
- data/docker-compose.yml +12 -3
- data/karafka.gemspec +14 -14
- data/lib/active_job/karafka.rb +20 -0
- data/lib/active_job/queue_adapters/karafka_adapter.rb +26 -0
- data/lib/karafka/active_job/consumer.rb +24 -0
- data/lib/karafka/active_job/dispatcher.rb +38 -0
- data/lib/karafka/active_job/job_extensions.rb +34 -0
- data/lib/karafka/active_job/job_options_contract.rb +15 -0
- data/lib/karafka/active_job/routing_extensions.rb +18 -0
- data/lib/karafka/app.rb +14 -20
- data/lib/karafka/base_consumer.rb +103 -34
- data/lib/karafka/cli/base.rb +4 -4
- data/lib/karafka/cli/info.rb +44 -9
- data/lib/karafka/cli/install.rb +3 -8
- data/lib/karafka/cli/server.rb +16 -43
- data/lib/karafka/cli.rb +4 -11
- data/lib/karafka/connection/client.rb +279 -93
- data/lib/karafka/connection/listener.rb +137 -38
- data/lib/karafka/connection/messages_buffer.rb +57 -0
- data/lib/karafka/connection/pauses_manager.rb +46 -0
- data/lib/karafka/connection/rebalance_manager.rb +62 -0
- data/lib/karafka/contracts/base.rb +23 -0
- data/lib/karafka/contracts/config.rb +44 -8
- data/lib/karafka/contracts/consumer_group.rb +1 -176
- data/lib/karafka/contracts/consumer_group_topic.rb +16 -8
- data/lib/karafka/contracts/server_cli_options.rb +2 -12
- data/lib/karafka/contracts.rb +1 -1
- data/lib/karafka/env.rb +46 -0
- data/lib/karafka/errors.rb +18 -18
- data/lib/karafka/helpers/multi_delegator.rb +2 -2
- data/lib/karafka/instrumentation/callbacks/error.rb +40 -0
- data/lib/karafka/instrumentation/callbacks/statistics.rb +42 -0
- data/lib/karafka/instrumentation/monitor.rb +14 -21
- data/lib/karafka/instrumentation/stdout_listener.rb +67 -91
- data/lib/karafka/instrumentation.rb +21 -0
- data/lib/karafka/licenser.rb +76 -0
- data/lib/karafka/{params → messages}/batch_metadata.rb +9 -13
- data/lib/karafka/messages/builders/batch_metadata.rb +52 -0
- data/lib/karafka/messages/builders/message.rb +38 -0
- data/lib/karafka/messages/builders/messages.rb +40 -0
- data/lib/karafka/{params/params.rb → messages/message.rb} +7 -12
- data/lib/karafka/messages/messages.rb +64 -0
- data/lib/karafka/{params → messages}/metadata.rb +4 -6
- data/lib/karafka/messages/seek.rb +9 -0
- data/lib/karafka/patches/rdkafka/consumer.rb +22 -0
- data/lib/karafka/pro/active_job/dispatcher.rb +58 -0
- data/lib/karafka/pro/active_job/job_options_contract.rb +27 -0
- data/lib/karafka/pro/loader.rb +29 -0
- data/lib/karafka/pro.rb +13 -0
- data/lib/karafka/processing/executor.rb +96 -0
- data/lib/karafka/processing/executors_buffer.rb +49 -0
- data/lib/karafka/processing/jobs/base.rb +18 -0
- data/lib/karafka/processing/jobs/consume.rb +28 -0
- data/lib/karafka/processing/jobs/revoked.rb +22 -0
- data/lib/karafka/processing/jobs/shutdown.rb +23 -0
- data/lib/karafka/processing/jobs_queue.rb +121 -0
- data/lib/karafka/processing/worker.rb +57 -0
- data/lib/karafka/processing/workers_batch.rb +22 -0
- data/lib/karafka/railtie.rb +75 -0
- data/lib/karafka/routing/builder.rb +15 -24
- data/lib/karafka/routing/consumer_group.rb +10 -18
- data/lib/karafka/routing/consumer_mapper.rb +1 -2
- data/lib/karafka/routing/router.rb +1 -1
- data/lib/karafka/routing/subscription_group.rb +53 -0
- data/lib/karafka/routing/subscription_groups_builder.rb +51 -0
- data/lib/karafka/routing/topic.rb +47 -25
- data/lib/karafka/runner.rb +59 -0
- data/lib/karafka/serialization/json/deserializer.rb +6 -15
- data/lib/karafka/server.rb +62 -25
- data/lib/karafka/setup/config.rb +98 -171
- data/lib/karafka/status.rb +13 -3
- data/lib/karafka/templates/example_consumer.rb.erb +16 -0
- data/lib/karafka/templates/karafka.rb.erb +14 -50
- data/lib/karafka/time_trackers/base.rb +19 -0
- data/lib/karafka/time_trackers/pause.rb +84 -0
- data/lib/karafka/time_trackers/poll.rb +65 -0
- data/lib/karafka/version.rb +1 -1
- data/lib/karafka.rb +35 -13
- data.tar.gz.sig +0 -0
- metadata +82 -104
- metadata.gz.sig +0 -0
- data/MIT-LICENCE +0 -18
- data/lib/karafka/assignment_strategies/round_robin.rb +0 -13
- data/lib/karafka/attributes_map.rb +0 -63
- data/lib/karafka/backends/inline.rb +0 -16
- data/lib/karafka/base_responder.rb +0 -226
- data/lib/karafka/cli/flow.rb +0 -48
- data/lib/karafka/cli/missingno.rb +0 -19
- data/lib/karafka/code_reloader.rb +0 -67
- data/lib/karafka/connection/api_adapter.rb +0 -158
- data/lib/karafka/connection/batch_delegator.rb +0 -55
- data/lib/karafka/connection/builder.rb +0 -23
- data/lib/karafka/connection/message_delegator.rb +0 -36
- data/lib/karafka/consumers/batch_metadata.rb +0 -10
- data/lib/karafka/consumers/callbacks.rb +0 -71
- data/lib/karafka/consumers/includer.rb +0 -64
- data/lib/karafka/consumers/responders.rb +0 -24
- data/lib/karafka/consumers/single_params.rb +0 -15
- data/lib/karafka/contracts/responder_usage.rb +0 -54
- data/lib/karafka/fetcher.rb +0 -42
- data/lib/karafka/helpers/class_matcher.rb +0 -88
- data/lib/karafka/helpers/config_retriever.rb +0 -46
- data/lib/karafka/helpers/inflector.rb +0 -26
- data/lib/karafka/params/builders/batch_metadata.rb +0 -30
- data/lib/karafka/params/builders/params.rb +0 -38
- data/lib/karafka/params/builders/params_batch.rb +0 -25
- data/lib/karafka/params/params_batch.rb +0 -60
- data/lib/karafka/patches/ruby_kafka.rb +0 -47
- data/lib/karafka/persistence/client.rb +0 -29
- data/lib/karafka/persistence/consumers.rb +0 -45
- data/lib/karafka/persistence/topics.rb +0 -48
- data/lib/karafka/responders/builder.rb +0 -36
- data/lib/karafka/responders/topic.rb +0 -55
- data/lib/karafka/routing/topic_mapper.rb +0 -53
- data/lib/karafka/serialization/json/serializer.rb +0 -31
- data/lib/karafka/setup/configurators/water_drop.rb +0 -36
- data/lib/karafka/templates/application_responder.rb.erb +0 -11
data/karafka.gemspec
CHANGED
@@ -5,28 +5,24 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
5
5
|
|
6
6
|
require 'karafka/version'
|
7
7
|
|
8
|
-
# rubocop:disable Metrics/BlockLength
|
9
8
|
Gem::Specification.new do |spec|
|
10
9
|
spec.name = 'karafka'
|
11
10
|
spec.version = ::Karafka::VERSION
|
12
11
|
spec.platform = Gem::Platform::RUBY
|
13
|
-
spec.authors = ['Maciej Mensfeld'
|
14
|
-
spec.email = %w[maciej@mensfeld.pl
|
15
|
-
spec.homepage = 'https://
|
12
|
+
spec.authors = ['Maciej Mensfeld']
|
13
|
+
spec.email = %w[maciej@mensfeld.pl]
|
14
|
+
spec.homepage = 'https://karafka.io'
|
16
15
|
spec.summary = 'Ruby based framework for working with Apache Kafka'
|
17
16
|
spec.description = 'Framework used to simplify Apache Kafka based Ruby applications development'
|
18
|
-
spec.license = '
|
17
|
+
spec.license = 'LGPL-3.0'
|
19
18
|
|
20
19
|
spec.add_dependency 'dry-configurable', '~> 0.13'
|
21
|
-
spec.add_dependency 'dry-inflector', '~> 0.2'
|
22
20
|
spec.add_dependency 'dry-monitor', '~> 0.5'
|
23
21
|
spec.add_dependency 'dry-validation', '~> 1.7'
|
24
|
-
spec.add_dependency '
|
25
|
-
spec.add_dependency '
|
26
|
-
spec.add_dependency '
|
27
|
-
spec.add_dependency '
|
28
|
-
spec.add_dependency 'waterdrop', '~> 1.4'
|
29
|
-
spec.add_dependency 'zeitwerk', '~> 2.4'
|
22
|
+
spec.add_dependency 'rdkafka', '>= 0.10'
|
23
|
+
spec.add_dependency 'thor', '>= 0.20'
|
24
|
+
spec.add_dependency 'waterdrop', '>= 2.2.0', '< 3.0.0'
|
25
|
+
spec.add_dependency 'zeitwerk', '~> 2.3'
|
30
26
|
|
31
27
|
spec.required_ruby_version = '>= 2.6.0'
|
32
28
|
|
@@ -36,7 +32,11 @@ Gem::Specification.new do |spec|
|
|
36
32
|
|
37
33
|
spec.cert_chain = %w[certs/mensfeld.pem]
|
38
34
|
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(spec)/}) }
|
39
|
-
spec.executables =
|
35
|
+
spec.executables = %w[karafka]
|
40
36
|
spec.require_paths = %w[lib]
|
37
|
+
|
38
|
+
spec.metadata = {
|
39
|
+
'source_code_uri' => 'https://github.com/karafka/karafka',
|
40
|
+
'rubygems_mfa_required' => 'true'
|
41
|
+
}
|
41
42
|
end
|
42
|
-
# rubocop:enable Metrics/BlockLength
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'active_job'
|
4
|
+
require 'active_job/queue_adapters'
|
5
|
+
require 'active_job/queue_adapters/karafka_adapter'
|
6
|
+
|
7
|
+
module ActiveJob
|
8
|
+
# Namespace for usage simplification outside of Rails where Railtie will not kick in.
|
9
|
+
# That way a require 'active_job/karafka' should be enough to use it
|
10
|
+
module Karafka
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
# We extend routing builder by adding a simple wrapper for easier jobs topics defining
|
15
|
+
# This needs to be extended here as it is going to be used in karafka routes, hence doing that in
|
16
|
+
# the railtie initializer would be too late
|
17
|
+
::Karafka::Routing::Builder.include ::Karafka::ActiveJob::RoutingExtensions
|
18
|
+
::Karafka::Routing::Proxy.include ::Karafka::ActiveJob::RoutingExtensions
|
19
|
+
|
20
|
+
# We extend ActiveJob stuff in the railtie
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# ActiveJob components to allow for jobs consumption with Karafka
|
4
|
+
module ActiveJob
|
5
|
+
# ActiveJob queue adapters
|
6
|
+
module QueueAdapters
|
7
|
+
# Karafka adapter for enqueuing jobs
|
8
|
+
# This is here for ease of integration with ActiveJob.
|
9
|
+
class KarafkaAdapter
|
10
|
+
# Enqueues the job using the configured dispatcher
|
11
|
+
#
|
12
|
+
# @param job [Object] job that should be enqueued
|
13
|
+
def enqueue(job)
|
14
|
+
::Karafka::App.config.internal.active_job.dispatcher.call(job)
|
15
|
+
end
|
16
|
+
|
17
|
+
# Raises info, that Karafka backend does not support scheduling jobs
|
18
|
+
#
|
19
|
+
# @param _job [Object] job we cannot enqueue
|
20
|
+
# @param _timestamp [Time] time when job should run
|
21
|
+
def enqueue_at(_job, _timestamp)
|
22
|
+
raise NotImplementedError, 'This queueing backend does not support scheduling jobs.'
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Karafka
|
4
|
+
module ActiveJob
|
5
|
+
# This is the consumer for ActiveJob that eats the messages enqueued with it one after another.
|
6
|
+
# It marks the offset after each message, so we make sure, none of the jobs is executed twice
|
7
|
+
class Consumer < ::Karafka::BaseConsumer
|
8
|
+
# Executes the ActiveJob logic
|
9
|
+
# @note ActiveJob does not support batches, so we just run one message after another
|
10
|
+
def consume
|
11
|
+
messages.each do |message|
|
12
|
+
::ActiveJob::Base.execute(
|
13
|
+
# We technically speaking could set this as deserializer and reference it from the
|
14
|
+
# message instead of using the `#raw_payload`. This is not done on purpose to simplify
|
15
|
+
# the ActiveJob setup here
|
16
|
+
::ActiveSupport::JSON.decode(message.raw_payload)
|
17
|
+
)
|
18
|
+
|
19
|
+
mark_as_consumed(message)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Karafka
|
4
|
+
module ActiveJob
|
5
|
+
# Dispatcher that sends the ActiveJob job to a proper topic based on the queue name
|
6
|
+
class Dispatcher
|
7
|
+
# Defaults for dispatching
|
8
|
+
# The can be updated by using `#karafka_options` on the job
|
9
|
+
DEFAULTS = {
|
10
|
+
dispatch_method: :produce_async
|
11
|
+
}.freeze
|
12
|
+
|
13
|
+
private_constant :DEFAULTS
|
14
|
+
|
15
|
+
# @param job [ActiveJob::Base] job
|
16
|
+
def call(job)
|
17
|
+
::Karafka.producer.public_send(
|
18
|
+
fetch_option(job, :dispatch_method, DEFAULTS),
|
19
|
+
topic: job.queue_name,
|
20
|
+
payload: ::ActiveSupport::JSON.encode(job.serialize)
|
21
|
+
)
|
22
|
+
end
|
23
|
+
|
24
|
+
private
|
25
|
+
|
26
|
+
# @param job [ActiveJob::Base] job
|
27
|
+
# @param key [Symbol] key we want to fetch
|
28
|
+
# @param defaults [Hash]
|
29
|
+
# @return [Object] options we are interested in
|
30
|
+
def fetch_option(job, key, defaults)
|
31
|
+
job
|
32
|
+
.class
|
33
|
+
.karafka_options
|
34
|
+
.fetch(key, defaults.fetch(key))
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Karafka
|
4
|
+
module ActiveJob
|
5
|
+
# Allows for setting karafka specific options in ActiveJob jobs
|
6
|
+
module JobExtensions
|
7
|
+
class << self
|
8
|
+
# Defines all the needed accessors and sets defaults
|
9
|
+
# @param klass [ActiveJob::Base] active job base
|
10
|
+
def extended(klass)
|
11
|
+
klass.class_attribute :_karafka_options
|
12
|
+
klass._karafka_options = {}
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
# @param new_options [Hash] additional options that allow for jobs Karafka related options
|
17
|
+
# customization
|
18
|
+
# @return [Hash] karafka options
|
19
|
+
def karafka_options(new_options = {})
|
20
|
+
return _karafka_options if new_options.empty?
|
21
|
+
|
22
|
+
# Make sure, that karafka options that someone wants to use are valid before assigning
|
23
|
+
# them
|
24
|
+
App.config.internal.active_job.job_options_contract.validate!(new_options)
|
25
|
+
|
26
|
+
new_options.each do |name, value|
|
27
|
+
_karafka_options[name] = value
|
28
|
+
end
|
29
|
+
|
30
|
+
_karafka_options
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Karafka
|
4
|
+
module ActiveJob
|
5
|
+
# Contract for validating the options that can be altered with `#karafka_options` per job class
|
6
|
+
# @note We keep this in the `Karafka::ActiveJob` namespace instead of `Karafka::Contracts` as
|
7
|
+
# we want to keep ActiveJob related Karafka components outside of the core Karafka code and
|
8
|
+
# all in the same place
|
9
|
+
class JobOptionsContract < Contracts::Base
|
10
|
+
params do
|
11
|
+
optional(:dispatch_method).value(included_in?: %i[produce_async produce_sync])
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Karafka
|
4
|
+
# ActiveJob related Karafka stuff
|
5
|
+
module ActiveJob
|
6
|
+
# Routing extensions for ActiveJob
|
7
|
+
module RoutingExtensions
|
8
|
+
# This method simplifies routes definition for ActiveJob topics / queues by auto-injecting
|
9
|
+
# the consumer class
|
10
|
+
# @param name [String, Symbol] name of the topic where ActiveJobs jobs should go
|
11
|
+
def active_job_topic(name)
|
12
|
+
topic(name) do
|
13
|
+
consumer App.config.internal.active_job.consumer
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
data/lib/karafka/app.rb
CHANGED
@@ -6,31 +6,23 @@ module Karafka
|
|
6
6
|
extend Setup::Dsl
|
7
7
|
|
8
8
|
class << self
|
9
|
-
#
|
10
|
-
# We need to know details about consumers in order to setup components,
|
11
|
-
# that's why we don't setup them after std setup is done
|
12
|
-
# @raise [Karafka::Errors::InvalidConfigurationError] raised when configuration
|
13
|
-
# doesn't match with the config contract
|
14
|
-
def boot!
|
15
|
-
initialize!
|
16
|
-
Setup::Config.validate!
|
17
|
-
Setup::Config.setup_components
|
18
|
-
initialized!
|
19
|
-
end
|
20
|
-
|
21
|
-
# @return [Karafka::Routing::Builder] consumers builder instance
|
9
|
+
# @return [Karafka::Routing::Builder] consumers builder instance alias
|
22
10
|
def consumer_groups
|
23
|
-
config
|
11
|
+
config
|
12
|
+
.internal
|
13
|
+
.routing_builder
|
24
14
|
end
|
25
15
|
|
26
|
-
#
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
config.internal.routing_builder.reload
|
16
|
+
# @return [Array<Karafka::Routing::SubscriptionGroup>] active subscription groups
|
17
|
+
def subscription_groups
|
18
|
+
consumer_groups
|
19
|
+
.active
|
20
|
+
.flat_map(&:subscription_groups)
|
32
21
|
end
|
33
22
|
|
23
|
+
# Just a nicer name for the consumer groups
|
24
|
+
alias routes consumer_groups
|
25
|
+
|
34
26
|
Status.instance_methods(false).each do |delegated|
|
35
27
|
define_method(delegated) do
|
36
28
|
App.config.internal.status.send(delegated)
|
@@ -42,7 +34,9 @@ module Karafka
|
|
42
34
|
root
|
43
35
|
env
|
44
36
|
logger
|
37
|
+
producer
|
45
38
|
monitor
|
39
|
+
pro?
|
46
40
|
].each do |delegated|
|
47
41
|
define_method(delegated) do
|
48
42
|
Karafka.send(delegated)
|
@@ -4,48 +4,80 @@
|
|
4
4
|
module Karafka
|
5
5
|
# Base consumer from which all Karafka consumers should inherit
|
6
6
|
class BaseConsumer
|
7
|
-
extend Forwardable
|
8
|
-
|
9
|
-
# Allows us to mark messages as consumed for non-automatic mode without having
|
10
|
-
# to use consumer client directly. We do this that way, because most of the people should not
|
11
|
-
# mess with the client instance directly (just in case)
|
12
|
-
%i[
|
13
|
-
mark_as_consumed
|
14
|
-
mark_as_consumed!
|
15
|
-
trigger_heartbeat
|
16
|
-
trigger_heartbeat!
|
17
|
-
].each do |delegated_method_name|
|
18
|
-
def_delegator :client, delegated_method_name
|
19
|
-
|
20
|
-
private delegated_method_name
|
21
|
-
end
|
22
|
-
|
23
7
|
# @return [Karafka::Routing::Topic] topic to which a given consumer is subscribed
|
24
|
-
|
25
|
-
# @return [Karafka::
|
26
|
-
attr_accessor :
|
27
|
-
|
28
|
-
|
29
|
-
#
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
Consumers::Includer.call(self)
|
34
|
-
end
|
8
|
+
attr_accessor :topic
|
9
|
+
# @return [Karafka::Messages::Messages] current messages batch
|
10
|
+
attr_accessor :messages
|
11
|
+
# @return [Karafka::Connection::Client] kafka connection client
|
12
|
+
attr_accessor :client
|
13
|
+
# @return [Karafka::TimeTrackers::Pause] current topic partition pause
|
14
|
+
attr_accessor :pause
|
15
|
+
# @return [Waterdrop::Producer] producer instance
|
16
|
+
attr_accessor :producer
|
35
17
|
|
36
18
|
# Executes the default consumer flow.
|
37
|
-
|
38
|
-
|
19
|
+
#
|
20
|
+
# @note We keep the seek offset tracking, and use it to compensate for async offset flushing
|
21
|
+
# that may not yet kick in when error occurs. That way we pause always on the last processed
|
22
|
+
# message.
|
23
|
+
def on_consume
|
24
|
+
Karafka.monitor.instrument('consumer.consume', caller: self) do
|
25
|
+
consume
|
26
|
+
end
|
27
|
+
|
28
|
+
pause.reset
|
29
|
+
|
30
|
+
# Mark as consumed only if manual offset management is not on
|
31
|
+
return if topic.manual_offset_management
|
32
|
+
|
33
|
+
# We use the non-blocking one here. If someone needs the blocking one, can implement it with
|
34
|
+
# manual offset management
|
35
|
+
mark_as_consumed(messages.last)
|
36
|
+
rescue StandardError => e
|
37
|
+
Karafka.monitor.instrument(
|
38
|
+
'error.occurred',
|
39
|
+
error: e,
|
40
|
+
caller: self,
|
41
|
+
type: 'consumer.consume.error'
|
42
|
+
)
|
43
|
+
client.pause(topic.name, messages.first.partition, @seek_offset || messages.first.offset)
|
44
|
+
pause.pause
|
39
45
|
end
|
40
46
|
|
41
|
-
|
47
|
+
# Trigger method for running on shutdown.
|
48
|
+
#
|
49
|
+
# @private
|
50
|
+
def on_revoked
|
51
|
+
Karafka.monitor.instrument('consumer.revoked', caller: self) do
|
52
|
+
revoked
|
53
|
+
end
|
54
|
+
rescue StandardError => e
|
55
|
+
Karafka.monitor.instrument(
|
56
|
+
'error.occurred',
|
57
|
+
error: e,
|
58
|
+
caller: self,
|
59
|
+
type: 'consumer.revoked.error'
|
60
|
+
)
|
61
|
+
end
|
42
62
|
|
43
|
-
#
|
44
|
-
#
|
45
|
-
|
46
|
-
|
63
|
+
# Trigger method for running on shutdown.
|
64
|
+
#
|
65
|
+
# @private
|
66
|
+
def on_shutdown
|
67
|
+
Karafka.monitor.instrument('consumer.shutdown', caller: self) do
|
68
|
+
shutdown
|
69
|
+
end
|
70
|
+
rescue StandardError => e
|
71
|
+
Karafka.monitor.instrument(
|
72
|
+
'error.occurred',
|
73
|
+
error: e,
|
74
|
+
caller: self,
|
75
|
+
type: 'consumer.shutdown.error'
|
76
|
+
)
|
47
77
|
end
|
48
78
|
|
79
|
+
private
|
80
|
+
|
49
81
|
# Method that will perform business logic and on data received from Kafka (it will consume
|
50
82
|
# the data)
|
51
83
|
# @note This method needs bo be implemented in a subclass. We stub it here as a failover if
|
@@ -53,5 +85,42 @@ module Karafka
|
|
53
85
|
def consume
|
54
86
|
raise NotImplementedError, 'Implement this in a subclass'
|
55
87
|
end
|
88
|
+
|
89
|
+
# Method that will be executed when a given topic partition is revoked. You can use it for
|
90
|
+
# some teardown procedures (closing file handler, etc).
|
91
|
+
def revoked; end
|
92
|
+
|
93
|
+
# Method that will be executed when the process is shutting down. You can use it for
|
94
|
+
# some teardown procedures (closing file handler, etc).
|
95
|
+
def shutdown; end
|
96
|
+
|
97
|
+
# Marks message as consumed in an async way.
|
98
|
+
#
|
99
|
+
# @param message [Messages::Message] last successfully processed message.
|
100
|
+
def mark_as_consumed(message)
|
101
|
+
client.mark_as_consumed(message)
|
102
|
+
@seek_offset = message.offset + 1
|
103
|
+
end
|
104
|
+
|
105
|
+
# Marks message as consumed in a sync way.
|
106
|
+
#
|
107
|
+
# @param message [Messages::Message] last successfully processed message.
|
108
|
+
def mark_as_consumed!(message)
|
109
|
+
client.mark_as_consumed!(message)
|
110
|
+
@seek_offset = message.offset + 1
|
111
|
+
end
|
112
|
+
|
113
|
+
# Seeks in the context of current topic and partition
|
114
|
+
#
|
115
|
+
# @param offset [Integer] offset where we want to seek
|
116
|
+
def seek(offset)
|
117
|
+
client.seek(
|
118
|
+
Karafka::Messages::Seek.new(
|
119
|
+
messages.metadata.topic,
|
120
|
+
messages.metadata.partition,
|
121
|
+
offset
|
122
|
+
)
|
123
|
+
)
|
124
|
+
end
|
56
125
|
end
|
57
126
|
end
|
data/lib/karafka/cli/base.rb
CHANGED
@@ -43,16 +43,16 @@ module Karafka
|
|
43
43
|
end
|
44
44
|
|
45
45
|
# Allows to set description of a given cli command
|
46
|
-
# @param
|
47
|
-
def desc(
|
48
|
-
@desc ||=
|
46
|
+
# @param desc [String] Description of a given cli command
|
47
|
+
def desc(desc)
|
48
|
+
@desc ||= desc
|
49
49
|
end
|
50
50
|
|
51
51
|
# This method will bind a given Cli command into Karafka Cli
|
52
52
|
# This method is a wrapper to way Thor defines its commands
|
53
53
|
# @param cli_class [Karafka::Cli] Karafka cli_class
|
54
54
|
def bind_to(cli_class)
|
55
|
-
cli_class.desc name,
|
55
|
+
cli_class.desc name, @desc
|
56
56
|
|
57
57
|
(@options || []).each { |option| cli_class.option(*option) }
|
58
58
|
|
data/lib/karafka/cli/info.rb
CHANGED
@@ -7,24 +7,59 @@ module Karafka
|
|
7
7
|
class Info < Base
|
8
8
|
desc 'Print configuration details and other options of your application'
|
9
9
|
|
10
|
+
# Nice karafka banner
|
11
|
+
BANNER = <<~BANNER
|
12
|
+
|
13
|
+
@@@ @@@@@ @@@
|
14
|
+
@@@ @@@ @@@
|
15
|
+
@@@ @@@ @@@@@@@@@ @@@ @@@ @@@@@@@@@ @@@@@@@@ @@@ @@@@ @@@@@@@@@
|
16
|
+
@@@@@@ @@@ @@@ @@@@@ @@@ @@@ @@@ @@@@@@@ @@@ @@@
|
17
|
+
@@@@@@@ @@@ @@@ @@@ @@@@ @@@ @@@ @@@@@@@ @@@ @@@
|
18
|
+
@@@ @@@@ @@@@@@@@@@ @@@ @@@@@@@@@@ @@@ @@@ @@@@ @@@@@@@@@@
|
19
|
+
|
20
|
+
BANNER
|
21
|
+
|
10
22
|
# Print configuration details and other options of your application
|
11
23
|
def call
|
24
|
+
Karafka.logger.info(BANNER)
|
25
|
+
Karafka.logger.info((core_info + license_info).join("\n"))
|
26
|
+
end
|
27
|
+
|
28
|
+
private
|
29
|
+
|
30
|
+
# @return [Array<String>] core framework related info
|
31
|
+
def core_info
|
12
32
|
config = Karafka::App.config
|
13
33
|
|
14
|
-
|
15
|
-
|
34
|
+
postfix = Karafka.pro? ? ' + Pro' : ''
|
35
|
+
|
36
|
+
[
|
37
|
+
"Karafka version: #{Karafka::VERSION}#{postfix}",
|
16
38
|
"Ruby version: #{RUBY_VERSION}",
|
17
|
-
"
|
39
|
+
"Rdkafka version: #{::Rdkafka::VERSION}",
|
40
|
+
"Subscription groups count: #{Karafka::App.subscription_groups.size}",
|
41
|
+
"Workers count: #{Karafka::App.config.concurrency}",
|
18
42
|
"Application client id: #{config.client_id}",
|
19
|
-
"Backend: #{config.backend}",
|
20
|
-
"Batch fetching: #{config.batch_fetching}",
|
21
|
-
"Batch consuming: #{config.batch_consuming}",
|
22
43
|
"Boot file: #{Karafka.boot_file}",
|
23
|
-
"Environment: #{Karafka.env}"
|
24
|
-
"Kafka seed brokers: #{config.kafka.seed_brokers}"
|
44
|
+
"Environment: #{Karafka.env}"
|
25
45
|
]
|
46
|
+
end
|
47
|
+
|
48
|
+
# @return [Array<String>] license related info
|
49
|
+
def license_info
|
50
|
+
config = Karafka::App.config
|
26
51
|
|
27
|
-
Karafka.
|
52
|
+
if Karafka.pro?
|
53
|
+
[
|
54
|
+
'License: Commercial',
|
55
|
+
"License entity: #{config.license.entity}",
|
56
|
+
"License expires on: #{config.license.expires_on}"
|
57
|
+
]
|
58
|
+
else
|
59
|
+
[
|
60
|
+
'License: LGPL-3.0'
|
61
|
+
]
|
62
|
+
end
|
28
63
|
end
|
29
64
|
end
|
30
65
|
end
|
data/lib/karafka/cli/install.rb
CHANGED
@@ -12,19 +12,15 @@ module Karafka
|
|
12
12
|
# Directories created by default
|
13
13
|
INSTALL_DIRS = %w[
|
14
14
|
app/consumers
|
15
|
-
app/responders
|
16
|
-
app/workers
|
17
15
|
config
|
18
|
-
lib
|
19
16
|
log
|
20
|
-
tmp/pids
|
21
17
|
].freeze
|
22
18
|
|
23
19
|
# Where should we map proper files from templates
|
24
20
|
INSTALL_FILES_MAP = {
|
25
21
|
'karafka.rb.erb' => Karafka.boot_file.basename,
|
26
22
|
'application_consumer.rb.erb' => 'app/consumers/application_consumer.rb',
|
27
|
-
'
|
23
|
+
'example_consumer.rb.erb' => 'app/consumers/example_consumer.rb'
|
28
24
|
}.freeze
|
29
25
|
|
30
26
|
# @param args [Array] all the things that Thor CLI accepts
|
@@ -35,6 +31,7 @@ module Karafka
|
|
35
31
|
Bundler.default_lockfile
|
36
32
|
)
|
37
33
|
).dependencies
|
34
|
+
|
38
35
|
@rails = dependencies.key?('railties') || dependencies.key?('rails')
|
39
36
|
end
|
40
37
|
|
@@ -48,9 +45,7 @@ module Karafka
|
|
48
45
|
target = Karafka.root.join(target)
|
49
46
|
|
50
47
|
template = File.read(Karafka.core_root.join("templates/#{source}"))
|
51
|
-
|
52
|
-
# Ruby < 2.6
|
53
|
-
render = ::ERB.new(template, nil, '-').result(binding)
|
48
|
+
render = ::ERB.new(template, trim_mode: '-').result(binding)
|
54
49
|
|
55
50
|
File.open(target, 'w') { |file| file.write(render) }
|
56
51
|
end
|
data/lib/karafka/cli/server.rb
CHANGED
@@ -5,27 +5,16 @@ module Karafka
|
|
5
5
|
class Cli < Thor
|
6
6
|
# Server Karafka Cli action
|
7
7
|
class Server < Base
|
8
|
-
# Server config settings contract
|
9
|
-
CONTRACT = Contracts::ServerCliOptions.new.freeze
|
10
|
-
|
11
|
-
private_constant :CONTRACT
|
12
|
-
|
13
8
|
desc 'Start the Karafka server (short-cut alias: "s")'
|
14
9
|
option aliases: 's'
|
15
|
-
option :daemon, default: false, type: :boolean, aliases: :d
|
16
|
-
option :pid, default: 'tmp/pids/karafka', type: :string, aliases: :p
|
17
10
|
option :consumer_groups, type: :array, default: nil, aliases: :g
|
18
11
|
|
19
12
|
# Start the Karafka server
|
20
13
|
def call
|
21
|
-
|
22
|
-
|
23
|
-
validate!
|
14
|
+
# Print our banner and info in the dev mode
|
15
|
+
print_marketing_info if Karafka::App.env.development?
|
24
16
|
|
25
|
-
|
26
|
-
FileUtils.mkdir_p File.dirname(cli.options[:pid])
|
27
|
-
daemonize
|
28
|
-
end
|
17
|
+
Contracts::ServerCliOptions.new.validate!(cli.options)
|
29
18
|
|
30
19
|
# We assign active topics on a server level, as only server is expected to listen on
|
31
20
|
# part of the topics
|
@@ -36,35 +25,19 @@ module Karafka
|
|
36
25
|
|
37
26
|
private
|
38
27
|
|
39
|
-
#
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
cli.options[:pid],
|
53
|
-
'w'
|
54
|
-
) { |file| file.write(::Process.pid) }
|
55
|
-
|
56
|
-
# Remove pidfile on stop, just before the server instance is going to be GCed
|
57
|
-
# We want to delay the moment in which the pidfile is removed as much as we can,
|
58
|
-
# so instead of removing it after the server stops running, we rely on the gc moment
|
59
|
-
# when this object gets removed (it is a bit later), so it is closer to the actual
|
60
|
-
# system process end. We do that, so monitoring and deployment tools that rely on a pid
|
61
|
-
# won't alarm or start new system process up until the current one is finished
|
62
|
-
ObjectSpace.define_finalizer(self, proc { send(:clean) })
|
63
|
-
end
|
64
|
-
|
65
|
-
# Removes a pidfile (if exist)
|
66
|
-
def clean
|
67
|
-
FileUtils.rm_f(cli.options[:pid]) if cli.options[:pid]
|
28
|
+
# Prints marketing info
|
29
|
+
def print_marketing_info
|
30
|
+
Karafka.logger.info Info::BANNER
|
31
|
+
|
32
|
+
if Karafka.pro?
|
33
|
+
Karafka.logger.info(
|
34
|
+
"\033[0;32mThank you for investing in the Karafka Pro subscription!\033[0m\n"
|
35
|
+
)
|
36
|
+
else
|
37
|
+
Karafka.logger.info(
|
38
|
+
"\033[0;31mYou like Karafka? Please consider getting a Pro subscription!\033[0m\n"
|
39
|
+
)
|
40
|
+
end
|
68
41
|
end
|
69
42
|
end
|
70
43
|
end
|