Skip to content
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

Merged

Conversation

jorgemd24
Copy link
Contributor

@jorgemd24 jorgemd24 commented Mar 5, 2024

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:

  1. Set your PHP memory limit low so you can catch any heavy process. For example, setting this up to 128MB.
  2. Call GET wp-json/wc/gla/mc/product-statistics/refresh
  3. Go to WooCommerce -> Status -> Action Scheduler -> Search for gla/jobs/update_merchant_product_statuses/process_item see how the jobs get completed successfully.
  4. See how the table wp_gla_merchant_issues gets populated with presync and account issues.

Additional details:

  • I refactor 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.
  • If the mc_statuses is not set for the product, we will consider that product is not synced in the MC. See here: Create product statuses jobs for each page token #2255 (comment)

Changelog entry

@github-actions github-actions bot added changelog: add A new feature, function, or functionality was added. type: enhancement The issue is a request for an enhancement. labels Mar 5, 2024
@jorgemd24 jorgemd24 changed the base branch from feature/refactor-product-stats to add/refactor-product-issues March 5, 2024 16:50
Copy link

codecov bot commented Mar 5, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 60.5%. Comparing base (c06f4e9) to head (6b7a614).
Report is 1 commits behind head on feature/refactor-product-stats.

Additional details and impacted files

Impacted file tree graph

@@                        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     
Flag Coverage Δ
php-unit-tests 60.5% <100.0%> (+0.2%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files Coverage Δ
...te/Controllers/MerchantCenter/IssuesController.php 85.5% <100.0%> (+0.7%) ⬆️
src/DB/ProductFeedQueryHelper.php 53.9% <100.0%> (+15.1%) ⬆️
src/Jobs/UpdateMerchantProductStatuses.php 92.9% <100.0%> (+0.3%) ⬆️
src/MerchantCenter/MerchantStatuses.php 72.5% <100.0%> (+5.9%) ⬆️
src/Product/ProductHelper.php 95.4% <100.0%> (ø)

Base automatically changed from add/refactor-product-issues to feature/refactor-product-stats March 5, 2024 17:53
@jorgemd24 jorgemd24 self-assigned this Mar 5, 2024
@jorgemd24 jorgemd24 changed the title Add/refactor account presync issues Refactor Account and Presync issues Mar 6, 2024
@jorgemd24 jorgemd24 requested a review from a team March 6, 2024 11:30
@jorgemd24 jorgemd24 marked this pull request as ready for review March 6, 2024 11:30
@@ -113,7 +113,7 @@ protected function get_issues_read_callback(): callable {
*/
protected function get_schema_properties(): array {
return [
'issues' => [
Copy link
Contributor

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 {
Copy link
Contributor

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

Comment on lines 102 to 106
// 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 = '-';
}

Copy link
Contributor

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 statusas null when is refreshing.

Copy link
Contributor Author

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();
Copy link
Contributor

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

Copy link
Contributor Author

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 {
Copy link
Contributor

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

Copy link
Contributor

@puntope puntope left a 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.

@jorgemd24
Copy link
Contributor Author

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!

Copy link
Contributor

@puntope puntope left a 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.

@jorgemd24 jorgemd24 merged commit b89c602 into feature/refactor-product-stats Mar 12, 2024
11 checks passed
@jorgemd24 jorgemd24 deleted the add/refactor-account-presync-issues branch March 12, 2024 16:01
@eason9487 eason9487 mentioned this pull request Mar 26, 2024
21 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
changelog: add A new feature, function, or functionality was added. type: enhancement The issue is a request for an enhancement.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants