Offload remote inbox notifications engine run using action-scheduler (https://github.com/woocommerce/woocommerce-admin/pull/6995)

* Offload remote inbox notifications engine run using action-scheduler

* release note

* fix changelog

* Use init hook to initialize async AS action

* Drop product count query workaround
This commit is contained in:
Bec Scott 2021-05-27 10:55:37 +10:00 committed by GitHub
parent 18473767eb
commit 663ab3d27e
4 changed files with 46 additions and 19 deletions

View File

@ -102,6 +102,7 @@ Release and roadmap notes are available on the [WooCommerce Developers Blog](htt
- Dev: Add payment method selector to onboarding store #6921
- Dev: Add disabled prop to SelectControl #6902
- Dev: Add filter variation to tracks data in products analytics. #6913
- Dev: Offload remote inbox notifications engine run using action-scheduler. #6995
- Dev: Add source param support for notes query. #6979
- Dev: Remove the use of Dashicons and replace with @wordpress/icons or gridicons. #7020
- Enhancement: Add expand/collapse to extendable task list. #6910

View File

@ -40,14 +40,10 @@ class ProductCountRuleProcessor implements RuleProcessorInterface {
* @return bool The result of the operation.
*/
public function process( $rule, $stored_state ) {
$products = $this->product_query->get_products();
$new_product_count = property_exists( $stored_state, 'new_product_count' )
? $stored_state->new_product_count
: 0;
$count = $products->total + $new_product_count;
$products = $this->product_query->get_products();
return ComparisonOperation::compare(
$count,
$products->total,
$rule->value,
$rule->operation
);

View File

@ -25,6 +25,9 @@ class RemoteInboxNotificationsEngine {
* Initialize the engine.
*/
public static function init() {
// Init things that need to happen before admin_init.
add_action( 'init', array( __CLASS__, 'on_init' ), 0, 0 );
// Continue init via admin_init.
add_action( 'admin_init', array( __CLASS__, 'on_admin_init' ) );
@ -73,12 +76,21 @@ class RemoteInboxNotificationsEngine {
add_action( 'activated_plugin', array( __CLASS__, 'run' ) );
add_action( 'deactivated_plugin', array( __CLASS__, 'run_on_deactivated_plugin' ), 10, 1 );
StoredStateSetupForProducts::init();
StoredStateSetupForProducts::admin_init();
// Pre-fetch stored state so it has the correct initial values.
self::get_stored_state();
}
/**
* An init hook is used here so that StoredStateSetupForProducts can set
* up a hook that gets triggered by action-scheduler - this is needed
* because the admin_init hook doesn't get triggered by WP Cron.
*/
public static function on_init() {
StoredStateSetupForProducts::init();
}
/**
* Go through the specs and run them.
*/

View File

@ -14,14 +14,33 @@ use \Automattic\WooCommerce\Admin\RemoteInboxNotifications\SpecRunner;
* Handles stored state setup for products.
*/
class StoredStateSetupForProducts {
const ASYNC_RUN_REMOTE_NOTIFICATIONS_ACTION_NAME =
'woocommerce_admin/stored_state_setup_for_products/async/run_remote_notifications';
/**
* Initialize the class
* Initialize the class via the admin_init hook.
*/
public static function init() {
public static function admin_init() {
add_action( 'product_page_product_importer', array( __CLASS__, 'run_on_product_importer' ) );
add_action( 'transition_post_status', array( __CLASS__, 'run_on_transition_post_status' ), 10, 3 );
}
/**
* Initialize the class via the init hook.
*/
public static function init() {
add_action( self::ASYNC_RUN_REMOTE_NOTIFICATIONS_ACTION_NAME, array( __CLASS__, 'run_remote_notifications' ) );
}
/**
* Run the remote notifications engine. This is triggered by
* action-scheduler after a product is added. It also cleans up from
* setting the product count increment.
*/
public static function run_remote_notifications() {
RemoteInboxNotificationsEngine::run();
}
/**
* Set initial stored state values.
*
@ -74,7 +93,7 @@ class StoredStateSetupForProducts {
$stored_state->there_are_now_products = true;
RemoteInboxNotificationsEngine::update_stored_state( $stored_state );
RemoteInboxNotificationsEngine::run();
self::enqueue_async_run_remote_notifications();
}
/**
@ -96,17 +115,16 @@ class StoredStateSetupForProducts {
$stored_state = RemoteInboxNotificationsEngine::get_stored_state();
$stored_state->there_are_now_products = true;
// This is used to increment the product count yielded by the query,
// which is one less than the actual product count at this point in the
// product publish lifecycle. This is currently being used by
// ProductCountRuleProcessor.
$stored_state->new_product_count = 1;
RemoteInboxNotificationsEngine::update_stored_state( $stored_state );
RemoteInboxNotificationsEngine::run();
self::enqueue_async_run_remote_notifications();
}
$stored_state->new_product_count = 0;
RemoteInboxNotificationsEngine::update_stored_state( $stored_state );
/**
* Enqueues an async action (using action-scheduler) to run remote
* notifications.
*/
private static function enqueue_async_run_remote_notifications() {
as_enqueue_async_action( self::ASYNC_RUN_REMOTE_NOTIFICATIONS_ACTION_NAME );
}
}