2022-01-01 13:13:43 -05:00
|
|
|
# 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
|
2022-02-03 07:18:57 -05:00
|
|
|
@instance ||= Store.new { |store| configure!(store) }
|
2022-01-01 13:13:43 -05:00
|
|
|
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 }
|
|
|
|
#
|
2022-02-03 07:18:57 -05:00
|
|
|
def self.configure!(store)
|
|
|
|
###
|
|
|
|
# Add subscriptions here:
|
2022-01-01 13:13:43 -05:00
|
|
|
|
2022-02-03 07:18:57 -05:00
|
|
|
store.subscribe ::MergeRequests::UpdateHeadPipelineWorker, to: ::Ci::PipelineCreatedEvent
|
2022-02-10 07:18:48 -05:00
|
|
|
store.subscribe ::Namespaces::UpdateRootStatisticsWorker, to: ::Projects::ProjectDeletedEvent
|
2022-01-01 13:13:43 -05:00
|
|
|
end
|
|
|
|
private_class_method :configure!
|
|
|
|
end
|
|
|
|
end
|
2022-02-03 07:18:57 -05:00
|
|
|
|
|
|
|
Gitlab::EventStore.prepend_mod_with('Gitlab::EventStore')
|