Class: Karafka::Web::Pro::Commanding::Matcher

Inherits:
Object
  • Object
show all
Defined in:
lib/karafka/web/pro/commanding/matcher.rb

Overview

Matcher that makes a decision whether a given command message should be applied/executed within the context of the current consumer process.

Since we use the ‘assign`, each process listens to this topic and receives messages with commands targeting all the processes, this is why it needs to be filtered.

The matcher uses a set of sub-matchers, each responsible for checking a specific criterion. Matchers have two methods:

  • ‘apply?` - returns true if the matcher’s criterion is present in the message

  • ‘matches?` - returns true if the criterion matches (only checked if apply? is true)

Matcher classes are auto-loaded from the Matchers namespace and sorted by priority. Required matchers (MessageType, SchemaVersion) have lower priority and are checked first.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.matcher_classesArray<Class>

Returns all matcher classes sorted by priority.

Returns:

  • (Array<Class>)

    all matcher classes sorted by priority



51
52
53
54
55
56
57
58
# File 'lib/karafka/web/pro/commanding/matcher.rb', line 51

def matcher_classes
  @matcher_classes ||= Matchers
    .constants
    .map { |name| Matchers.const_get(name) }
    .select { |klass| klass.is_a?(Class) && klass < Matchers::Base }
    .sort_by(&:priority)
    .freeze
end

Instance Method Details

#matches?(message) ⇒ Boolean

Returns is this message dedicated to current process and is actionable.

Parameters:

  • message (Karafka::Messages::Message)

    message with command

Returns:

  • (Boolean)

    is this message dedicated to current process and is actionable



63
64
65
66
67
68
69
70
71
# File 'lib/karafka/web/pro/commanding/matcher.rb', line 63

def matches?(message)
  self
    .class
    .matcher_classes
    .lazy
    .map { |klass| klass.new(message) }
    .select(&:apply?)
    .all?(&:matches?)
end