NotificationPreferencesService.php
4 weeks ago
NotificationProcessor.php
4 weeks ago
NotificationRetryHandler.php
4 weeks ago
PendingNotificationStore.php
4 weeks ago
NotificationRetryHandler.php
171 lines
| 1 | <?php |
| 2 | |
| 3 | declare( strict_types = 1 ); |
| 4 | |
| 5 | namespace Automattic\WooCommerce\Internal\PushNotifications\Services; |
| 6 | |
| 7 | defined( 'ABSPATH' ) || exit; |
| 8 | |
| 9 | use Automattic\WooCommerce\Internal\PushNotifications\Notifications\Notification; |
| 10 | use Automattic\WooCommerce\Internal\PushNotifications\PushNotifications; |
| 11 | use Exception; |
| 12 | |
| 13 | /** |
| 14 | * Handles retry scheduling for failed WPCOM push notification sends. |
| 15 | * |
| 16 | * Uses ActionScheduler to schedule retries with exponential backoff. |
| 17 | * After all retry attempts are exhausted, logs a permanent failure. |
| 18 | * |
| 19 | * @since 10.8.0 |
| 20 | */ |
| 21 | class NotificationRetryHandler { |
| 22 | |
| 23 | /** |
| 24 | * ActionScheduler hook for retry jobs. |
| 25 | */ |
| 26 | const RETRY_HOOK = 'wc_push_notification_retry'; |
| 27 | |
| 28 | /** |
| 29 | * Maximum number of retries before giving up (5 total attempts including |
| 30 | * the initial send). |
| 31 | */ |
| 32 | const MAX_RETRIES = 4; |
| 33 | |
| 34 | /** |
| 35 | * Maximum retry delay in seconds (24 hours). If WPCOM requests a |
| 36 | * Retry-After longer than this the notification is dropped — a push |
| 37 | * notification arriving days late would be more confusing than helpful. |
| 38 | */ |
| 39 | const MAX_RETRY_DELAY = 86400; |
| 40 | |
| 41 | /** |
| 42 | * Backoff delays in seconds, indexed by attempt number (1-based). |
| 43 | * |
| 44 | * Attempt 1: 60s (1 minute) |
| 45 | * Attempt 2: 300s (5 minutes) |
| 46 | * Attempt 3: 900s (15 minutes) |
| 47 | * Attempt 4: 3600s (60 minutes) |
| 48 | * |
| 49 | * @var array<int, int> |
| 50 | */ |
| 51 | const BACKOFF_SCHEDULE = array( |
| 52 | 1 => 60, |
| 53 | 2 => 300, |
| 54 | 3 => 900, |
| 55 | 4 => 3600, |
| 56 | ); |
| 57 | |
| 58 | /** |
| 59 | * Registers the ActionScheduler hook for retry jobs. |
| 60 | * |
| 61 | * @return void |
| 62 | * |
| 63 | * @since 10.8.0 |
| 64 | */ |
| 65 | public function register(): void { |
| 66 | add_action( self::RETRY_HOOK, array( $this, 'handle_retry' ), 10, 3 ); |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Schedules a retry for a failed notification send. |
| 71 | * |
| 72 | * If the maximum number of retries has been reached, logs a permanent |
| 73 | * failure instead of scheduling another attempt. |
| 74 | * |
| 75 | * @param Notification $notification The notification that failed. |
| 76 | * @param int|null $retry_after Optional Retry-After value from WPCOM (seconds). |
| 77 | * @param int $current_attempt The attempt number that just failed (0-based). |
| 78 | * @return void |
| 79 | * |
| 80 | * @since 10.8.0 |
| 81 | */ |
| 82 | public function schedule( Notification $notification, ?int $retry_after, int $current_attempt ): void { |
| 83 | $next_attempt = max( 0, $current_attempt ) + 1; |
| 84 | |
| 85 | if ( $next_attempt > self::MAX_RETRIES || ! isset( self::BACKOFF_SCHEDULE[ $next_attempt ] ) ) { |
| 86 | wc_get_logger()->error( |
| 87 | sprintf( |
| 88 | 'Push notification permanently failed after %d attempts (type=%s, resource_id=%d).', |
| 89 | $next_attempt, |
| 90 | $notification->get_type(), |
| 91 | $notification->get_resource_id() |
| 92 | ), |
| 93 | array( 'source' => PushNotifications::FEATURE_NAME ) |
| 94 | ); |
| 95 | return; |
| 96 | } |
| 97 | |
| 98 | $delay = $retry_after ?? self::BACKOFF_SCHEDULE[ $next_attempt ]; |
| 99 | |
| 100 | if ( $delay > self::MAX_RETRY_DELAY ) { |
| 101 | wc_get_logger()->warning( |
| 102 | sprintf( |
| 103 | 'Push notification dropped: retry delay %ds exceeds maximum %ds (type=%s, resource_id=%d).', |
| 104 | $delay, |
| 105 | self::MAX_RETRY_DELAY, |
| 106 | $notification->get_type(), |
| 107 | $notification->get_resource_id() |
| 108 | ), |
| 109 | array( 'source' => PushNotifications::FEATURE_NAME ) |
| 110 | ); |
| 111 | return; |
| 112 | } |
| 113 | |
| 114 | as_schedule_single_action( |
| 115 | time() + $delay, |
| 116 | self::RETRY_HOOK, |
| 117 | array( |
| 118 | 'type' => $notification->get_type(), |
| 119 | 'resource_id' => $notification->get_resource_id(), |
| 120 | 'attempt' => $next_attempt, |
| 121 | ), |
| 122 | NotificationProcessor::ACTION_SCHEDULER_GROUP, |
| 123 | true |
| 124 | ); |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * ActionScheduler callback for retry jobs. |
| 129 | * |
| 130 | * Reconstructs the notification from the stored type and resource ID, |
| 131 | * then delegates to the processor with is_retry=true. |
| 132 | * |
| 133 | * @param string $type The notification type. |
| 134 | * @param int $resource_id The resource ID. |
| 135 | * @param int $attempt The current retry attempt number (1-based). |
| 136 | * @return void |
| 137 | * |
| 138 | * @since 10.8.0 |
| 139 | */ |
| 140 | public function handle_retry( string $type, int $resource_id, int $attempt ): void { |
| 141 | try { |
| 142 | $notification = Notification::from_array( |
| 143 | array( |
| 144 | 'type' => $type, |
| 145 | 'resource_id' => $resource_id, |
| 146 | ) |
| 147 | ); |
| 148 | } catch ( Exception $e ) { |
| 149 | wc_get_logger()->error( |
| 150 | sprintf( 'Retry failed: %s', $e->getMessage() ), |
| 151 | array( 'source' => PushNotifications::FEATURE_NAME ) |
| 152 | ); |
| 153 | return; |
| 154 | } |
| 155 | |
| 156 | try { |
| 157 | wc_get_container()->get( NotificationProcessor::class )->process( |
| 158 | $notification, |
| 159 | true, |
| 160 | $attempt |
| 161 | ); |
| 162 | } catch ( Exception $e ) { |
| 163 | wc_get_logger()->error( |
| 164 | sprintf( 'Retry failed: %s', $e->getMessage() ), |
| 165 | array( 'source' => PushNotifications::FEATURE_NAME ) |
| 166 | ); |
| 167 | $this->schedule( $notification, null, $attempt ); |
| 168 | } |
| 169 | } |
| 170 | } |
| 171 |