Commit 81b7d6ad authored by Mike Kozono's avatar Mike Kozono

Automatically publish created events

For Models with Replicators that define the
"handle_after_create_commit" hook method.
parent 678846d1
...@@ -11,8 +11,7 @@ module Geo ...@@ -11,8 +11,7 @@ module Geo
class_methods do class_methods do
end end
# Called by Packages::PackageFile on create def handle_after_create_commit
def publish_created_event
publish(:created, **created_params) publish(:created, **created_params)
end end
......
...@@ -35,7 +35,6 @@ class Packages::PackageFile < ApplicationRecord ...@@ -35,7 +35,6 @@ class Packages::PackageFile < ApplicationRecord
with_replicator Geo::PackageFileReplicator with_replicator Geo::PackageFileReplicator
after_save :update_file_metadata, if: :saved_change_to_file? after_save :update_file_metadata, if: :saved_change_to_file?
after_create_commit -> { replicator.publish_created_event }
update_project_statistics project_statistics_name: :packages_size update_project_statistics project_statistics_name: :packages_size
......
...@@ -3,11 +3,14 @@ ...@@ -3,11 +3,14 @@
module Gitlab module Gitlab
module Geo module Geo
module ReplicableModel module ReplicableModel
def self.included(klass) extend ActiveSupport::Concern
klass.extend(ClassMethods)
included do
# If this hook turns out not to apply to all Models, perhaps we should extract a `ReplicableBlobModel`
after_create_commit -> { replicator.handle_after_create_commit if replicator.respond_to?(:handle_after_create_commit) }
end end
module ClassMethods class_methods do
def with_replicator(klass) def with_replicator(klass)
raise ArgumentError, 'Must be a class inheriting from Gitlab::Geo::Replicator' unless klass < ::Gitlab::Geo::Replicator raise ArgumentError, 'Must be a class inheriting from Gitlab::Geo::Replicator' unless klass < ::Gitlab::Geo::Replicator
......
...@@ -38,6 +38,10 @@ describe Gitlab::Geo::Replicator do ...@@ -38,6 +38,10 @@ describe Gitlab::Geo::Replicator do
context 'model DSL' do context 'model DSL' do
class DummyModel class DummyModel
include ActiveModel::Model include ActiveModel::Model
def self.after_create_commit(*args)
end
include Gitlab::Geo::ReplicableModel include Gitlab::Geo::ReplicableModel
with_replicator DummyReplicator with_replicator DummyReplicator
......
...@@ -18,10 +18,10 @@ RSpec.shared_examples 'a blob replicator' do ...@@ -18,10 +18,10 @@ RSpec.shared_examples 'a blob replicator' do
stub_current_geo_node(primary) stub_current_geo_node(primary)
end end
describe '#publish_created_event' do describe '#handle_after_create_commit' do
it 'creates a Geo::Event' do it 'creates a Geo::Event' do
expect do expect do
replicator.publish_created_event replicator.handle_after_create_commit
end.to change { ::Geo::Event.count }.by(1) end.to change { ::Geo::Event.count }.by(1)
expect(::Geo::Event.last.attributes).to include( expect(::Geo::Event.last.attributes).to include(
...@@ -60,5 +60,18 @@ RSpec.shared_examples 'a blob replicator' do ...@@ -60,5 +60,18 @@ RSpec.shared_examples 'a blob replicator' do
it 'is a Class' do it 'is a Class' do
expect(invoke_model).to be_a(Class) expect(invoke_model).to be_a(Class)
end end
# For convenience (and reliability), instead of asking developers to include shared examples on each model spec as well
context 'replicable model' do
it 'defines #replicator' do
expect(model_record).to respond_to(:replicator)
end
it 'invokes replicator.handle_after_create_commit on create' do
expect(replicator).to receive(:handle_after_create_commit)
model_record.save!
end
end
end 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