2022-01-01 13:13:43 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
# This module should be included in order to turn an ApplicationWorker
|
|
|
|
# into a Subscriber.
|
|
|
|
# This module overrides the `perform` method and provides a better and
|
|
|
|
# safer interface for handling events via `handle_event` method.
|
|
|
|
#
|
|
|
|
# @example:
|
|
|
|
# class SomeEventSubscriber
|
|
|
|
# include Gitlab::EventStore::Subscriber
|
|
|
|
#
|
|
|
|
# def handle_event(event)
|
|
|
|
# # ...
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
|
|
|
|
module Gitlab
|
|
|
|
module EventStore
|
|
|
|
module Subscriber
|
2022-02-08 13:16:03 -05:00
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
|
|
|
included do
|
|
|
|
include ApplicationWorker
|
|
|
|
|
|
|
|
loggable_arguments 0, 1
|
2022-05-25 02:08:38 -04:00
|
|
|
idempotent!
|
2022-02-08 13:16:03 -05:00
|
|
|
end
|
|
|
|
|
2022-01-01 13:13:43 -05:00
|
|
|
def perform(event_type, data)
|
|
|
|
raise InvalidEvent, event_type unless self.class.const_defined?(event_type)
|
|
|
|
|
|
|
|
event = event_type.constantize.new(
|
|
|
|
data: data.with_indifferent_access
|
|
|
|
)
|
|
|
|
|
|
|
|
handle_event(event)
|
|
|
|
end
|
|
|
|
|
|
|
|
def handle_event(event)
|
|
|
|
raise NotImplementedError, 'you must implement this methods in order to handle events'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|