Merge pull request #23484 from woocommerce/update/marketplace-queue-handling

Marketplace queue handling - avoid recurring event
This commit is contained in:
Mike Jolley 2019-04-24 17:00:40 +01:00 committed by GitHub
commit 036a91d20c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 8 deletions

View File

@ -188,12 +188,22 @@ class WC_Marketplace_Suggestions {
}
/**
* Pull suggestion data from remote endpoint & cache in a transient.
* Pull suggestion data from options. This is retrieved from a remote endpoint.
*
* @return array of json API data
*/
public static function get_suggestions_api_data() {
$data = get_option( 'woocommerce_marketplace_suggestions', array() );
// If the options have never been updated, or were updated over a week ago, queue update.
if ( empty( $data['updated'] ) || ( time() - WEEK_IN_SECONDS ) > $data['updated'] ) {
$next = WC()->queue()->get_next( 'woocommerce_update_marketplace_suggestions' );
if ( ! $next ) {
WC()->queue()->cancel_all( 'woocommerce_update_marketplace_suggestions' );
WC()->queue()->schedule_single( time(), 'woocommerce_update_marketplace_suggestions' );
}
}
return ! empty( $data['suggestions'] ) ? $data['suggestions'] : array();
}
}

View File

@ -26,12 +26,6 @@ class WC_Marketplace_Updater {
* Schedule events and hook appropriate actions.
*/
public static function init() {
$queue = WC()->queue();
$next = $queue->get_next( 'woocommerce_update_marketplace_suggestions' );
if ( ! $next ) {
$queue->schedule_recurring( time(), WEEK_IN_SECONDS, 'woocommerce_update_marketplace_suggestions' );
}
add_action( 'woocommerce_update_marketplace_suggestions', array( __CLASS__, 'update_marketplace_suggestions' ) );
}
@ -78,7 +72,7 @@ class WC_Marketplace_Updater {
* Re-schedules the job earlier than the main weekly one.
*/
public static function retry() {
WC()->queue()->cancel( 'woocommerce_update_marketplace_suggestions' );
WC()->queue()->cancel_all( 'woocommerce_update_marketplace_suggestions' );
WC()->queue()->schedule_single( time() + DAY_IN_SECONDS, 'woocommerce_update_marketplace_suggestions' );
}
}