Module: Eventsimple::Outbox::Consumer

Defined in:
lib/eventsimple/outbox/consumer.rb

Defined Under Namespace

Classes: ExitError

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(klass) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/eventsimple/outbox/consumer.rb', line 11

def self.extended(klass)
  klass.class_exec do
    class_attribute :_event_klass
    class_attribute :_identifier
    class_attribute :_processor_klass
    class_attribute :_processor_pool
    class_attribute :_concurrency, default: 5
    class_attribute :_batch_size, default: 1000
    class_attribute :stop_consumer, default: false
  end
end

Instance Method Details

#consumes_event(event_klass) ⇒ Object



27
28
29
30
31
# File 'lib/eventsimple/outbox/consumer.rb', line 27

def consumes_event(event_klass)
  event_klass._outbox_enabled = true

  self._event_klass = event_klass
end

#identifier(name) ⇒ Object



23
24
25
# File 'lib/eventsimple/outbox/consumer.rb', line 23

def identifier(name)
  self._identifier = name
end

#processor(processor_klass, concurrency: 5) ⇒ Object



33
34
35
36
37
# File 'lib/eventsimple/outbox/consumer.rb', line 33

def processor(processor_klass, concurrency: 5)
  self._concurrency = concurrency
  self._processor_klass = processor_klass
  self._processor_pool = _concurrency.times.map { processor_klass.new }
end

#run_consumerObject



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/eventsimple/outbox/consumer.rb', line 52

def run_consumer
  raise 'Eventsimple: No event class defined' unless _event_klass
  raise 'Eventsimple: No processor defined' unless _processor_klass
  raise 'Eventsimple: No identifier defined' unless _identifier
  raise 'Eventsimple: No concurrency defined' unless _concurrency.is_a?(Integer)

  $stdout.puts("Starting consumer for #{_identifier}")

  cursor = Outbox::Cursor.fetch(_identifier)

  until stop_consumer
    _event_klass.unscoped.in_batches(start: cursor + 1, load: true, of: _batch_size).each do |batch|
      grouped_events = batch.group_by { |event| event.aggregate_id.unpack1('L') % _concurrency }

      promises = grouped_events.map { |index, events|
        Concurrent::Promises.future {
          events.each do |event|
            _processor_pool[index].call(event)
            raise ExitError if stop_consumer
          end
        }
      }

      Concurrent::Promises.zip(*promises).value!

      cursor = batch.last.id
      Outbox::Cursor.set(_identifier, cursor)
    end

    sleep(1)
  end
rescue ExitError
  $stdout.puts("Stopping consumer for #{_identifier}")
end

#startObject

rubocop:disable Metrics/AbcSize



39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/eventsimple/outbox/consumer.rb', line 39

def start # rubocop:disable Metrics/AbcSize
  Signal.trap('INT') do
    self.stop_consumer = true
    $stdout.puts('INT received, stopping consumer')
  end
  Signal.trap('TERM') do
    self.stop_consumer = true
    $stdout.puts('TERM received, stopping consumer')
  end

  run_consumer
end