Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure deleted assets are marked as live #9828

Merged
merged 7 commits into from
Jan 22, 2025

Conversation

lauraghiorghisor-tw
Copy link
Contributor

@lauraghiorghisor-tw lauraghiorghisor-tw commented Jan 17, 2025

We want to separate out the catch-all logic we currently have around asset updates. The AttachmentUpdater service listener was listening on all possible events, making it hard to understand which specific updates need to happen in which case. Additionally, in the MetadataWorker that it enqueued, the update and delete logic fired together. For deletion, similar updates used to fire on attachment deletion, edition publish and edition discard.

We now understand that, due to the limitations of how we can represent draft/live states in Asset Manager, we can only delete a pre-existing live asset at the point of publish. Any earlier, and the live edition's attachment would become unavailable. Thus, we decided to separate out the Asset Manager update and delete calls that run at edition publication time.

These changes are being made in the context of a bug that causes assets to remain live when a previously published attachment gets replaced and deleted. The issue was that the deleted? method on the replacement AttachmentData evaluated to true when attempting to update the deleted asset to live (draft: false), causing the update to be skipped. By separating the publish logic we are confident in removing the deleted? check on update.

  • These changes make it clear that an attachment deletion should only be sent to Asset Manager at the point of publish.
  • Removed the asset deletion/update call from the AttachmentsController's destroy method, which only ever succeeded for first draft editions before.
  • Removed the delete calls to Asset Manager that were mixed in with the updates in the MetadataWorker - deletion is now only triggered from the publish listener, at the point of publish.
  • 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.
  • Keeping in line with the async implementation we had before (all updates and deletions were run from the MetadataWorker), we added jobs inside the listeners.

In addition to the edition workflow changes, we've had to make sure asset deletion also works for Policy Groups, as they are not editionable.

Trello

@lauraghiorghisor-tw lauraghiorghisor-tw force-pushed the ensure-deleted-assets-are-marked-as-live branch 2 times, most recently from 9cc2978 to e384346 Compare January 17, 2025 11:17
@lauraghiorghisor-tw lauraghiorghisor-tw marked this pull request as ready for review January 17, 2025 11:17
Copy link
Contributor

@ryanb-gds ryanb-gds left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me, a few comments notwithstanding. I would like a review from @ChrisBAshton on this before we merge it just to see if he likes the direction of travel. Personally though I feel having service listeners and jobs that respond to particular changes in edition state is making things much easier to understand. The old job that tried to work out the changes it needs to make to Asset Manager from the state of the edition was very confusing.

app/services/service_listeners/attachment_asset_deleter.rb Outdated Show resolved Hide resolved
config/initializers/edition_services.rb Outdated Show resolved Hide resolved
test/integration/attachment_deletion_integration_test.rb Outdated Show resolved Hide resolved
config/initializers/edition_services.rb Outdated Show resolved Hide resolved
@lauraghiorghisor-tw lauraghiorghisor-tw force-pushed the ensure-deleted-assets-are-marked-as-live branch 2 times, most recently from d0155f1 to 806798e Compare January 17, 2025 12:44
Copy link
Contributor

@ChrisBAshton ChrisBAshton left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice one for getting your heads around this! ⭐

config/initializers/edition_services.rb Outdated Show resolved Hide resolved
app/sidekiq/asset_manager_attachment_metadata_worker.rb Outdated Show resolved Hide resolved
config/initializers/edition_services.rb Outdated Show resolved Hide resolved
@lauraghiorghisor-tw lauraghiorghisor-tw force-pushed the ensure-deleted-assets-are-marked-as-live branch from 806798e to 81a13dd Compare January 17, 2025 15:36
These tests now explicitly cover how deletion works, namely that, in the case of already published editions, an attachment deletion on a new draft will only ever be sent to Asset Manager at the point of publish.

Changes:
- Add test to cover replaced assets - to capture a known bug where attachments that are replaced and then deleted, cause the original replaced attachment's assets to remain live, rather than redirect to their replacement.
- Fix existing test so that it triggers the edition service listener when publishing.
We want to separate out the catch-all logic we currently have around asset updates. The `AttachmentUpdater` service listener was listening on all possible events, making it hard to understand which specific updates need to happen in which case. Additionally, in the `MetadataWorker` that it enqueues, the update and delete logic fired together.

These changes are being made in the context of a bug that causes assets to remain live when a new draft attachment gets replaced and deleted. The issue was that the `deleted?` method on the replaced `AttachmentData` evaluated to true when attempting to update the asset to live after deleting the attachment. By separating the publish logic we are confident in removing the `deleted?` on update.

We also removed the deletion call from the attachment controller's destroy method, so that the only delete calls to Asset Manager now happen from the publish listener, at the point of publish.
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.
@ryanb-gds ryanb-gds force-pushed the ensure-deleted-assets-are-marked-as-live branch from e7be563 to 860585e Compare January 20, 2025 15:35
This log tracked how many times we attempted to update a deleted asset, with the assumption that that is fault in the system.
We now know that we actually need to update deleted draft assets for replacements to work, therefore removing the logging.
@ryanb-gds ryanb-gds force-pushed the ensure-deleted-assets-are-marked-as-live branch 2 times, most recently from 79317e7 to dae69db Compare January 21, 2025 13:22
…blishing and include parent document urls

We want to ensure that the draft value for each asset is set to false when the attachable is published, but Asset Manager does not support updates to assets that are already deleted and not drafts. There's also no real need to update any asset that is not a draft in Asset Manager when publishing. The only time an update might be required is after editing a slug in Whitehall, but that should be handled following the slug editing operation (however it isn't currently). Note that we may still need to delete assets that are not drafts, so we always attempt to delete any assets where attachment_data.deleted? is true.

The only way we can think of identifying which attachment data objects need publishing is to check if the attachment data has a single publicly visible attachable, which would imply that the attachment data was created during the editing of the just published edition.

The previous test was inaccurate in its setup in that it assumed the PublishAttachmentAssetJob would operate with a draft attachable, but in reality it is only enqueued after the state of the attachable has been set to published in the database.

We also include the parent document URL for every asset, to ensure that updating a deleted asset for an attachable that has just been published sets a public Asset Manager URL instead of the draft-origin URL. Asset Manager validates that non-draft assets do not have a draft-origin URL.
@ryanb-gds ryanb-gds force-pushed the ensure-deleted-assets-are-marked-as-live branch from dae69db to e1e6724 Compare January 21, 2025 14:39
…outcome attachment assets

Until now the delete all attachments methods for these edition types was not capturing the outcome attachments. We fix this by ensuring we destroy attachments for all of the attachables associated with the edition.
@ryanb-gds ryanb-gds force-pushed the ensure-deleted-assets-are-marked-as-live branch from d166657 to 77a59a5 Compare January 21, 2025 16:57
@lauraghiorghisor-tw lauraghiorghisor-tw force-pushed the ensure-deleted-assets-are-marked-as-live branch 2 times, most recently from db17a64 to d06d09b Compare January 22, 2025 12:03
Previously, when deleting a policy group attachment, we called the `AttachmentUpdater` - this enqueued the `MetadataWorker`, which dealt with both deletion and updates. We have since separated the update and delete logic for editions. We are now explicit about the fact that the delete request only gets sent to Asset Manager on publish. This logic is captured in the `AttachmentAssetDeleter`.

Nonetheless, since Policy Groups are not editionable, they do not use that workflow - their attachments go live upon creation and should be removed from live immediately upon being destroyed. This commit ensures that assets belonging to a policy group's deleted attachments are deleted from Asset Manager on update.
@lauraghiorghisor-tw lauraghiorghisor-tw force-pushed the ensure-deleted-assets-are-marked-as-live branch from d06d09b to ab930d7 Compare January 22, 2025 12:07
@lauraghiorghisor-tw lauraghiorghisor-tw merged commit dd8a728 into main Jan 22, 2025
19 checks passed
@lauraghiorghisor-tw lauraghiorghisor-tw deleted the ensure-deleted-assets-are-marked-as-live branch January 22, 2025 13:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants