When using CLI, run queries without queue

This commit is contained in:
Mike Jolley 2019-03-14 11:46:41 +00:00
parent 520c517410
commit 062d147b59
1 changed files with 36 additions and 24 deletions

View File

@ -1296,6 +1296,12 @@ function wc_update_product_lookup_tables_is_running() {
function wc_update_product_lookup_tables() {
global $wpdb;
$is_cli = defined( 'WP_CLI' ) && WP_CLI;
if ( ! $is_cli ) {
WC_Admin_Notices::add_notice( 'regenerating_lookup_table' );
}
// Make a row per product in lookup table.
$wpdb->query(
"
@ -1308,7 +1314,7 @@ function wc_update_product_lookup_tables() {
"
);
// Queue update events.
// List of column names in the lookup table we need to populate.
$columns = array(
'min_price',
'max_price',
@ -1323,17 +1329,21 @@ function wc_update_product_lookup_tables() {
);
foreach ( $columns as $index => $column ) {
WC()->queue()->schedule_single(
time() + $index,
'wc_update_product_lookup_tables_column',
array(
'column' => $column,
),
'wc_update_product_lookup_tables'
);
if ( $is_cli ) {
wc_update_product_lookup_tables_column( $column );
} else {
WC()->queue()->schedule_single(
time() + $index,
'wc_update_product_lookup_tables_column',
array(
'column' => $column,
),
'wc_update_product_lookup_tables'
);
}
}
// Rating counts are serialised so add them gradually using queue.
// Rating counts are serialised so they have to be unserialised before populating the lookup table.
$rating_count_rows = $wpdb->get_results(
"
SELECT post_id, meta_value FROM {$wpdb->postmeta}
@ -1345,23 +1355,25 @@ function wc_update_product_lookup_tables() {
);
if ( $rating_count_rows ) {
$rating_count_rows = array_chunk( $rating_count_rows, 50 );
$index = 10;
if ( $is_cli ) {
wc_update_product_lookup_tables_rating_count( $rating_count_rows );
} else {
$rating_count_rows = array_chunk( $rating_count_rows, 50 );
$index = count( $columns ) + 1;
foreach ( $rating_count_rows as $rows ) {
WC()->queue()->schedule_single(
time() + $index,
'wc_update_product_lookup_tables_rating_count',
array(
'rows' => $rows,
),
'wc_update_product_lookup_tables'
);
foreach ( $rating_count_rows as $rows ) {
WC()->queue()->schedule_single(
time() + $index,
'wc_update_product_lookup_tables_rating_count',
array(
'rows' => $rows,
),
'wc_update_product_lookup_tables'
);
$index ++;
}
}
}
// Add notice.
WC_Admin_Notices::add_notice( 'regenerating_lookup_table' );
}
/**