Skip to content

Commit ac0f5f2

Browse files
authored
Merge pull request #1009 from alphagov/requeue-by-type
Update requeue task to requeue by document type
2 parents 9a61eb0 + acb9f16 commit ac0f5f2

File tree

4 files changed

+43
-41
lines changed

4 files changed

+43
-41
lines changed

lib/queue_publisher.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ def connection
1818
class PublishFailedError < StandardError
1919
end
2020

21-
def send_message(edition, event_type: nil, routing_key: nil)
21+
def send_message(edition, event_type: nil, routing_key: nil, persistent: true)
2222
return if @noop
2323
routing_key ||= routing_key(edition, event_type)
24-
publish_message(routing_key, edition, content_type: 'application/json', persistent: true)
24+
publish_message(routing_key, edition, content_type: 'application/json', persistent: persistent)
2525
end
2626

2727
def routing_key(edition, event_type)

lib/requeue_content.rb

Lines changed: 20 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,33 @@
11
class RequeueContent
2-
def initialize(number_of_items: nil)
3-
@number_of_items = number_of_items
2+
def initialize(scope)
3+
# Restrict scope to stuff that's live (published or unpublished)
4+
# Unpublished content without a content store representation won't
5+
# be returned, but we're not interested in this content.
6+
@scope = scope.where(content_store: :live)
7+
@version = Event.maximum(:id)
48
end
59

6-
attr_accessor :number_of_items
7-
810
def call
9-
if number_of_items.present?
10-
Edition.where(state: :published).limit(number_of_items).each do |edition|
11-
publish_to_queue(edition)
12-
end
13-
else
14-
Edition.where(state: :published).find_each do |edition|
15-
publish_to_queue(edition)
16-
end
11+
scope.each do |edition|
12+
publish_to_queue(edition)
1713
end
1814
end
1915

2016
private
2117

22-
def publish_to_queue(edition)
23-
version = Event.maximum(:id)
24-
25-
queue_payload = Presenters::EditionPresenter.new(
26-
edition, draft: false,
27-
).for_message_queue(version)
18+
attr_reader :scope, :version
2819

29-
# FIXME: Rummager currently only listens to the message queue for the
30-
# event type 'links'. This behaviour will eventually be updated so that
31-
# it listens to other update types as well. This will happen as part of
32-
# ongoing architectural work to make the message queue the sole source of
33-
# search index updates. When that happens, the event_type below should
34-
# be changed - perhaps to a newly introduced, more-appropriately named
35-
# one. Maybe something like 'reindex'.
20+
def publish_to_queue(edition)
21+
presenter = DownstreamPayload.new(edition, version, draft: false)
22+
queue_payload = presenter.message_queue_payload
23+
service = PublishingAPI.service(:queue_publisher)
3624

37-
PublishingAPI.service(:queue_publisher).send_message(queue_payload, event_type: "links")
25+
# Requeue is considered a different event_type to major, minor etc
26+
# because we don't want to send additional email alerts to users.
27+
service.send_message(
28+
queue_payload,
29+
routing_key: "#{edition.schema_name}.bulk.reindex",
30+
persistent: false
31+
)
3832
end
3933
end

lib/tasks/queue.rake

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,16 @@ namespace :queue do
2828
end
2929
end
3030

31-
desc "Add published editions to the message queue, optionally specifying a limit on the number of items"
32-
task :requeue_content, [:number_of_items] => :environment do |_, args|
33-
RequeueContent.new(number_of_items: args[:number_of_items]).call
31+
desc "Add published editions to the message queue by document type"
32+
task :requeue_document_type, [:document_type] => :environment do |_, args|
33+
document_type = args[:document_type]
34+
raise ValueError("expecting document_type") unless document_type.present?
35+
36+
scope = Edition
37+
.with_document
38+
.with_unpublishing
39+
.where(document_type: document_type)
40+
41+
RequeueContent.new(scope).call
3442
end
3543
end

spec/lib/requeue_content_spec.rb

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,20 @@
55
FactoryGirl.create(:live_edition, base_path: '/ci1')
66
FactoryGirl.create(:live_edition, base_path: '/ci2')
77
FactoryGirl.create(:live_edition, base_path: '/ci3')
8+
FactoryGirl.create(:gone_live_edition, base_path: '/ci4')
9+
FactoryGirl.create(:redirect_live_edition, base_path: '/ci5')
10+
FactoryGirl.create(:draft_edition, base_path: '/ci5')
811
end
912

1013
describe "#call" do
11-
it "by default, it republishes all editions" do
12-
expect(PublishingAPI.service(:queue_publisher)).to receive(:send_message).exactly(3).times
13-
RequeueContent.new.call
14-
end
14+
it "it republishes all live editions" do
15+
scope = Edition
16+
.with_document
17+
.with_unpublishing
18+
19+
expect(PublishingAPI.service(:queue_publisher)).to receive(:send_message).exactly(5).times
1520

16-
it "limits the number of items published, if a limit is provided" do
17-
expect(PublishingAPI.service(:queue_publisher)).to receive(:send_message)
18-
.exactly(1)
19-
.times
20-
.with(a_hash_including(:content_id), event_type: "links")
21-
RequeueContent.new(number_of_items: 1).call
21+
RequeueContent.new(scope).call
2222
end
2323
end
2424
end

0 commit comments

Comments
 (0)