Automattic\WooCommerce\Internal\PushNotifications\Services

NotificationRetryHandler::schedulepublicWC 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.

Method of the class: NotificationRetryHandler{}

No Hooks.

Returns

null. 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 10.9.4

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 ] ) ) {
		wc_get_logger()->error(
			sprintf(
				'Push notification permanently failed after %d attempts (type=%s, resource_id=%d).',
				$next_attempt,
				$notification->get_type(),
				$notification->get_resource_id()
			),
			array( 'source' => PushNotifications::FEATURE_NAME )
		);
		return;
	}

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

	if ( $delay > self::MAX_RETRY_DELAY ) {
		wc_get_logger()->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( 'source' => PushNotifications::FEATURE_NAME )
		);
		return;
	}

	as_schedule_single_action(
		time() + $delay,
		self::RETRY_HOOK,
		array(
			'type'        => $notification->get_type(),
			'resource_id' => $notification->get_resource_id(),
			'attempt'     => $next_attempt,
		),
		NotificationProcessor::ACTION_SCHEDULER_GROUP,
		true
	);
}