-
Notifications
You must be signed in to change notification settings - Fork 21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Refactor Account and Presync issues #2298
Refactor Account and Presync issues #2298
Conversation
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## feature/refactor-product-stats #2298 +/- ##
==================================================================
+ Coverage 60.3% 60.5% +0.2%
- Complexity 4181 4183 +2
==================================================================
Files 457 457
Lines 17733 17745 +12
==================================================================
+ Hits 10693 10742 +49
+ Misses 7040 7003 -37
Flags with carried forward coverage won't be shown. Click here to find out more.
|
…nt-presync-issues
@@ -113,7 +113,7 @@ protected function get_issues_read_callback(): callable { | |||
*/ | |||
protected function get_schema_properties(): array { | |||
return [ | |||
'issues' => [ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💅 use WP_REST_Response as Response;
Is unused.
@@ -168,13 +160,16 @@ public function get_product_statistics( bool $force_refresh = false ): array { | |||
* @throws Exception If the account state can't be retrieved from Google. | |||
*/ | |||
public function get_issues( string $type = null, int $per_page = 0, int $page = 1, bool $force_refresh = false ): array { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💅 What do you think about updating the return docs?
* @return array With two indices, results (may be paged) and count (considers type).
Now we are returning 'loading' as well
src/DB/ProductFeedQueryHelper.php
Outdated
// If the refresh_status_data_job is scheduled, we don't know the status yet as it is being refreshed. | ||
if ( $refresh_status_data_job && $refresh_status_data_job->is_scheduled() ) { | ||
$mc_status = '-'; | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
❓ Do we really need this chunk of code? Maybe we can keep status
as null when is refreshing.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it is necessary because if there's no mc_status
, it'll just return 'not-synced'. And most of the time, mc_status
won't be null.
If the mc_statuses is not set for the product, we will consider that product is not synced in the MC. See here: #2255 (comment)
$mc_status = $product_helper->get_mc_status( $product ) ?: $product_helper->get_sync_status( $product ); |
I switched from using -
to null
because it might be clearer to indicate that there's no status yet.
// If force_refresh is true or if not transient, return empty array and scheduled the job to update the statuses. | ||
if ( ! $job->is_scheduled() && ( $force_refresh || ( ! $force_refresh && null === $this->mc_statuses ) ) ) { | ||
// Schedule job to update the statuses. | ||
$job->schedule(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why we need double-checking if the job is running?
AFAIK schedule()
checks is_running() for the current page.
I see you created the is_Schedule function without arguments to avoid page checks?
public function is_scheduled(): bool {
// We set 'args' to null so it matches any arguments. This is because it's possible to have multiple instances of the job running with different page tokens
return $this->is_running( null );
}
Maybe we can just tweak can_schedule
function and then rely on $job->schedule itself to check if the job is running.
/**
* Can the job be scheduled.
*
* @param array|null $args
*
* @return bool Returns true if the job can be scheduled.
*/
public function can_schedule( $args = [] ): bool {
return parent::can_schedule( $args ) && $this->merchant_center->is_connected() && ! $this->is_running( null );
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @puntope for the suggestion, if we do it like that, the second job won't start because at the time we schedule it, the first job hasn't finished yet.
$this->schedule( [ [ 'next_page_token' => $next_page_token ] ] ); |
* | ||
* @throws Exception If the account state can't be retrieved from Google. | ||
*/ | ||
public function refresh_account_and_presync_issues(): void { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💅 Maybe we can add @since
param
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @jorgemd24 for the refactor work. Everything works as expected. I left just some comments regarding the code.
Thanks @puntope for your time and your suggestions! I addressed your comments and replied to your questions! Do you mind to have a second look? Thanks! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for changes and explanation @jorgemd24 Looking good to me.
Changes proposed in this Pull Request:
Part of #2146 & #2250
This PR fetches the account and presync issues using the same background job used to fetch the product statuses. The issues will be fetched in the first batch. The logic remains mostly unchanged from before, the main difference is the addition of a new property `loading when retrieving the issues. This indicates whether the job is scheduled or running and if the process to fetch the issues is in progress. I'll create a follow-up PR to update the UI to reflect the loading status of the issues.
Detailed test instructions:
wp-json/wc/gla/mc/product-statistics/refresh
gla/jobs/update_merchant_product_statuses/process_item
see how the jobs get completed successfully.Additional details:
maybe_refresh_status_data
so it returns the AS job, so it is easier to determine if the process of updating the status and issues is scheduled or in progress.Changelog entry