Extract Geo::EventStore

parent 554b53f1
module Geo
# Base class for event store classes.
#
# Each store should also specify its event type by calling
# `self.event_type = ...` in the body of the class. The value of
# this method should be a symbol such as `:repository_updated_event`
# or `:repository_deleted_event`. For example:
#
# class RepositoryUpdatedEventStore < EventStore
# self.event_type = :repository_updated_event
# end
#
# The event type is used to determine which attribute we should set
# on an instance of the Geo::EventLog class.
#
# Event store classes should implement the instance method `build_event`.
# The `build_event` method is supposed to return an instance of the event
# that will be logged.
class EventStore
class << self
attr_accessor :event_type
end
attr_reader :project, :params
def initialize(project, params = {})
@project = project
@params = params
end
def create
return unless Gitlab::Geo.primary?
Geo::EventLog.transaction do
event_log = Geo::EventLog.new
event_log.public_send("#{self.class.event_type}=", build_event)
event_log.save!
end
rescue ActiveRecord::RecordInvalid, NoMethodError
log("#{self.event_type.to_s.humanize} could not be created")
end
private
def build_event
raise NotImplementedError,
"#{self.class} does not implement #{__method__}"
end
def log(message)
Rails.logger.error("#{self.class.name}: #{message} for project #{project.path_with_namespace} (#{project.id})")
end
end
end
module Geo
class RepositoryDeletedEventStore
attr_reader :project, :repo_path, :wiki_path
class RepositoryDeletedEventStore < EventStore
self.event_type = :repository_deleted_event
def initialize(project, repo_path:, wiki_path:)
@project = project
@repo_path = repo_path
@wiki_path = wiki_path
end
def create
return unless Gitlab::Geo.primary?
private
Geo::EventLog.transaction do
event_log = Geo::EventLog.new
deleted_event = Geo::RepositoryDeletedEvent.new(
project: project,
repository_storage_name: project.repository.storage,
repository_storage_path: project.repository_storage_path,
deleted_path: repo_path,
deleted_wiki_path: wiki_path,
deleted_project_name: project.name)
event_log.repository_deleted_event = deleted_event
event_log.save
end
def build_event
Geo::RepositoryDeletedEvent.new(
project: project,
repository_storage_name: project.repository.storage,
repository_storage_path: project.repository_storage_path,
deleted_path: params.fetch(:repo_path),
deleted_wiki_path: params.fetch(:wiki_path),
deleted_project_name: project.name)
end
end
end
module Geo
class RepositoryRenamedEventStore
attr_reader :project, :old_path, :old_path_with_namespace
def initialize(project, old_path:, old_path_with_namespace:)
@project = project
@old_path = old_path
@old_path_with_namespace = old_path_with_namespace
end
def create
return unless Gitlab::Geo.primary?
Geo::EventLog.transaction do
event_log = Geo::EventLog.new
event_log.repository_renamed_event = build_event
event_log.save!
end
rescue ActiveRecord::RecordInvalid
log("Renamed event could not be created")
end
class RepositoryRenamedEventStore < EventStore
self.event_type = :repository_renamed_event
private
......@@ -31,11 +13,15 @@ module Geo
new_path_with_namespace: project.full_path,
old_wiki_path_with_namespace: old_wiki_path_with_namespace,
new_wiki_path_with_namespace: new_wiki_path_with_namespace,
old_path: old_path,
old_path: params.fetch(:old_path),
new_path: project.path
)
end
def old_path_with_namespace
params.fetch(:old_path_with_namespace)
end
def old_wiki_path_with_namespace
"#{old_path_with_namespace}.wiki"
end
......@@ -43,9 +29,5 @@ module Geo
def new_wiki_path_with_namespace
project.wiki.path_with_namespace
end
def log(message)
Rails.logger.info("#{self.class.name}: #{message} for project #{project.path_with_namespace} (#{project.id})")
end
end
end
module Geo
class RepositoryUpdatedEventStore
attr_reader :project, :source, :refs, :changes
def initialize(project, refs: [], changes: [], source: Geo::RepositoryUpdatedEvent::REPOSITORY)
@project = project
@refs = refs
@changes = changes
@source = source
end
def create
return unless Gitlab::Geo.primary?
Geo::EventLog.transaction do
event_log = Geo::EventLog.new
event_log.repository_updated_event = build_event
event_log.save!
end
rescue ActiveRecord::RecordInvalid
log("#{Geo::PushEvent.sources.key(source).humanize} updated event could not be created")
end
class RepositoryUpdatedEventStore < EventStore
self.event_type = :repository_updated_event
private
......@@ -35,6 +16,18 @@ module Geo
)
end
def refs
params.fetch(:refs, [])
end
def changes
params.fetch(:changes, [])
end
def source
params.fetch(:source, Geo::RepositoryUpdatedEvent::REPOSITORY)
end
def ref
refs.first if refs.length == 1
end
......@@ -54,9 +47,5 @@ module Geo
def push_remove_branch?
changes.any? { |change| Gitlab::Git.branch_ref?(change[:ref]) && Gitlab::Git.blank_ref?(change[:after]) }
end
def log(message)
Rails.logger.info("#{self.class.name}: #{message} for project #{project.path_with_namespace} (#{project.id})")
end
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment