Entity references can result in multiple builds

Created on 3 March 2023, over 1 year ago
Updated 30 March 2023, about 1 year ago

Problem/Motivation

Updating an entity with entity references triggers hook_entity_update for each of the referenced entities. Since a build is triggered for every entity that is updated, this can result in several builds when only one would suffice. This could be problematic for sites using a frontend that has a limited number of builds per month.

Steps to reproduce

Create a node with a multi-value field that references other nodes.
Configure the build hooks module so that Content is considered a loggable entity
Configure the frontend(s) deployment strategy to be: "When content is updated"
Create a node that references multiple nodes
Multiple builds are triggered.

Proposed resolution

Update the hook_entity_* logic to mark/flag frontends for deployment
Use a request event subscriber that checks the flag and triggers a build if it is necessary

✨ Feature request
Status

Needs work

Version

3.3

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States steyep

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @steyep
  • Status changed to Needs review over 1 year ago
  • πŸ‡ΊπŸ‡ΈUnited States steyep

    Attaching a patch of the proposed solution

  • The last submitted patch, 2: 3345885-2.patch, failed testing. View results β†’
    - codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

  • Status changed to Needs work over 1 year ago
  • πŸ‡ΊπŸ‡ΈUnited States steyep

    Fixing test failures

  • πŸ‡ΊπŸ‡ΈUnited States steyep

    Modifies the patch to use KernelEvents::TERMINATE event so that the builds are triggered when using $entity->save();.

  • πŸ‡¦πŸ‡ΊAustralia larowlan πŸ‡¦πŸ‡ΊπŸ.au GMT+10

    Seems like a good idea, but we'll probably need to manually check the state in tests and trigger.

    There's also \Drupal\Core\DestructableInterface in core which can be used instead of the terminate event

  • πŸ‡¦πŸ‡ΊAustralia larowlan πŸ‡¦πŸ‡ΊπŸ.au GMT+10
    +++ b/src/EventSubscriber/BuildHooksResponseSubscriber.php
    @@ -0,0 +1,52 @@
    +      $this->trigger->deployFrontendEntityUpdateEnvironments();
    

    this should delete the state flag too right? otherwise it just keeps triggering?

  • πŸ‡ΊπŸ‡ΈUnited States steyep

    Using the needs_destruction tag is a good idea but I think that (and now that i'm thinking about it - the terminate event) occur too late in the lifecycle to invalidate the cache tag and write a message using the messenger service.

    The deployFrontendEntityUpdateEnvironments will delete the state flag which should prevent multiple runs.

    I've attached a revised patch

  • πŸ‡¦πŸ‡ΊAustralia larowlan πŸ‡¦πŸ‡ΊπŸ.au GMT+10

    Looks like there's some failing tests here

Production build 0.69.0 2024