Skip to content

Commit

Permalink
Merge pull request #18 from alphagov/modulize
Browse files Browse the repository at this point in the history
Make publishing event pipeline more modular
  • Loading branch information
csutter authored Sep 26, 2023
2 parents 5cdaa02 + ad5662c commit 6a4735c
Show file tree
Hide file tree
Showing 10 changed files with 54 additions and 18 deletions.
17 changes: 17 additions & 0 deletions lib/publishing_event_pipeline.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
require "publishing_event_pipeline/configuration"
require "publishing_event_pipeline/document"
require "publishing_event_pipeline/document_lifecycle_event"

require "publishing_event_pipeline/message_processor"

require "publishing_event_pipeline/repositories/null_repository"

module PublishingEventPipeline
def self.configuration
@configuration ||= Configuration.new
end

def self.configure
yield(configuration)
end
end
9 changes: 9 additions & 0 deletions lib/publishing_event_pipeline/configuration.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module PublishingEventPipeline
class Configuration
attr_accessor :logger, :repository

def initialize
@logger = Logger.new($stdout)
end
end
end
2 changes: 0 additions & 2 deletions lib/publishing_event_pipeline/document_lifecycle_event.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
require_relative "document"

module PublishingEventPipeline
# Domain model for a content change event coming through from a publishing system
class DocumentLifecycleEvent
Expand Down
5 changes: 1 addition & 4 deletions lib/publishing_event_pipeline/message_processor.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
require_relative "document_lifecycle_event"
require_relative "repositories/null_repository"

module PublishingEventPipeline
# Processes incoming content changes from the publishing message queue.
class MessageProcessor
attr_reader :event_class, :repository

def initialize(
event_class: DocumentLifecycleEvent,
repository: Repositories::NullRepository.new
repository: PublishingEventPipeline.configuration.repository
)
@event_class = event_class
@repository = repository
Expand Down
2 changes: 0 additions & 2 deletions lib/publishing_event_pipeline/repositories/null_repository.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
require_relative "../document"

module PublishingEventPipeline
module Repositories
# A repository that does nothing, for use until we can integrate with the real product.
Expand Down
24 changes: 20 additions & 4 deletions lib/tasks/publishing_event_pipeline.rake
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
require "govuk_message_queue_consumer"
require "publishing_event_pipeline"

require "publishing_event_pipeline/message_processor"

# TODO: For now, this lives within the application repository, but we may want to extract it to a
# completely separate unit if we can keep dependencies between the read and write sides of this
# project to a minimum. This isn't something that is 100% clear at this stage.
#
# Until then, these tasks intentionally run outside the Rails environment to avoid us adding any
# implicit dependencies on the Rails application.
#
# rubocop:disable Rails/RakeEnvironment
namespace :publishing_event_pipeline do
desc "Create RabbitMQ queue for development environment"
task create_queue: :environment do
task :create_queue do
# The exchange, queue, and binding are created via Terraform outside of local development:
# https://github.com/alphagov/govuk-aws/blob/main/terraform/projects/app-publishing-amazonmq/
# TODO: Remove dependency on Rails if extracted to a separate unit
raise "This task should only be run in development" unless Rails.env.development?

bunny = Bunny.new
Expand All @@ -16,10 +24,18 @@ namespace :publishing_event_pipeline do
end

desc "Listens to and processes messages from the published documents queue"
task process_messages: :environment do
task :process_messages do
PublishingEventPipeline.configure do |config|
# TODO: Once we have access to the search product and written a repository for it, this should
# be set to the real repository. Until then, this allows us to verify that the pipeline is
# working as expected through the logs.
config.repository = PublishingEventPipeline::Repositories::NullRepository.new
end

GovukMessageQueueConsumer::Consumer.new(
queue_name: ENV.fetch("PUBLISHING_EVENT_MESSAGE_QUEUE_NAME"),
processor: PublishingEventPipeline::MessageProcessor.new,
).run
end
end
# rubocop:enable Rails/RakeEnvironment
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
require "publishing_event_pipeline/document_lifecycle_event"

RSpec.describe PublishingEventPipeline::DocumentLifecycleEvent do
subject(:event) { described_class.new(message_hash) }

Expand Down
2 changes: 0 additions & 2 deletions spec/lib/publishing_event_pipeline/message_processor_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
require "publishing_event_pipeline/message_processor"

require "govuk_message_queue_consumer"
require "govuk_message_queue_consumer/test_helpers"

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
require "publishing_event_pipeline/repositories/null_repository"

RSpec.describe PublishingEventPipeline::Repositories::NullRepository do
let(:repository) { described_class.new }
let(:content_id) { "some_content_id" }
Expand Down
7 changes: 7 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@
Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }
GovukTest.configure

# TODO: If the write side of this application is extracted to a separate unit, we will need to
# remove this, otherwise it can be made permanent.
require "publishing_event_pipeline"
PublishingEventPipeline.configure do |config|
config.repository = PublishingEventPipeline::Repositories::NullRepository.new
end

# See https://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
RSpec.configure do |config|
config.default_formatter = "doc" if config.files_to_run.one?
Expand Down

0 comments on commit 6a4735c

Please sign in to comment.