Automattic\WooCommerce\Internal\BatchProcessing

BatchProcessingController::remove_or_retry_failed_processors │ private │ WC 9.1.0

Hooked onto 'shutdown'. This cleanup routine checks enqueued processors and whether they are scheduled or not to either re-eschedule them or remove them from the queue. This prevents stale states where Action Scheduler won't schedule any more attempts but we still report the processor as enqueued.

Метод класса: BatchProcessingController{}

Хуков нет.

Возвращает

null. Ничего (null).

Использование

// private - только в коде основоного (родительского) класса
$result = $this->remove_or_retry_failed_processors(): void;

Список изменений

С версии 9.1.0 Введена.

Код BatchProcessingController::remove_or_retry_failed_processors() WC 11.3.0

private function remove_or_retry_failed_processors(): void {
	if ( ! did_action( 'wp_loaded' ) ) {
		return;
	}

	$last_error = error_get_last();
	if ( ! is_null( $last_error ) && in_array( $last_error['type'], array( E_ERROR, E_PARSE, E_CORE_ERROR, E_COMPILE_ERROR, E_USER_ERROR, E_RECOVERABLE_ERROR ), true ) ) {
		return;
	}

	// Everything below reads "not scheduled" as grounds for recording a failure against a processor
	// and eventually dropping it from the queue. Action Scheduler being unloaded also reads as
	// "not scheduled", so bail rather than dismantle the queue over a missing dependency.
	if ( ! ActionSchedulerUtil::can_check_scheduled_actions() ) {
		return;
	}

	// Normalize corrupted option values before passing processor names to is_scheduled().
	$enqueued_processors = $this->sanitize_processor_list( $this->get_enqueued_processors() );
	if ( empty( $enqueued_processors ) ) {
		return;
	}

	$watchdog_scheduled = $this->run_scheduler_lookup( fn() => ActionSchedulerUtil::has_scheduled_action( self::WATCHDOG_ACTION_NAME ) );
	if ( false !== $watchdog_scheduled ) {
		return;
	}

	$unscheduled_processors = array();
	foreach ( $enqueued_processors as $processor ) {
		$is_scheduled = $this->run_scheduler_lookup( fn() => $this->is_scheduled( $processor ) );
		// A failed lookup is not evidence of a failed processor. Finish all checks before changing state.
		if ( null === $is_scheduled ) {
			return;
		}
		if ( ! $is_scheduled ) {
			$unscheduled_processors[] = $processor;
		}
	}

	foreach ( $unscheduled_processors as $processor ) {
		try {
			$instance = $this->get_processor_instance( $processor );
		} catch ( \Exception $e ) {
			continue;
		}

		$exception = new \Exception( 'Processor is enqueued but not scheduled. Background job was probably killed or marked as failed. Reattempting execution.' );
		$this->update_processor_state( $instance, 0, $exception );
		$this->log_error( $exception, $instance, array() );

		if ( $this->is_consistently_failing( $instance ) ) {
			$this->log_consistent_failure( $instance, $this->get_process_details( $instance ) );
			$this->remove_processor( $processor );
		} else {
			$this->schedule_batch_processing( $processor, true );
		}
	}
}