Automattic\WooCommerce\Internal\PushNotifications\Services

PendingNotificationStore::schedule_safety_net │ private │ WC 10.7.0

Schedules an ActionScheduler safety net job for the notification.

If the shutdown hook never fires (OOM, SIGKILL, etc.), this job guarantees the notification is still processed.

Method of the class: PendingNotificationStore{}

No Hooks.

Returns

string. scheduled, already_scheduled, or schedule_failed when Action Scheduler refused the job.

Usage

// private - for code of main (parent) class only
$result = $this->schedule_safety_net( $notification ): string;
$notification(Notification) (required)
The notification to schedule.

Changelog

Since 10.7.0 Introduced.

PendingNotificationStore::schedule_safety_net() code WC 11.3.0

private function schedule_safety_net( Notification $notification ): string {
	// Canonical, identity-keyed args shared with NotificationProcessor::cancel_safety_net().
	// Action Scheduler matches stored args by exact equality, so both sides must derive
	// them from the same place; see Notification::get_safety_net_args().
	$args = $notification->get_safety_net_args();

	if ( ActionSchedulerUtil::has_scheduled_action( NotificationProcessor::SAFETY_NET_HOOK, $args, NotificationProcessor::ACTION_SCHEDULER_GROUP ) ) {
		return 'already_scheduled';
	}

	// Action Scheduler returns 0 rather than throwing when it cannot store
	// the job, and a notification whose fallback was never scheduled has
	// nothing to catch it if the loopback does not arrive.
	$action_id = as_schedule_single_action(
		time() + NotificationProcessor::SAFETY_NET_DELAY,
		NotificationProcessor::SAFETY_NET_HOOK,
		$args,
		NotificationProcessor::ACTION_SCHEDULER_GROUP,
		true
	);

	return $action_id > 0 ? 'scheduled' : 'schedule_failed';
}