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

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

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 enqueues, 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 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? 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 fired 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.

@lauraghiorghisor-tw lauraghiorghisor-tw force-pushed the ensure-deleted-assets-are-marked-as-live branch from 81b8453 to 9cc2978 Compare January 17, 2025 09:48
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.
@lauraghiorghisor-tw lauraghiorghisor-tw force-pushed the ensure-deleted-assets-are-marked-as-live branch 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
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.
@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
The `attachable_url` for an attachment_data` always evaluates to nil when the `attachment_data` deleted? is true.
However, there is no reason to include this in the request to asset manager, when updating published assets.

The `AssetUpdater` compares the request it's about to send to the current state of Asset Manager, and cancels the request if it matches the state in Asset Manager. When the `parent_document_url` is nil, the comparison is unequal and the request gets made and subsequently fails - because Asset Manager rejects requests to update assets that are both deleted and non-draft.
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.
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