Class: Karafka::Routing::Route

Inherits:
Object
  • Object
show all
Defined in:
lib/karafka/routing/route.rb

Overview

Class representing a single route (from topic to worker) with all additional features and elements. Single route contains descriptions of:

  • topic - Kafka topic name (required)

  • controller - Class of a controller that will handle messages from a given topic (required)

  • group - Kafka group that we want to use (optional)

  • worker - Which worker should handle the backend task (optional)

  • parser - What parsed do we want to use to unparse the data (optional)

  • interchanger - What interchanger to encode/decode data do we want to use (optional)

Constant Summary collapse

NAME_FORMAT =

Only ASCII alphanumeric characters and underscore and dash are allowed in topics and groups

/\A(\w|\-)+\z/
ATTRIBUTES =

Options that we can set per each route

%i(
  group
  topic
  worker
  parser
  interchanger
  responder
  inline
).freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#controllerObject

This we can get “directly” because it does not have any details, etc



29
30
31
# File 'lib/karafka/routing/route.rb', line 29

def controller
  @controller
end

Instance Method Details

#buildObject

Initializes default values for all the options that support defaults if their values are not yet specified. This is need to be done (cannot be lazy loaded on first use) because everywhere except Karafka server command, those would not be initialized on time - for example for Sidekiq



35
36
37
38
# File 'lib/karafka/routing/route.rb', line 35

def build
  ATTRIBUTES.each { |attr| send(attr) }
  self
end

#groupString

Note:

If group is not provided in a route, will build one based on the app name and the route topic (that is required)

Returns Kafka group name.

Returns:

  • (String)

    Kafka group name



43
44
45
# File 'lib/karafka/routing/route.rb', line 43

def group
  (@group ||= "#{Karafka::App.config.name.underscore}_#{topic}").to_s
end

#inlineBoolean

Note:

This method can be set to false, so direct assigment ||= would not work

Returns Should we perform execution in the background (default) or inline. This can be set globally and overwritten by a per route setting.

Returns:

  • (Boolean)

    Should we perform execution in the background (default) or inline. This can be set globally and overwritten by a per route setting



80
81
82
83
# File 'lib/karafka/routing/route.rb', line 80

def inline
  return @inline unless @inline.nil?
  @inline = Karafka::App.config.inline
end

#interchangerClass

Returns Interchanger class (not an instance) that we want to use to interchange params between Karafka server and Karafka background job.

Returns:

  • (Class)

    Interchanger class (not an instance) that we want to use to interchange params between Karafka server and Karafka background job



73
74
75
# File 'lib/karafka/routing/route.rb', line 73

def interchanger
  @interchanger ||= Karafka::Params::Interchanger
end

#parserClass

Note:

If not provided - will use JSON as default

Returns Parser class (not instance) that we want to use to unparse Kafka messages.

Returns:

  • (Class)

    Parser class (not instance) that we want to use to unparse Kafka messages



67
68
69
# File 'lib/karafka/routing/route.rb', line 67

def parser
  @parser ||= JSON
end

#responderClass?

Returns Class (not an instance) of a responder that should respond from controller back to Kafka (usefull for piping dataflows).

Returns:

  • (Class, nil)

    Class (not an instance) of a responder that should respond from controller back to Kafka (usefull for piping dataflows)



61
62
63
# File 'lib/karafka/routing/route.rb', line 61

def responder
  @responder ||= Karafka::Responders::Builder.new(controller).build
end

#topicString

Returns route topic - this is the core esence of Kafka.

Returns:

  • (String)

    route topic - this is the core esence of Kafka



48
49
50
# File 'lib/karafka/routing/route.rb', line 48

def topic
  @topic.to_s
end

#validate!Object

Checks if topic and group have proper format (acceptable by Kafka)

Raises:



88
89
90
91
# File 'lib/karafka/routing/route.rb', line 88

def validate!
  raise Errors::InvalidTopicName, topic if NAME_FORMAT !~ topic
  raise Errors::InvalidGroupName, group if NAME_FORMAT !~ group
end

#workerClass

Note:

If not provided - will be built based on the provided controller

Returns Class (not an instance) of a worker that should be used to schedule the background job.

Returns:

  • (Class)

    Class (not an instance) of a worker that should be used to schedule the background job



55
56
57
# File 'lib/karafka/routing/route.rb', line 55

def worker
  @worker ||= Karafka::Workers::Builder.new(controller).build
end