-
Notifications
You must be signed in to change notification settings - Fork 336
Closed
Labels
P0High priorityHigh priorityTeam SIssues for Squad 1Issues for Squad 1Type: EnhancementImprovement of an existing featureImprovement of an existing feature
Description
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 has status
email_failedwith_send_attempts>=MAX_ATTEMPTS, an admin-facing error email is sent exactly once for that batch to Site Kit admins (users withPermissions::MANAGE_OPTIONS), provided the failure reason is sendable.- Sendable failures are those whose
category_id(stored inMETA_ERROR_DETAILS>error_data) is notsending_error. This includes:permissions_error- permissions revoked / insufficient access (from Track Module reporting errors within Email Reporting #12110)report_error- API data retrieval failures beyond permissions (from Track Module reporting errors within Email Reporting #12110)null/ absentcategory_id- generic server errors such as invalid user, invalid date range, render failures (from Track Email Sending and generic Server Errors #11861)
- Non-sendable:
category_id===sending_error(wp_mail / mail transport failures from Track Email Sending and generic Server Errors #11861) must not trigger the admin email, as the email delivery mechanism itself is broken.
- Sendable failures are those whose
- If any log in the batch was sent successfully (
email_sentstatus), no admin error email is sent for that batch. - The admin email uses the existing
error-emailtemplate with title and description set based on the error type in PUE Errors spreadsheet. - Error emails are recorded via a new
META_ADMIN_NOTIFIEDpost meta flag on the batch's first log, preventing duplicate sends for the same batch failure.
Implementation Brief
Backend
-
Create file
includes/Core/Email_Reporting/Batch_Error_Notifier.php- Constructor accepts
Email_Log_Batch_Query,Email(sender), andContext(for site URL/domain). maybe_notify( string $batch_id, string $frequency )method:- Query all post IDs in the batch via
Email_Log_Batch_Query. - Return early if any log has
email_sentstatus, or any failed log has_send_attempts<MAX_ATTEMPTSusingis_batch_all_failed. - Return early if
META_ADMIN_NOTIFIEDis already set on the first log in the batch (prevents duplicate sends) usingis_batch_admin_notified. - Extract
META_ERROR_DETAILSfrom the first failed log, decode JSON, and readcategory_idfromerror_data.[first_error_code].category_id. - Return early (do nothing) if
category_id === 'sending_error'- the mail system is broken, so admin email cannot be sent. - Map
category_idto a human-readable failure description viaContent_Map::get_body()using category-keyed title copy built from copy below in the PUE Errors spreadsheet. - Resolve admin recipients:
get_users( array( 'capability' => PERMISSIONS::MANAGE_OPTIONS ) ); dedupe email addresses; return early if none. - Extract
META_REPORT_REFERENCE_DATESfrom the first log viaEmail_Log::get_date_range_from_log()andMETA_REPORT_FREQUENCYfor the batch context. - Build the template data array matching the
error-emailtemplate contract:subject,preheader,site.domain,site.url,title,body(array of paragraphs),primary_call_to_action,footer. - Create an
Email_Template_Renderer( null )(no sections map needed) and callrender( 'error-email', $template_data )for HTML andrender_text( 'error-email', $template_data )for plain text. - Send via
Email::send()to all resolved admin addresses with both HTML and plain text content. - On success or failure, set
META_ADMIN_NOTIFIEDto1usingmark_batch_admin_notified.
- Query all post IDs in the batch via
- Constructor accepts
-
Update file
includes/Core/Email_Reporting/Email_Log.php- Add constant
META_ADMIN_NOTIFIED = '_admin_notified'. - Register the new post meta in
register_post_meta()withtype => 'string',single => true, using the existingmeta_auth_callback.
- Add constant
-
Update file
includes/Core/Email_Reporting/Email_Log_Batch_Query.php- Add
is_batch_all_failed( string $batch_id ): bool- returnstrueonly when every log in the batch hasemail_failedstatus and_send_attempts >= MAX_ATTEMPTS, and no log hasemail_sentstatus. - Add
is_batch_admin_notified( string $batch_id ): bool- checks ifMETA_ADMIN_NOTIFIEDis set on the first log in the batch. - Add
mark_batch_admin_notified( string $batch_id ): void- setsMETA_ADMIN_NOTIFIEDon the first log in the batch.
- Add
-
Update file
includes/Core/Email_Reporting/Fallback_Task.php- Accept
Batch_Error_Notifieras a new constructor dependency. - In
handle_fallback_action(), when$this->batch_query->is_complete( $batch_id )returnstrue, call$this->batch_error_notifier->maybe_notify( $batch_id, $frequency )before the early return.
- Accept
-
Update file
includes/Core/Email_Reporting/Worker_Task.php- Accept
Batch_Error_Notifieras a new constructor dependency. - In
handle_callback_action(), afterprocess_pending_logs()completes, check$this->batch_query->is_complete( $batch_id )and if true, call$this->batch_error_notifier->maybe_notify( $batch_id, $frequency ).
- Accept
-
Update file
includes/Core/Email_Reporting/Email_Reporting.php- Instantiate
Batch_Error_Notifierwith$this->email_log_batch_query,$email_sender, and$this->context. - Pass the notifier instance into both
Worker_TaskandFallback_Taskconstructors.
- Instantiate
-
Update file
includes/Core/Email_Reporting/Content_Map.php- Add relevant content key for title and body sections for each emailable variant in PUE Errors spreadsheet.
Test Coverage
- Create
tests/phpunit/integration/Core/Email_Reporting/Batch_Error_NotifierTest.php:- Test: sends admin email when all logs failed after
MAX_ATTEMPTSwith a sendablecategory_id(permissions_error,report_error, or absent). - Test: does not send when
category_id === 'sending_error'(non-sendable). - Test: does not send when any log in the batch has
email_sentstatus (mixed batch). - Test: does not send when
META_ADMIN_NOTIFIEDis already set (duplicate prevention). - Test: sets
META_ADMIN_NOTIFIEDafter sending. - Test: correctly resolves admin recipients with
MANAGE_OPTIONScapability. - Test: does nothing if no eligible admin recipients exist.
- Assert email sentwith correct recipients, subject, and body content matching the error category.
- Test: sends admin email when all logs failed after
- Update
tests/phpunit/integration/Core/Email_Reporting/Fallback_TaskTest.php:- Test: when
is_complete()returns true,maybe_notify()on the notifier is called. - Test: when batch is not complete, notifier is not called.
- Test: when
- Update
tests/phpunit/integration/Core/Email_Reporting/Worker_TaskTest.php:- Test: after processing, if batch becomes complete,
maybe_notify()is called. - Test: if batch still has pending logs, notifier is not called.
- Test: after processing, if batch becomes complete,
QA Brief
- Install the new tester plugin (@benbowler will share in Slack)
- Test each error type in the PUE Errors spreadsheet and confirm the emails are received correctly by all authenticated admins and the copy and links match the spreadsheet.
Changelog entry
- Send emails to site admins when entire batches of emails fail to send.
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
P0High priorityHigh priorityTeam SIssues for Squad 1Issues for Squad 1Type: EnhancementImprovement of an existing featureImprovement of an existing feature