-
Notifications
You must be signed in to change notification settings - Fork 193
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add custom asset deleter to deal with attachable discard
We want to separate out the catch-all logic we currently have around asset updates. In the `MetadataWorker` the update and delete logic were firing together, making it difficult to understand which specific updates, if any, we need when deleting. Additionally, the same updates would fire on attachment deletion, edition publish and edition discard. We have now made it clear (previous commits) that an attachment deletion should only be sent to Asset Manager at the point of publish. This commit deals with the remaining logic around discard. We used to call the `AttachmentDeleter` from inside the `MetadataWorker`, which suggested updates are being made at discard time. We have now extracted what needs to happen on edition discard into a service listener, making it clear that no updates are firing, only asset deletion.
- Loading branch information
1 parent
d3ad536
commit e384346
Showing
10 changed files
with
99 additions
and
76 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
13 changes: 13 additions & 0 deletions
13
app/services/service_listeners/attachment_asset_deleter.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
module ServiceListeners | ||
class AttachmentAssetDeleter | ||
def self.call(attachable:) | ||
Attachment.includes(:attachment_data).where(attachable: attachable.attachables).find_each do |attachment| | ||
attachment_data = attachment.attachment_data | ||
|
||
next unless attachment_data&.deleted? | ||
|
||
DeleteAttachmentAssetJob.perform_async(attachment_data.id) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
class DeleteAttachmentAssetJob < WorkerBase | ||
sidekiq_options queue: "asset_manager" | ||
|
||
def perform(attachment_data_id) | ||
attachment_data = AttachmentData.find(attachment_data_id) | ||
|
||
attachment_data.assets.each do |asset| | ||
AssetManager::AssetDeleter.call(asset.asset_manager_id) | ||
rescue AssetManager::ServiceHelper::AssetNotFound | ||
logger.info("Asset #{asset.asset_manager_id} has already been deleted from Asset Manager") | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 0 additions & 43 deletions
43
test/unit/app/services/asset_manager/attachment_deleter_test.rb
This file was deleted.
Oops, something went wrong.
36 changes: 36 additions & 0 deletions
36
test/unit/app/services/service_listeners/attachment_asset_deleter_test.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
require "test_helper" | ||
|
||
module ServiceListeners | ||
class AttachmentAssetDeleterTest < ActiveSupport::TestCase | ||
extend Minitest::Spec::DSL | ||
|
||
describe ServiceListeners::AttachmentAssetDeleter do | ||
let(:edition) { create(:draft_news_article) } | ||
let(:first_attachment) { create(:file_attachment, attachable: edition, attachment_data: create(:attachment_data, attachable: edition)) } | ||
let(:second_attachment) { create(:file_attachment, attachable: edition, attachment_data: create(:attachment_data, attachable: edition)) } | ||
|
||
before do | ||
stub_asset(first_attachment.attachment_data.assets.first.asset_manager_id) | ||
stub_asset(second_attachment.attachment_data.assets.first.asset_manager_id) | ||
end | ||
|
||
it "calls deleter for all assets of all attachments" do | ||
edition.delete! | ||
edition.delete_all_attachments | ||
|
||
AssetManager::AssetDeleter.expects(:call).with(first_attachment.attachment_data.assets.first.asset_manager_id) | ||
AssetManager::AssetDeleter.expects(:call).with(second_attachment.attachment_data.assets.first.asset_manager_id) | ||
|
||
ServiceListeners::AttachmentAssetDeleter.call(attachable: edition) | ||
DeleteAttachmentAssetJob.drain | ||
end | ||
|
||
def stub_asset(asset_manger_id, attributes = {}) | ||
url_id = "http://asset-manager/assets/#{asset_manger_id}" | ||
Services.asset_manager.stubs(:asset) | ||
.with(asset_manger_id) | ||
.returns(attributes.merge(id: url_id).stringify_keys) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
require "test_helper" | ||
|
||
class DeleteAttachmentAssetJobTest < ActiveSupport::TestCase | ||
extend Minitest::Spec::DSL | ||
|
||
describe DeleteAttachmentAssetJob do | ||
let(:attachable) { create(:draft_news_article) } | ||
let(:attachment_data) { create(:attachment_data, attachable:) } | ||
let(:asset_manager_id) { attachment_data.assets.first.asset_manager_id } | ||
let(:worker) { DeleteAttachmentAssetJob.new } | ||
|
||
it "deletes the asset" do | ||
AssetManager::AssetDeleter.expects(:call).with(asset_manager_id) | ||
|
||
worker.perform(attachment_data.id) | ||
end | ||
|
||
it "raises an error if the asset deletion fails for an unknown reason" do | ||
expected_error = GdsApi::HTTPServerError.new(500) | ||
AssetManager::AssetDeleter.expects(:call).raises(expected_error) | ||
|
||
assert_raises(GdsApi::HTTPServerError) { worker.perform(attachment_data.id) } | ||
end | ||
end | ||
end |