Skip to content

Commit

Permalink
Merge pull request #2566 from alphagov/optionally-put-publish-intents…
Browse files Browse the repository at this point in the history
…-to-content-store-asynchronously

Optionally send PublishIntent additions/deletions to content store asynchronously
  • Loading branch information
aldavidson authored Dec 11, 2023
2 parents 67f1788 + 4e29961 commit 26dcc4e
Show file tree
Hide file tree
Showing 6 changed files with 153 additions and 6 deletions.
9 changes: 8 additions & 1 deletion app/commands/delete_publish_intent.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,14 @@ module Commands
class DeletePublishIntent < BaseCommand
def call
if downstream
PublishingAPI.service(:live_content_store).delete_publish_intent(base_path)
enqueue = ENV.fetch("ENQUEUE_PUBLISH_INTENTS", false)
if enqueue == "true"
DeletePublishIntentWorker.perform_async(
"base_path" => base_path,
)
else
PublishingAPI.service(:live_content_store).delete_publish_intent(base_path)
end
end

Success.new({})
Expand Down
10 changes: 9 additions & 1 deletion app/commands/put_publish_intent.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,15 @@ def call

if downstream
payload = publish_intent
Adapters::ContentStore.put_publish_intent(base_path, payload)
enqueue = ENV.fetch("ENQUEUE_PUBLISH_INTENTS", false)
if enqueue == "true"
PutPublishIntentWorker.perform_async(
"base_path" => base_path,
"payload" => payload.to_json,
)
else
Adapters::ContentStore.put_publish_intent(base_path, payload)
end
end

Success.new(payload)
Expand Down
25 changes: 25 additions & 0 deletions app/workers/delete_publish_intent_worker.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
require "sidekiq-unique-jobs"

class DeletePublishIntentWorker
include DownstreamQueue
include Sidekiq::Worker
include PerformAsyncInQueue

sidekiq_options queue: HIGH_QUEUE,
lock: :until_executing,
lock_args_method: :uniq_args,
on_conflict: :log

def self.uniq_args(args)
[
args.first["base_path"],
name,
]
end

def perform(args = {})
PublishingAPI.service(:live_content_store).delete_publish_intent(args["base_path"])
rescue AbortWorkerError => e
notify_airbrake(e, args)
end
end
25 changes: 25 additions & 0 deletions app/workers/put_publish_intent_worker.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
require "sidekiq-unique-jobs"

class PutPublishIntentWorker
include DownstreamQueue
include Sidekiq::Worker
include PerformAsyncInQueue

sidekiq_options queue: HIGH_QUEUE,
lock: :until_executing,
lock_args_method: :uniq_args,
on_conflict: :log

def self.uniq_args(args)
[
args.first["base_path"],
name,
]
end

def perform(args = {})
Adapters::ContentStore.put_publish_intent(args["base_path"], JSON.parse(args["payload"]))
rescue AbortWorkerError => e
notify_airbrake(e, args)
end
end
50 changes: 48 additions & 2 deletions spec/commands/delete_publish_intent_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,63 @@
}
end

let(:content_store) { PublishingAPI.service(:live_content_store) }

it "responds successfully" do
result = described_class.call(payload)
expect(result).to be_a(Commands::Success)
end

context "when the downstream flag is set to false" do
it "does not send any downstream requests" do
expect(DownstreamDraftWorker).not_to receive(:perform_async)
expect(DownstreamLiveWorker).not_to receive(:perform_async)
expect(content_store).not_to receive(:delete_publish_intent)

described_class.call(payload, downstream: false)
end
end

context "when the downstream flag is set to false" do
it "does not send any downstream requests" do
expect(content_store).not_to receive(:delete_publish_intent)

described_class.call(payload, downstream: false)
end
end

context "when the downstream flag is set to true" do
context "and the ENQUEUE_PUBLISH_INTENTS flag is true" do
before do
ENV["ENQUEUE_PUBLISH_INTENTS"] = "true"
end

it "enqueues a DeletePublishIntent job" do
expect(DeletePublishIntentWorker).to receive(:perform_async)
described_class.call(payload, downstream: true)
end
end

context "and the ENQUEUE_PUBLISH_INTENTS flag is not true" do
before do
ENV["ENQUEUE_PUBLISH_INTENTS"] = "no"
end

it "does send downstream requests" do
expect(content_store).to receive(:delete_publish_intent)

described_class.call(payload, downstream: true)
end
end

context "and the ENQUEUE_PUBLISH_INTENTS flag is not present" do
before do
ENV.delete("ENQUEUE_PUBLISH_INTENTS")
end

it "does send downstream requests" do
expect(content_store).to receive(:delete_publish_intent)

described_class.call(payload, downstream: true)
end
end
end
end
40 changes: 38 additions & 2 deletions spec/commands/put_publish_intent_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,46 @@

context "when the downstream flag is set to false" do
it "does not send any downstream requests" do
expect(DownstreamDraftWorker).not_to receive(:perform_async)
expect(DownstreamLiveWorker).not_to receive(:perform_async)
expect(Adapters::ContentStore).not_to receive(:put_publish_intent)

described_class.call(payload, downstream: false)
end
end

context "when the downstream flag is set to true" do
context "and the ENQUEUE_PUBLISH_INTENTS flag is true" do
before do
ENV["ENQUEUE_PUBLISH_INTENTS"] = "true"
end

it "enqueues a PutPublishIntent job" do
expect(PutPublishIntentWorker).to receive(:perform_async)
described_class.call(payload, downstream: true)
end
end

context "and the ENQUEUE_PUBLISH_INTENTS flag is not true" do
before do
ENV["ENQUEUE_PUBLISH_INTENTS"] = "no"
end

it "does send downstream requests" do
expect(Adapters::ContentStore).to receive(:put_publish_intent)

described_class.call(payload, downstream: true)
end
end

context "and the ENQUEUE_PUBLISH_INTENTS flag is not present" do
before do
ENV.delete("ENQUEUE_PUBLISH_INTENTS")
end

it "does send downstream requests" do
expect(Adapters::ContentStore).to receive(:put_publish_intent)

described_class.call(payload, downstream: true)
end
end
end
end

0 comments on commit 26dcc4e

Please sign in to comment.