Skip to content

Commit

Permalink
Merge pull request #15 from alphagov/publish-publishing
Browse files Browse the repository at this point in the history
Add "gone" event handling; rename publish->ing
  • Loading branch information
csutter authored Sep 20, 2023
2 parents c58faae + 605747d commit eeecfe8
Show file tree
Hide file tree
Showing 9 changed files with 91 additions and 52 deletions.
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ gem "railties", RAILS_GEMS_VERSION
gem "bootsnap", require: false
gem "govuk_app_config"

# Gems for publish_event_pipeline
# Gems for publishing_event_pipeline
gem "govuk_message_queue_consumer", require: false

group :test do
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require_relative "publish_event"
require_relative "publishing_event"

module PublishEventPipeline
module PublishingEventPipeline
# Processes incoming content changes from the publishing message queue.
class MessageProcessor
# Implements the callback interface required by `govuk_message_queue_consumer`
Expand All @@ -15,12 +15,12 @@ def initialize(message)
end

def call
publish_event = PublishEvent.from_message_hash(message.payload)
document = publish_event.document
publishing_event = PublishingEvent.from_message_hash(message.payload)
document = publishing_event.document
Rails.logger.info(
sprintf(
"Received %s: %s ('%s')",
publish_event.update_type, document.content_id, document.title
publishing_event.update_type, document.content_id, document.title
),
)
message.ack
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
require "domain/searchable_document_data"

module PublishEventPipeline
module PublishingEventPipeline
# Domain model for a content change event coming through from a publishing system
#
# @attr_reader [String] update_type The type of update that has occurred
# @attr_reader [Integer] payload_version The payload version of the update
# @attr_reader [Document] document The document that has been added/modified/deleted
PublishEvent = Data.define(:update_type, :payload_version, :document) do
# Creates a PublishEvent from a message hash conforming to the publishing schema.
PublishingEvent = Data.define(:update_type, :payload_version, :document) do
# Creates a PublishingEvent from a message hash conforming to the publishing schema.
#
# @param message_hash [Hash] The message hash to create the document from.
# @return [PublishEvent] The newly created PublishEvent instance.
# @return [PublishingEvent] The newly created PublishingEvent instance.
def self.from_message_hash(message_hash)
new(
update_type: message_hash.fetch("update_type"),
update_type: message_hash["update_type"],
payload_version: message_hash.fetch("payload_version"),
document: SearchableDocumentData.new(
content_id: message_hash.fetch("content_id"),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
require "govuk_message_queue_consumer"

require "publish_event_pipeline/message_processor"
require "publishing_event_pipeline/message_processor"

namespace :publish_event_pipeline do
namespace :publishing_event_pipeline do
desc "Create RabbitMQ queue for development environment"
task create_queue: :environment do
# The exchange, queue, and binding are created via Terraform outside of local development:
Expand All @@ -12,14 +12,14 @@ namespace :publish_event_pipeline do
bunny = Bunny.new
channel = bunny.start.create_channel
exch = Bunny::Exchange.new(channel, :topic, "published_documents")
channel.queue(ENV.fetch("PUBLISH_EVENT_MESSAGE_QUEUE_NAME")).bind(exch, routing_key: "*.*")
channel.queue(ENV.fetch("PUBLISHING_EVENT_MESSAGE_QUEUE_NAME")).bind(exch, routing_key: "*.*")
end

desc "Listens to and processes messages from the published documents queue"
task process_messages: :environment do
GovukMessageQueueConsumer::Consumer.new(
queue_name: ENV.fetch("PUBLISH_EVENT_MESSAGE_QUEUE_NAME"),
processor: PublishEventPipeline::MessageProcessor,
queue_name: ENV.fetch("PUBLISHING_EVENT_MESSAGE_QUEUE_NAME"),
processor: PublishingEventPipeline::MessageProcessor,
).run
end
end
21 changes: 21 additions & 0 deletions spec/fixtures/files/message_queue/gone_message.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"document_type": "gone",
"schema_name": "gone",
"base_path": "/government/publications/guidance-for-parents-and-carers-on-safeguarding-children-in-out-of-school-settings.lt",
"locale": "lt",
"publishing_app": "whitehall",
"public_updated_at": "2023-09-15T10:25:05Z",
"details": {
"explanation": "<div class=\"govspeak\"><p>This translation is no longer available. You can find the original version of this content at <a href=\"https://www.gov.uk/government/publications/guidance-for-parents-and-carers-on-safeguarding-children-in-out-of-school-settings\" class=\"govuk-link\">https://www.gov.uk/government/publications/guidance-for-parents-and-carers-on-safeguarding-children-in-out-of-school-settings</a></p>\n</div>",
"alternative_path": ""
},
"routes": [
{
"path": "/government/publications/guidance-for-parents-and-carers-on-safeguarding-children-in-out-of-school-settings.lt",
"type": "exact"
}
],
"content_id": "966bae6d-223e-4102-a6e5-e874012390e5",
"govuk_request_id": "21-1695137029.556-10.13.22.59-107886",
"payload_version": 65893230
}
33 changes: 0 additions & 33 deletions spec/lib/publish_event_pipeline/publish_event_spec.rb

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
require "publish_event_pipeline/message_processor"
require "publishing_event_pipeline/message_processor"

require "govuk_message_queue_consumer"
require "govuk_message_queue_consumer/test_helpers"

RSpec.describe PublishEventPipeline::MessageProcessor do
RSpec.describe PublishingEventPipeline::MessageProcessor do
describe ".process" do
subject(:class_acting_as_processor) { described_class }

Expand All @@ -14,7 +14,7 @@
subject(:command) { described_class.new(message) }

let(:message) { GovukMessageQueueConsumer::MockMessage.new(payload) }
let(:payload) { json_fixture_as_hash("message_queue/message.json") }
let(:payload) { json_fixture_as_hash("message_queue/republish_message.json") }

before do
allow(Rails.logger).to receive(:info)
Expand Down
51 changes: 51 additions & 0 deletions spec/lib/publishing_event_pipeline/publishing_event_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
require "publishing_event_pipeline/publishing_event"

RSpec.describe PublishingEventPipeline::PublishingEvent do
describe ".from_message_hash" do
subject(:publishing_event) { described_class.from_message_hash(message_hash) }

context "with a valid republish message" do
let(:message_hash) { json_fixture_as_hash("message_queue/republish_message.json") }

it "maps the message onto a PublishingEvent" do
expected_document = SearchableDocumentData.new(
content_id: "f75d26a3-25a4-4c31-beea-a77cada4ce12",
title: "Ebola medal for over 3000 heroes",
)
expected_publishing_event = described_class.new(
update_type: "republish",
payload_version: 65_861_808,
document: expected_document,
)

expect(publishing_event).to eq(expected_publishing_event)
end
end

context "with a valid gone message" do
let(:message_hash) { json_fixture_as_hash("message_queue/gone_message.json") }

it "maps the message onto a PublishingEvent" do
expected_document = SearchableDocumentData.new(
content_id: "966bae6d-223e-4102-a6e5-e874012390e5",
title: nil,
)
expected_publishing_event = described_class.new(
update_type: nil,
payload_version: 65_893_230,
document: expected_document,
)

expect(publishing_event).to eq(expected_publishing_event)
end
end

context "with a message hash missing required fields" do
let(:message_hash) { { "title" => "I'm incomplete" } }

it "raises an error" do
expect { publishing_event }.to raise_error(KeyError)
end
end
end
end

0 comments on commit eeecfe8

Please sign in to comment.