Automattic\WooCommerce\Internal\PushNotifications\Services

NotificationRetryHandler::schedule │ public │ WC 10.8.0

Schedules a retry for a failed notification send.

If the maximum number of retries has been reached, logs a permanent failure instead of scheduling another attempt.

Every path that ends without a scheduled retry calls Notification::reset_processing_meta(), including a scheduling call that returns 0. Action Scheduler returns 0 when it is not initialised or when the store rejects the action, and nothing else would then clear the claimed marker that NotificationProcessor::process() checks.

Method of the class: NotificationRetryHandler{}

No Hooks.

Returns

void. Nothing (null).

Usage

$NotificationRetryHandler = new NotificationRetryHandler();
$NotificationRetryHandler->schedule( $notification, ?int $retry_after, $current_attempt ): void;
$notification(Notification) (required)
The notification that failed.
?int $retry_after(required)
.
$current_attempt(int) (required)
The attempt number that just failed (0-based).

Changelog

Since 10.8.0 Introduced.

NotificationRetryHandler::schedule() code WC 11.3.0

public function schedule( Notification $notification, ?int $retry_after, int $current_attempt ): void {
	$next_attempt = max( 0, $current_attempt ) + 1;

	if ( $next_attempt > self::MAX_RETRIES || ! isset( self::BACKOFF_SCHEDULE[ $next_attempt ] ) ) {
		$this->step_logger->log_failure(
			$notification,
			'retry',
			'exhausted',
			'error',
			sprintf(
				'Push notification permanently failed after %d attempts (type=%s, resource_id=%d).',
				$next_attempt,
				$notification->get_type(),
				$notification->get_resource_id()
			),
			array( 'attempt' => $current_attempt )
		);
		$notification->reset_processing_meta();
		return;
	}

	$delay = $retry_after ?? self::BACKOFF_SCHEDULE[ $next_attempt ];

	if ( $delay > self::MAX_RETRY_DELAY ) {
		$this->step_logger->log_failure(
			$notification,
			'retry',
			'delay_too_long',
			'warning',
			sprintf(
				'Push notification dropped: retry delay %ds exceeds maximum %ds (type=%s, resource_id=%d).',
				$delay,
				self::MAX_RETRY_DELAY,
				$notification->get_type(),
				$notification->get_resource_id()
			),
			array(
				'attempt' => $current_attempt,
				'delay'   => $delay,
			)
		);
		$notification->reset_processing_meta();
		return;
	}

	// Action Scheduler dispatches array_values( $args ), so these keys are
	// decorative and the order alone decides which handle_retry() parameter
	// each value lands in.
	$args = array(
		'type'        => $notification->get_type(),
		'resource_id' => $notification->get_resource_id(),
		'attempt'     => $next_attempt,
	);

	$identity_data = $notification->get_identity_data();

	// Appended only when there is any, so orders and reviews keep the exact
	// argument list they had before this field existed and `$unique` still
	// matches a retry scheduled before it. Stock retries do change, so
	// during the deploy both formats can be pending for one product.
	if ( ! empty( $identity_data ) ) {
		$args['extra'] = $identity_data;
	}

	$action_id = as_schedule_single_action(
		time() + $delay,
		self::RETRY_HOOK,
		$args,
		NotificationProcessor::ACTION_SCHEDULER_GROUP,
		true
	);

	if ( ! $action_id ) {
		$this->step_logger->log_failure(
			$notification,
			'retry',
			'schedule_failed',
			'error',
			sprintf(
				'Push notification retry could not be scheduled (type=%s, resource_id=%d, attempt=%d).',
				$notification->get_type(),
				$notification->get_resource_id(),
				$next_attempt
			),
			array( 'attempt' => $current_attempt )
		);
		$notification->reset_processing_meta();
		return;
	}

	$this->step_logger->log_notification_step(
		$notification,
		'retry',
		'scheduled',
		array(
			'attempt'      => $current_attempt,
			'next_attempt' => $next_attempt,
			'delay'        => $delay,
		)
	);
}