From 4f5e537a549969e2c73743123cee5ffd16c8d1fc Mon Sep 17 00:00:00 2001 From: Chi-Hsuan Huang Date: Fri, 11 Feb 2022 15:08:11 +0800 Subject: [PATCH] Fix a lot of pending "run_remote_notifications" scheduled actions (https://github.com/woocommerce/woocommerce-admin/pull/8285) * Change to only create remote action when there_are_now_products is false * Add changelog * Update src/RemoteInboxNotifications/StoredStateSetupForProducts.php Co-authored-by: Ilyas Foo * Update function name * Add comment Co-authored-by: Ilyas Foo --- ...x-pending-run-remote-notifications-actions | 4 +++ .../StoredStateSetupForProducts.php | 25 ++++++++++--------- 2 files changed, 17 insertions(+), 12 deletions(-) create mode 100644 plugins/woocommerce-admin/changelogs/fix-pending-run-remote-notifications-actions diff --git a/plugins/woocommerce-admin/changelogs/fix-pending-run-remote-notifications-actions b/plugins/woocommerce-admin/changelogs/fix-pending-run-remote-notifications-actions new file mode 100644 index 00000000000..3f2594873e9 --- /dev/null +++ b/plugins/woocommerce-admin/changelogs/fix-pending-run-remote-notifications-actions @@ -0,0 +1,4 @@ +Significance: patch +Type: Fix + +Fix too many pending run_remote_notifications actions. #8285 diff --git a/plugins/woocommerce-admin/src/RemoteInboxNotifications/StoredStateSetupForProducts.php b/plugins/woocommerce-admin/src/RemoteInboxNotifications/StoredStateSetupForProducts.php index c84bf64ff1b..2d48dd56e45 100644 --- a/plugins/woocommerce-admin/src/RemoteInboxNotifications/StoredStateSetupForProducts.php +++ b/plugins/woocommerce-admin/src/RemoteInboxNotifications/StoredStateSetupForProducts.php @@ -89,11 +89,7 @@ class StoredStateSetupForProducts { } // phpcs:enable - $stored_state = RemoteInboxNotificationsEngine::get_stored_state(); - $stored_state->there_are_now_products = true; - RemoteInboxNotificationsEngine::update_stored_state( $stored_state ); - - self::enqueue_async_run_remote_notifications(); + self::update_stored_state_and_possibly_run_remote_notifications(); } /** @@ -112,19 +108,24 @@ class StoredStateSetupForProducts { return; } - $stored_state = RemoteInboxNotificationsEngine::get_stored_state(); - $stored_state->there_are_now_products = true; - - RemoteInboxNotificationsEngine::update_stored_state( $stored_state ); - - self::enqueue_async_run_remote_notifications(); + self::update_stored_state_and_possibly_run_remote_notifications(); } /** * Enqueues an async action (using action-scheduler) to run remote * notifications. */ - private static function enqueue_async_run_remote_notifications() { + private static function update_stored_state_and_possibly_run_remote_notifications() { + $stored_state = RemoteInboxNotificationsEngine::get_stored_state(); + // If the stored_state is the same, we don't need to run remote notifications to avoid unnecessary action scheduling. + if ( true === $stored_state->there_are_now_products ) { + return; + } + + $stored_state->there_are_now_products = true; + RemoteInboxNotificationsEngine::update_stored_state( $stored_state ); + + // Run self::run_remote_notifications asynchronously. as_enqueue_async_action( self::ASYNC_RUN_REMOTE_NOTIFICATIONS_ACTION_NAME ); } }