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

Sync: Add Async Sender #7482

Closed
wants to merge 2 commits into from
Closed

Sync: Add Async Sender #7482

wants to merge 2 commits into from

Conversation

lezama
Copy link
Contributor

@lezama lezama commented Jul 18, 2017

Add the option to load the Jetpack Sync Sender on another request as core does it for cron*. (hat tip @westi).

This feature is Off by default, and can be turned on via a jetpack_sync_settings_async_sender filter

*We are avoiding just using spawn_cron as we already tried using it in the past, causing troubles with misconfigured cron setups.

How to test

turn on the feature(off by default):

  • Use the sync settings endpoint to set async_sender to 1
    or

  • Using a filter: add_filter('jetpack_sync_settings_async_sender', '__return_true');

  • on the ngrok console you should see a separate request to admin-ajax.php just for syncing

  • alternatively follow @enejb's instructions on Sync: add fastcgi finish request to sync #11146 and check that the original request doesn't get delayed

@enejb
Copy link
Member

enejb commented May 2, 2018

@lezama Do we still want to take this approach?

@lezama
Copy link
Contributor Author

lezama commented May 4, 2018

what do you think about offering it as an alternative?

@enejb
Copy link
Member

enejb commented May 4, 2018

Sure should we put in as an option or a constant?
maybe something we can change via the sync settings on a site

@lezama
Copy link
Contributor Author

lezama commented May 7, 2018

maybe something we can change via the sync settings on a site

👍

@brbrr brbrr added the [Type] Enhancement Changes to an existing feature — removing, adding, or changing parts of it label Jun 26, 2018
@stale
Copy link

stale bot commented Sep 24, 2018

This PR has been marked as stale. This happened because:

  • It has been inactive in the past 3 months.
  • It hasn’t been labeled `[Pri] Blocker`, `[Pri] High`.

No further action is needed. But it's worth checking if this PR has clear testing instructions, is it up to date with master, and it is still valid. Feel free to close this issue if you think it's not valid anymore — if you do, please add a brief explanation.

@stale stale bot added the [Status] Stale label Sep 24, 2018
@lezama lezama requested a review from a team as a code owner January 14, 2019 18:30
@jetpackbot
Copy link

jetpackbot commented Jan 14, 2019

Warnings
⚠️ "Testing instructions" are missing for this PR. Please add some
⚠️ "Proposed changelog entry" is missing for this PR. Please include any meaningful changes

This is automated check which relies on PULL_REQUEST_TEMPLATE.We encourage you to follow that template as it helps Jetpack maintainers do their job. If you think 'Testing instructions' or 'Proposed changelog entry' are not needed for your PR - please explain why you think so. Thanks for cooperation 🤖

Generated by 🚫 dangerJS against bb6c2b7

@enejb
Copy link
Member

enejb commented Jan 14, 2019

This works as expected for me.

To test this. I did the following.

diff --git a/sync/class.jetpack-sync-sender.php b/sync/class.jetpack-sync-sender.php
index 4a5b678e3..7f9a42f38 100644
--- a/sync/class.jetpack-sync-sender.php
+++ b/sync/class.jetpack-sync-sender.php
@@ -103,6 +103,8 @@ class Jetpack_Sync_Sender {
        }

        public function do_sync() {
+               l( 'do SYNC ', getmypid() );
+               sleep( 10 );
                return $this->do_sync_and_set_delays( $this->sync_queue );
        }

Did an api call to the site.
Noticed that the api call returned in 10 seconds.

And then enabled the async_sender option by setting it to true.
via the sync manager on .com

And notice that the error log still showed an do SYNC call.
but the api response was much faster.

enejb added a commit that referenced this pull request Jan 15, 2019
Based on the commit to core https://core.trac.wordpress.org/changeset/44488
That improves cron response. This PR tries to do the same but for Jetpack.

This works together with #7482
To improve the page load response time when sync is progress.
@lezama lezama added the [Status] Needs Review To request a review from Crew. Label will be renamed soon. label Jan 17, 2019
@lezama lezama added this to the 7.0 milestone Jan 17, 2019
@enejb
Copy link
Member

enejb commented Jan 22, 2019

@lezama can you review this again.

if ( $this->is_locked() ) {
$self = $this->get_instance();
// Remove any sync sending...
remove_action( 'shutdown', array( $self ,'do_sync' ) );
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can't we use $this?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure maybe.

@jeherve jeherve added [Status] In Progress and removed [Status] Needs Review To request a review from Crew. Label will be renamed soon. labels Jan 24, 2019
jeherve pushed a commit that referenced this pull request Jan 24, 2019
Based on the commit to core https://core.trac.wordpress.org/changeset/44488
That improves cron response. This PR tries to do the same but for Jetpack.

This works together with #7482
To improve the page load response time when sync is progress.

#### Testing instructions:

* Go to '..'

On an environment that support the function add the following to your code. 
```
diff --git a/sync/class.jetpack-sync-sender.php b/sync/class.jetpack-sync-sender.php
index 70cb4a09c..cd65e62bd 100644
--- a/sync/class.jetpack-sync-sender.php
+++ b/sync/class.jetpack-sync-sender.php
@@ -205,6 +205,8 @@ class Jetpack_Sync_Sender {
                if ( function_exists( 'fastcgi_finish_request' ) && version_compare( phpversion(), '7.0.16', '>=' ) ) {
                        fastcgi_finish_request();
                }
+               error_log( 'Does this show up' )?
+               sleep(10);

                $checkout_start_time = microtime( true );
```


Do something that should trigger a sync request. Such as triggering a full sync or doing updating a setting. 
Next try doing an api request to you site. 
Without the call to fastcgi_finish_request(); the api request should take at least 10 seconds to complete. 
With the call to `fastcgi_finish_request();` the request should be a lot faster. 


#### Proposed changelog entry for your changes:
* Improves sync performance on PHP 7.0.16 and higher with fastcgi_finish_request enabled.
@enejb
Copy link
Member

enejb commented Jan 25, 2019

testing.php
plugin to help us test this new feature.

<?php
/*
Plugin Name: Testing async syncing
Version: 1.0
*/

add_filter( 'pre_option_jetpack_sync_settings_async_sender', '__return_true' );
add_filter( 'jetpack_sync_send_data', 'eb_testing_send_data_sleep_before', 4, 1 );
add_filter( 'jetpack_sync_send_data', 'eb_testing_send_data_sleep_after', 14, 1 );

// fakes slow sync
function eb_testing_send_data_sleep_before( $send ) {
	error_log( 'Sleeping....' . getmypid() );
	sleep( 10 );
	return $send;
}

function eb_testing_send_data_sleep_after( $send ) {
	error_log( 'Done Sending... '. getmypid() );
	return $send;
}

@jeherve
Copy link
Member

jeherve commented Jan 28, 2019

Is this ready for review?

@lezama lezama modified the milestones: 7.0, 7.1 Jan 29, 2019
@jeherve jeherve removed this from the 7.1 milestone Feb 19, 2019
@kraftbj
Copy link
Contributor

kraftbj commented Apr 17, 2019

Just checking on the expected timeline for this? Are we hoping to land it soon or could close if we're not going to add it?

@lezama
Copy link
Contributor Author

lezama commented Apr 18, 2019

@enejb what do you think about this one?

@lezama
Copy link
Contributor Author

lezama commented Dec 6, 2019

came up with a snippet we can use inside a plugin:

if ( isset( $_POST['action'] ) && 'jetpack_sync_async_sender' === $_POST['action'] ) {
	add_filter( 'jetpack_sync_sender_should_load', '__return_true' );
	add_action( 'wp_ajax_nopriv_jetpack_sync_async_sender', '__return_true' );
	ignore_user_abort( true );
} else {
	if ( ! ( defined( 'DOING_CRON' ) && DOING_CRON ) ) {
		add_action( 'shutdown', function () {
			if ( ! empty( \Automattic\Jetpack\Sync\Actions::get_sync_status()['queue_size'] ) ) {
				$args = array(
					'body'      => array(
						'action' => 'jetpack_sync_async_sender',
					),
					'timeout'   => 0.01,
					'blocking'  => false,
					'sslverify' => apply_filters( 'https_local_ssl_verify', false )
				);
				wp_remote_post( admin_url( 'admin-ajax.php' ), $args );
			}
		}, 10000 );
	}
}```

@kraftbj
Copy link
Contributor

kraftbj commented Mar 30, 2021

Going to close this but @mdbitz or @bisko, feel free to bring this back from the dead if so desired.

@kraftbj kraftbj closed this Mar 30, 2021
@mdbitz
Copy link
Contributor

mdbitz commented Apr 5, 2021

Thanks Kraft, agreed this can be closed and we are referencing it from Asana in the backlog for when we circle back to it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Package] Sync [Type] Enhancement Changes to an existing feature — removing, adding, or changing parts of it
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

8 participants