site-kit-wp icon indicating copy to clipboard operation
site-kit-wp copied to clipboard

Send errors to admins via email (if possible)

Open benbowler opened this issue 1 month ago • 1 comments

Feature Description

Update the scheduler system so that if an entire batch fails to send, including all fallbacks, the error is sent to relevant admins. Only errors which do not prevent email sending can be sent.


Do not alter or remove anything below. The following sections will be managed by moderators only.

Acceptance criteria

  • After a batch exhausts all three send attempts (worker + fallbacks) and every log in the batch is failed, an admin-facing error email is sent exactly once for that batch to Site Kit admins (capability-based, e.g., Permissions::MANAGE_OPTIONS), provided the failure reason is sendable
    • Sendable failures are limited to: network errors, permissions revoked, property deleted, and server errors. Failures that indicate email sending itself is broken (e.g., wp_mail_failed, mail transport errors) must not trigger the admin email.
  • If any log in the batch was sent successfully, no admin error email is sent for that batch.
  • The admin email includes the batch context (frequency/date window) and the human-readable failure reason/category drawn from the logged errors
  • Error emails are recorded/logged in a way that prevents duplicate sends for the same batch failure

Implementation Brief

  • [ ] * Add a notifier to the email reporting pipeline (e.g., includes/Core/Email_Reporting/Batch_Error_Notifier.php) that can:
    • Inspect a batch's logs (via Email_Log_Batch_Query) to determine: all logs failed, attempts >= MAX_ATTEMPTS, and no prior "admin-notified" flag (new meta, e.g., Email_Log::META_ADMIN_NOTIFIED).
    • Collect error details from Email_Log::META_ERROR_DETAILS, classify them into the allowed categories (network, permissions revoked, property deleted, server errors) based on WP_Error codes; ignore non-sendable errors (mail transport failures, empty/unknown errors).
    • Build and send an admin email using Google\Site_Kit\Core\Email\Email using the template added in #11852 that includes: subject, frequency/date window, and the categorized reason(s).
    • Mark the batch as notified (set meta on each log or a batch-scoped meta) to avoid duplicate sends.
  • [ ] Wire the notifier into the scheduler flow:
    • In includes/Core/Email_Reporting/Fallback_Task.php, when is_complete( $batch_id ) becomes true, check if the batch ended entirely failed; if so, invoke the notifier. Ensure this runs before early returns so the last fallback can send the admin email.
    • If preferable, add a helper in Email_Log_Batch_Query to return status/error summaries (get_batch_statuses, all_failed_after_max_attempts) to keep Fallback_Task lean.
  • Recipients: resolve admins with the appropriate capability (e.g., Permissions::MANAGE_OPTIONS); dedupe addresses; skip sending if no recipients.
  • Guardrails: if the notifier detects only non-sendable errors (mail failures), do nothing; if sending the admin email fails, do not retry for that batch (already at terminal state).

Test Coverage

  • Notifier unit tests: sends when all logs failed after MAX_ATTEMPTS with sendable error; skips when mixed sent/failed; skips on mail-transport errors; marks notified to prevent duplicates.
  • Fallback task integration: when a batch completes with all failed and notifier is mocked to send, it is invoked; when batch not complete or has sent logs, notifier is not called.

QA Brief

Changelog entry

benbowler avatar Nov 27 '25 16:11 benbowler