1 resize per batch

This commit is contained in:
Mike Jolley 2018-01-26 14:31:27 +00:00
parent 06e84b7af3
commit 4edd65c124
2 changed files with 42 additions and 29 deletions

View File

@ -30,22 +30,19 @@ class WC_Regenerate_Images_Request extends WP_Background_Process {
$this->prefix = 'wp_' . get_current_blog_id();
$this->action = 'wc_regenerate_images';
// This is needed to prevent timeouts due to threading. See https://core.trac.wordpress.org/ticket/36534.
@putenv( 'MAGICK_THREAD_LIMIT=1' ); // @codingStandardsIgnoreLine.
parent::__construct();
}
/**
* Fires when the job should start
* Limit each task ran per batch to 1 for image regen.
*
* @return void
* @return bool
*/
public function dispatch() {
$log = wc_get_logger();
$log->info( __( 'Starting product image regeneration job.', 'woocommerce' ),
array(
'source' => 'wc-image-regeneration',
)
);
parent::dispatch();
protected function batch_limit_exceeded() {
return true;
}
/**
@ -90,9 +87,6 @@ class WC_Regenerate_Images_Request extends WP_Background_Process {
include ABSPATH . 'wp-admin/includes/image.php';
}
// This is needed to prevent timeouts due to threading. See https://core.trac.wordpress.org/ticket/36534.
@putenv( 'MAGICK_THREAD_LIMIT=1' ); // @codingStandardsIgnoreLine.
$log = wc_get_logger();
// translators: %s: ID of the attachment.

View File

@ -120,12 +120,27 @@ abstract class WP_Background_Process extends WP_Async_Request {
/**
* Delete queue
*
* @param string $key Key.
*
* @param string $key Key. Leave blank to delete all batches.
* @return $this
*/
public function delete( $key ) {
delete_site_option( $key );
public function delete( $key = '' ) {
if ( $key ) {
delete_site_option( $key );
} else {
global $wpdb;
$table = $wpdb->options;
$column = 'option_name';
if ( is_multisite() ) {
$table = $wpdb->sitemeta;
$column = 'meta_key';
}
$key = $wpdb->esc_like( $this->identifier . '_batch_' ) . '%';
$wpdb->query( $wpdb->prepare( "DELETE FROM {$table} WHERE {$column} LIKE %s", $key ) ); // @codingStandardsIgnoreLine.
}
return $this;
}
@ -272,6 +287,15 @@ abstract class WP_Background_Process extends WP_Async_Request {
return $batch;
}
/**
* See if the batch limit has been exceeded.
*
* @return bool
*/
protected function batch_limit_exceeded() {
return $this->time_exceeded() || $this->memory_exceeded();
}
/**
* Handle
*
@ -293,20 +317,19 @@ abstract class WP_Background_Process extends WP_Async_Request {
unset( $batch->data[ $key ] );
}
// Update batch so this task is not repeated.
$this->update( $batch->key, $batch->data );
if ( $this->time_exceeded() || $this->memory_exceeded() ) {
if ( $this->batch_limit_exceeded() ) {
// Batch limits reached.
break;
}
}
// Delete current batch if complete.
if ( empty( $batch->data ) ) {
// Update or delete current batch.
if ( ! empty( $batch->data ) ) {
$this->update( $batch->key, $batch->data );
} else {
$this->delete( $batch->key );
}
} while ( ! $this->time_exceeded() && ! $this->memory_exceeded() && ! $this->is_queue_empty() );
} while ( ! $this->batch_limit_exceeded() && ! $this->is_queue_empty() );
$this->unlock_process();
@ -463,13 +486,9 @@ abstract class WP_Background_Process extends WP_Async_Request {
*/
public function cancel_process() {
if ( ! $this->is_queue_empty() ) {
$batch = $this->get_batch();
$this->delete( $batch->key );
$this->delete();
wp_clear_scheduled_hook( $this->cron_hook_identifier );
}
}
/**