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 <foo.ilyas@gmail.com>

* Update function name

* Add comment

Co-authored-by: Ilyas Foo <foo.ilyas@gmail.com>
This commit is contained in:
Chi-Hsuan Huang 2022-02-11 15:08:11 +08:00 committed by GitHub
parent e1ac06afa7
commit 4f5e537a54
2 changed files with 17 additions and 12 deletions

View File

@ -0,0 +1,4 @@
Significance: patch
Type: Fix
Fix too many pending run_remote_notifications actions. #8285

View File

@ -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 );
}
}