ontainer();
$processor = $container->has( $processor_class_name ) ? $container->get( $processor_class_name ) : null;
/**
* Filters the instance of a processor for a given class name.
*
* @param object|null $processor The processor instance given by the dependency injection container, or null if none was obtained.
* @param string $processor_class_name The full class name of the processor.
* @return BatchProcessorInterface|null The actual processor instance to use, or null if none could be retrieved.
*
* @since 6.8.0.
*/
$processor = apply_filters( 'woocommerce_get_batch_processor', $processor, $processor_class_name );
if ( ! isset( $processor ) && class_exists( $processor_class_name ) ) {
// This is a fallback for when the batch processor is not registered in the container.
$processor = new $processor_class_name();
}
if ( ! is_a( $processor, BatchProcessorInterface::class ) ) {
throw new \Exception( "Unable to initialize batch processor instance for $processor_class_name" );
}
return $processor;
}
/**
* Helper method to get list of all the enqueued processors.
*
* @return array List (of string) of the class names of the enqueued processors.
*/
public function get_enqueued_processors(): array {
$enqueued_processors = get_option( self::ENQUEUED_PROCESSORS_OPTION_NAME, array() );
if ( ! is_array( $enqueued_processors ) ) {
$this->logger->error( 'Could not fetch list of processors. Clearing up queue.', array( 'source' => 'batch-processing' ) );
delete_option( self::ENQUEUED_PROCESSORS_OPTION_NAME );
$enqueued_processors = array();
}
return $enqueued_processors;
}
/**
* Dequeue a processor once it has no more items pending processing.
*
* @param string $processor_class_name Full processor class name.
*/
private function dequeue_processor( string $processor_class_name ): void {
$pending_processes = $this->get_enqueued_processors();
if ( in_array( $processor_class_name, $pending_processes, true ) ) {
$this->clear_processor_state( $processor_class_name );
$pending_processes = array_diff( $pending_processes, array( $processor_class_name ) );
$this->set_enqueued_processors( $pending_processes );
}
}
/**
* Helper method to set the enqueued processor class names.
*
* @param array $processors List of full processor class names.
*/
private function set_enqueued_processors( array $processors ): void {
update_option( self::ENQUEUED_PROCESSORS_OPTION_NAME, $processors, false );
}
/**
* Check if a particular processor is enqueued.
*
* @param string $processor_class_name Fully qualified class name of the processor.
*
* @return bool True if the processor is enqueued.
*/
public function is_enqueued( string $processor_class_name ): bool {
return in_array( $processor_class_name, $this->get_enqueued_processors(), true );
}
/**
* Dequeue and de-schedule a processor instance so that it won't be processed anymore.
*
* @param string $processor_class_name Fully qualified class name of the processor.
* @return bool True if the processor has been dequeued, false if the processor wasn't enqueued (so nothing has been done).
*/
public function remove_processor( string $processor_class_name ): bool {
$enqueued_processors = $this->get_enqueued_processors();
if ( ! in_array( $processor_class_name, $enqueued_processors, true ) ) {
return false;
}
$enqueued_processors = array_diff( $enqueued_processors, array( $processor_class_name ) );
if ( empty( $enqueued_processors ) ) {
$this->force_clear_all_processes();
} else {
update_option( self::ENQUEUED_PROCESSORS_OPTION_NAME, $enqueued_processors, false );
as_unschedule_all_actions( self::PROCESS_SINGLE_BATCH_ACTION_NAME, array( $processor_class_name ) );
$this->clear_processor_state( $processor_class_name );
}
return true;
}
/**
* Dequeues and de-schedules all the processors.
*/
public function force_clear_all_processes(): void {
as_unschedule_all_actions( self::PROCESS_SINGLE_BATCH_ACTION_NAME );
as_unschedule_all_actions( self::WATCHDOG_ACTION_NAME );
foreach ( $this->get_enqueued_processors() as $processor ) {
$this->clear_processor_state( $processor );
}
update_option( self::ENQUEUED_PROCESSORS_OPTION_NAME, array(), false );
}
/**
* Log an error that happened while processing a batch.
*
* @param \Exception $error Exception object to log.
* @param BatchProcessorInterface $batch_processor Batch processor instance.
* @param array $batch Batch that was being processed.
*/
protected function log_error( \Exception $error, BatchProcessorInterface $batch_processor, array $batch ): void {
$error_message = "Error processing batch for {$batch_processor->get_name()}: {$error->getMessage()}";
$error_context = array(
'exception' => $error,
'source' => 'batch-processing',
);
// Log only first and last, as the entire batch may be too big.
if ( count( $batch ) > 0 ) {
$error_context = array_merge(
$error_context,
array(
'batch_start' => $batch[0],
'batch_end' => end( $batch ),
)
);
}
/**
* Filters the error message for a batch processing.
*
* @param string $error_message The error message that will be logged.
* @param \Exception $error The exception that was thrown by the processor.
* @param BatchProcessorInterface $batch_processor The processor that threw the exception.
* @param array $batch The batch that was being processed.
* @param array $error_context Context to be passed to the logging function.
* @return string The actual error message that will be logged.
*
* @since 6.8.0
*/
$error_message = apply_filters( 'wc_batch_processing_log_message', $error_message, $error, $batch_processor, $batch, $error_context );
$this->logger->error( $error_message, $error_context );
}
/**
* Determines whether a given processor is consistently failing based on how many recent consecutive failures it has had.
*
* @since 9.1.0
*
* @param BatchProcessorInterface $batch_processor The processor that we want to check.
* @return boolean TRUE if processor is consistently failing. FALSE otherwise.
*/
private function is_consistently_failing( BatchProcessorInterface $batch_processor ): bool {
$process_details = $this->get_process_details( $batch_processor );
$max_attempts = absint(
/**
* Controls the failure threshold for batch processors. That is, the number of times we'll attempt to
* process a batch that has resulted in a failure. Once above this threshold, the processor won't be
* re-scheduled and will be removed from the queue.
*
* @since 9.1.0
*
* @param int $failure_threshold Maximum number of times for the processor to try processing a given batch.
* @param BatchProcessorInterface $batch_processor The processor instance.
* @param array $process_details Array with batch processor state.
*/
apply_filters(
'wc_batch_processing_max_attempts',
self::FAILING_PROCESS_MAX_ATTEMPTS_DEFAULT,
$batch_processor,
$process_details
)
);
return absint( $process_details['recent_failures'] ?? 0 ) >= max( $max_attempts, 1 );
}
/**
* Creates log entry with details about a batch processor that is consistently failing.
*
* @since 9.1.0
*
* @param BatchProcessorInterface $batch_processor The batch processor instance.
* @param array $process_details Failing process details.
*/
private function log_consistent_failure( BatchProcessorInterface $batch_processor, array $process_details ): void {
$this->logger->error(
"Batch processor {$batch_processor->get_name()} appears to be failing consistently: {$process_details['recent_failures']} unsuccessful attempt(s). No further attempts will be made.",
array(
'source' => 'batch-processing',
'failures' => $process_details['recent_failures'],
'first_failure' => $process_details['batch_first_failure'],
'last_failure' => $process_details['batch_last_failure'],
)
);
}
/**
* 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.
*
* @since 9.1.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;
}
// The most efficient way to check for an existing action is to use `as_has_scheduled_action`, but in unusual
// cases where another plugin has loaded a very old version of Action Scheduler, it may not be available to us.
$has_scheduled_action = function_exists( 'as_has_scheduled_action') ? 'as_has_scheduled_action' : 'as_next_scheduled_action';
if ( call_user_func( $has_scheduled_action, self::WATCHDOG_ACTION_NAME ) ) {
return;
}
$enqueued_processors = $this->get_enqueued_processors();
$unscheduled_processors = array_diff( $enqueued_processors, array_filter( $enqueued_processors, array( $this, 'is_scheduled' ) ) );
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 );
}
}
}
}
Fatal error: Uncaught Automattic\WooCommerce\Internal\DependencyManagement\ContainerException: Attempt to get an instance of class 'Automattic\WooCommerce\Internal\BatchProcessing\BatchProcessingController', which doesn't exist. in /home/htzanetatos/public_html/wp-content/plugins/woocommerce/src/Internal/DependencyManagement/RuntimeContainer.php:102
Stack trace:
#0 /home/htzanetatos/public_html/wp-content/plugins/woocommerce/src/Internal/DependencyManagement/RuntimeContainer.php(167): Automattic\WooCommerce\Internal\DependencyManagement\RuntimeContainer->get_core('Automattic\\WooC...', Array)
#1 [internal function]: Automattic\WooCommerce\Internal\DependencyManagement\RuntimeContainer->Automattic\WooCommerce\Internal\DependencyManagement\{closure}(Object(ReflectionParameter))
#2 /home/htzanetatos/public_html/wp-content/plugins/woocommerce/src/Internal/DependencyManagement/RuntimeContainer.php(156): array_map(Object(Closure), Array)
#3 /home/htzanetatos/public_html/wp-content/plugins/woocommerce/src/Internal/DependencyMana in /home/htzanetatos/public_html/wp-content/plugins/woocommerce/src/Internal/DependencyManagement/RuntimeContainer.php on line 102