summaryrefslogtreecommitdiff
path: root/lib/gitlab/event_store.rb
blob: 3d7b6b27eb0b0ee3260fb4935c45b436bcb48668 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# frozen_string_literal: true

# Gitlab::EventStore is a simple pub-sub mechanism that lets you publish
# domain events and use Sidekiq workers as event handlers.
#
# It can be used to decouple domains from different bounded contexts
# by publishing domain events and let any interested parties subscribe
# to them.
#
module Gitlab
  module EventStore
    Error = Class.new(StandardError)
    InvalidEvent = Class.new(Error)
    InvalidSubscriber = Class.new(Error)

    def self.publish(event)
      instance.publish(event)
    end

    def self.instance
      @instance ||= configure!
    end

    # Define all event subscriptions using:
    #
    #   store.subscribe(DomainA::SomeWorker, to: DomainB::SomeEvent)
    #
    # It is possible to subscribe to a subset of events matching a condition:
    #
    #   store.subscribe(DomainA::SomeWorker, to: DomainB::SomeEvent), if: ->(event) { event.data == :some_value }
    #
    def self.configure!
      Store.new do |store|
        ###
        # Add subscriptions here:

        store.subscribe ::MergeRequests::UpdateHeadPipelineWorker, to: ::Ci::PipelineCreatedEvent
      end
    end
    private_class_method :configure!
  end
end