feat: #3586481 Prevent duplicate Queue/Cron items when entity is saved multiple times

Description

Resolves #3586481.

When using the Queue/Cron worker type, saving an entity multiple times before cron runs causes duplicate queue items to accumulate for the same entity/field combination, leading to redundant AI API calls and unpredictable processing order.

This MR adds a deduplication check to QueueWorkerProcessor::modify() that queries the queue table before creating a new item. If a pending item for the same entity/field already exists, the new item is skipped. A new per-field checkbox "Re-queue on each save" lets site builders override this behaviour when they explicitly need the latest content to always be re-processed.

The direct database query approach mirrors the existing AutomatorFieldData::lastInQueue() method in the same module — Drupal's QueueInterface has no API for inspecting pending items, and queue items are not Drupal entities, so a database query is the only correct approach.

Changed files:

  • modules/ai_automators/src/Plugin/AiAutomatorProcess/QueueWorkerProcessor.php — adds Connection injection, isAlreadyQueued() helper, and early-return guard in modify()
  • modules/ai_automators/src/FormAlter/AiAutomatorFieldConfig.php — adds the "Re-queue on each save" checkbox, visible only when Queue/Cron is selected

Re-queue_on_each_save

Testing instructions

  1. Enable the ai_automators module and configure an AI Automator on a node field using the Queue/Cron worker type.

  2. Confirm the new "Re-queue on each save" checkbox appears below the worker type selection and is unchecked by default.

  3. Save the node twice without running cron. Verify only one queue item exists:

    drush sqlq "SELECT COUNT(*) FROM queue WHERE name='ai_automator_field_modifier';"
  4. Check "Re-queue on each save", save the node twice again, and confirm two items are now added.

  5. Run drush cron and confirm the field is populated correctly in both cases.

Checklist

  • I have linked the related issue in the MR title or description
  • I have performed a self-review of my own code
  • I have added or updated tests, or explained in the description why this change is not covered by tests
  • I have written testing instructions and verified them locally
  • This MR contains no breaking API or hook changes, or they are explicitly documented in the description

AI Compliance

Note

  • AI Generated Code
    Mainly generated by AI, reviewed and approved by a human before this MR was created.
Edited by Christoph Breidert

Merge request reports

Loading