← All changes
|
jetpack_vendor/automattic/jetpack-sync/src/class-sender.php
+38
-28
13.3.3
→
16.3-beta
View file →
| @@ -127,9 +127,9 @@ | ||
| 127 | 127 | * Incremental sync queue object. |
| 128 | 128 | * |
| 129 | 129 | * @access private |
| 130 | 130 | * |
| 131 | - * @var Automattic\Jetpack\Sync\Queue | |
| 131 | + * @var \Automattic\Jetpack\Sync\Queue | |
| 132 | 132 | */ |
| 133 | 133 | private $sync_queue; |
| 134 | 134 | |
| 135 | 135 | /** |
| @@ -136,9 +136,9 @@ | ||
| 136 | 136 | * Full sync queue object. |
| 137 | 137 | * |
| 138 | 138 | * @access private |
| 139 | 139 | * |
| 140 | - * @var Automattic\Jetpack\Sync\Queue | |
| 140 | + * @var \Automattic\Jetpack\Sync\Queue | |
| 141 | 141 | */ |
| 142 | 142 | private $full_sync_queue; |
| 143 | 143 | |
| 144 | 144 | /** |
| @@ -145,9 +145,9 @@ | ||
| 145 | 145 | * Codec object for encoding and decoding sync items. |
| 146 | 146 | * |
| 147 | 147 | * @access private |
| 148 | 148 | * |
| 149 | - * @var Automattic\Jetpack\Sync\Codec_Interface | |
| 149 | + * @var \Automattic\Jetpack\Sync\Codec_Interface | |
| 150 | 150 | */ |
| 151 | 151 | private $codec; |
| 152 | 152 | |
| 153 | 153 | /** |
| @@ -164,9 +164,9 @@ | ||
| 164 | 164 | * |
| 165 | 165 | * @access private |
| 166 | 166 | * @static |
| 167 | 167 | * |
| 168 | - * @var Automattic\Jetpack\Sync\Sender | |
| 168 | + * @var \Automattic\Jetpack\Sync\Sender | |
| 169 | 169 | */ |
| 170 | 170 | private static $instance; |
| 171 | 171 | |
| 172 | 172 | /** |
| @@ -225,9 +225,9 @@ | ||
| 225 | 225 | ! is_wp_error( $verified_user ) |
| 226 | 226 | && $verified_user |
| 227 | 227 | ) { |
| 228 | 228 | $old_user = wp_get_current_user(); |
| 229 | - $this->old_user = isset( $old_user->ID ) ? $old_user->ID : 0; | |
| 229 | + $this->old_user = $old_user->ID ?? 0; | |
| 230 | 230 | wp_set_current_user( $verified_user['user_id'] ); |
| 231 | 231 | } |
| 232 | 232 | } |
| 233 | 233 | |
| @@ -291,8 +291,9 @@ | ||
| 291 | 291 | * @return boolean|WP_Error True if this sync sending was successful, error object otherwise. |
| 292 | 292 | */ |
| 293 | 293 | public function do_full_sync() { |
| 294 | 294 | $sync_module = Modules::get_module( 'full-sync' ); |
| 295 | + '@phan-var Modules\Full_Sync_Immediately|Modules\Full_Sync $sync_module'; | |
| 295 | 296 | if ( ! $sync_module ) { |
| 296 | 297 | return; |
| 297 | 298 | } |
| 298 | 299 | // Full Sync Disabled. |
| @@ -312,9 +313,9 @@ | ||
| 312 | 313 | } |
| 313 | 314 | |
| 314 | 315 | $this->continue_full_sync_enqueue(); |
| 315 | 316 | // immediate full sync sends data in continue_full_sync_enqueue. |
| 316 | - if ( ! str_contains( get_class( $sync_module ), 'Full_Sync_Immediately' ) ) { | |
| 317 | + if ( ! $sync_module instanceof Modules\Full_Sync_Immediately ) { | |
| 317 | 318 | return $this->do_sync_and_set_delays( $this->full_sync_queue ); |
| 318 | 319 | } else { |
| 319 | 320 | $status = $sync_module->get_status(); |
| 320 | 321 | // Sync not started or Sync finished. |
| @@ -341,9 +342,11 @@ | ||
| 341 | 342 | if ( $this->get_next_sync_time( 'full-sync-enqueue' ) > microtime( true ) ) { |
| 342 | 343 | return false; |
| 343 | 344 | } |
| 344 | 345 | |
| 345 | - Modules::get_module( 'full-sync' )->continue_enqueuing(); | |
| 346 | + $full_sync_module = Modules::get_module( 'full-sync' ); | |
| 347 | + '@phan-var Modules\Full_Sync_Immediately|Modules\Full_Sync $full_sync_module'; | |
| 348 | + $full_sync_module->continue_enqueuing(); | |
| 346 | 349 | |
| 347 | 350 | $this->set_next_sync_time( time() + $this->get_enqueue_wait_time(), 'full-sync-enqueue' ); |
| 348 | 351 | } |
| 349 | 352 | |
| @@ -354,9 +357,12 @@ | ||
| 354 | 357 | * |
| 355 | 358 | * @return boolean|WP_Error True if this sync sending was successful, error object otherwise. |
| 356 | 359 | */ |
| 357 | 360 | public function do_sync() { |
| 358 | - if ( ! Settings::is_dedicated_sync_enabled() ) { | |
| 361 | + // Sync directly during cron. We are doing this because otherwise | |
| 362 | + // the dedicated sync flow would be spawning HTTP requests during cron shutdown, | |
| 363 | + // which can be unreliable and cause sync lag for time-sensitive events like updates. | |
| 364 | + if ( ! Settings::is_dedicated_sync_enabled() || Settings::is_doing_cron() ) { | |
| 359 | 365 | $result = $this->do_sync_and_set_delays( $this->sync_queue ); |
| 360 | 366 | } else { |
| 361 | 367 | $result = Dedicated_Sender::spawn_sync( $this->sync_queue ); |
| 362 | 368 | } |
| @@ -390,9 +396,9 @@ | ||
| 390 | 396 | * This is used to test the feature is working. |
| 391 | 397 | * |
| 392 | 398 | * @see \Automattic\Jetpack\Sync\Dedicated_Sender::can_spawn_dedicated_sync_request |
| 393 | 399 | */ |
| 394 | - // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped | |
| 400 | + // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- This is just a constant string used for Validation. | |
| 395 | 401 | echo Dedicated_Sender::DEDICATED_SYNC_VALIDATION_STRING; |
| 396 | 402 | |
| 397 | 403 | // Try to disconnect the request as quickly as possible and process things in the background. |
| 398 | 404 | $this->fastcgi_finish_request(); |
| @@ -414,14 +420,14 @@ | ||
| 414 | 420 | if ( session_status() === PHP_SESSION_ACTIVE ) { |
| 415 | 421 | session_write_close(); |
| 416 | 422 | } |
| 417 | 423 | |
| 424 | + // Actually try to send Sync events. | |
| 425 | + $result = $this->do_sync_and_set_delays( $this->sync_queue ); | |
| 426 | + | |
| 418 | 427 | // Output not used right now. Try to release dedicated sync lock |
| 419 | 428 | Dedicated_Sender::try_release_lock_spawn_request(); |
| 420 | 429 | |
| 421 | - // Actually try to send Sync events. | |
| 422 | - $result = $this->do_sync_and_set_delays( $this->sync_queue ); | |
| 423 | - | |
| 424 | 430 | // If no errors occurred, re-spawn a dedicated Sync request. |
| 425 | 431 | if ( true === $result ) { |
| 426 | 432 | Dedicated_Sender::spawn_sync( $this->sync_queue ); |
| 427 | 433 | } |
| @@ -426,9 +432,9 @@ | ||
| 426 | 432 | Dedicated_Sender::spawn_sync( $this->sync_queue ); |
| 427 | 433 | } |
| 428 | 434 | |
| 429 | 435 | if ( $do_real_exit ) { |
| 430 | - exit; | |
| 436 | + exit( 0 ); | |
| 431 | 437 | } |
| 432 | 438 | } |
| 433 | 439 | |
| 434 | 440 | /** |
| @@ -438,9 +444,9 @@ | ||
| 438 | 444 | * Will be delayed until the next sync time comes. |
| 439 | 445 | * |
| 440 | 446 | * @access public |
| 441 | 447 | * |
| 442 | - * @param Automattic\Jetpack\Sync\Queue $queue Queue object. | |
| 448 | + * @param \Automattic\Jetpack\Sync\Queue $queue Queue object. | |
| 443 | 449 | * |
| 444 | 450 | * @return boolean|WP_Error True if this sync sending was successful, error object otherwise. |
| 445 | 451 | */ |
| 446 | 452 | public function do_sync_and_set_delays( $queue ) { |
| @@ -493,10 +499,12 @@ | ||
| 493 | 499 | } |
| 494 | 500 | if ( 'wpcom_error' === $sync_result->get_error_code() ) { |
| 495 | 501 | $this->set_next_sync_time( time() + self::WPCOM_ERROR_SYNC_DELAY, $queue->id ); |
| 496 | 502 | } |
| 497 | - } elseif ( $exceeded_sync_wait_threshold ) { | |
| 498 | - // If we actually sent data and it took a while, wait before sending again. | |
| 503 | + } elseif ( $exceeded_sync_wait_threshold && ! Settings::is_doing_cron() ) { | |
| 504 | + // If a send was slow, briefly pause before the next one. | |
| 505 | + // Applies only to Dedicated/Normal Sync to avoid impacting user traffic; | |
| 506 | + // cron jobs are exempt. | |
| 499 | 507 | $this->set_next_sync_time( time() + $this->get_sync_wait_time(), $queue->id ); |
| 500 | 508 | } |
| 501 | 509 | |
| 502 | 510 | return $sync_result; |
| @@ -506,10 +514,10 @@ | ||
| 506 | 514 | * Retrieve the next sync items to send. |
| 507 | 515 | * |
| 508 | 516 | * @access public |
| 509 | 517 | * |
| 510 | - * @param (array|Automattic\Jetpack\Sync\Queue_Buffer) $buffer_or_items Queue buffer or array of objects. | |
| 511 | - * @param boolean $encode Whether to encode the items. | |
| 518 | + * @param (array|\Automattic\Jetpack\Sync\Queue_Buffer) $buffer_or_items Queue buffer or array of objects. | |
| 519 | + * @param boolean $encode Whether to encode the items. | |
| 512 | 520 | * @return array Sync items to send. |
| 513 | 521 | */ |
| 514 | 522 | public function get_items_to_send( $buffer_or_items, $encode = true ) { |
| 515 | 523 | // Track how long we've been processing so we can avoid request timeouts. |
| @@ -576,9 +584,9 @@ | ||
| 576 | 584 | * |
| 577 | 585 | * @access private |
| 578 | 586 | */ |
| 579 | 587 | private function fastcgi_finish_request() { |
| 580 | - if ( function_exists( 'fastcgi_finish_request' ) && version_compare( phpversion(), '7.0.16', '>=' ) ) { | |
| 588 | + if ( function_exists( 'fastcgi_finish_request' ) ) { | |
| 581 | 589 | fastcgi_finish_request(); |
| 582 | 590 | } |
| 583 | 591 | } |
| 584 | 592 | |
| @@ -586,9 +594,9 @@ | ||
| 586 | 594 | * Perform sync for a certain sync queue. |
| 587 | 595 | * |
| 588 | 596 | * @access public |
| 589 | 597 | * |
| 590 | - * @param Automattic\Jetpack\Sync\Queue $queue Queue object. | |
| 598 | + * @param \Automattic\Jetpack\Sync\Queue $queue Queue object. | |
| 591 | 599 | * |
| 592 | 600 | * @return boolean|WP_Error True if this sync sending was successful, error object otherwise. |
| 593 | 601 | */ |
| 594 | 602 | public function do_sync_for_queue( $queue ) { |
| @@ -700,18 +708,19 @@ | ||
| 700 | 708 | * Immediately sends a single item without firing or enqueuing it |
| 701 | 709 | * |
| 702 | 710 | * @param string $action_name The action. |
| 703 | 711 | * @param array $data The data associated with the action. |
| 712 | + * @param string $key The key to use for the action. | |
| 704 | 713 | * |
| 705 | - * @return Items processed. TODO: this doesn't make much sense anymore, it should probably be just a bool. | |
| 714 | + * @return array Items processed. TODO: this doesn't make much sense anymore, it should probably be just a bool. | |
| 706 | 715 | */ |
| 707 | - public function send_action( $action_name, $data = null ) { | |
| 716 | + public function send_action( $action_name, $data = null, $key = null ) { | |
| 708 | 717 | if ( ! Settings::is_sender_enabled( 'full_sync' ) ) { |
| 709 | 718 | return array(); |
| 710 | 719 | } |
| 711 | 720 | |
| 712 | 721 | // Compose the data to be sent. |
| 713 | - $action_to_send = $this->create_action_to_send( $action_name, $data ); | |
| 722 | + $action_to_send = $this->create_action_to_send( $action_name, $data, $key ); | |
| 714 | 723 | |
| 715 | 724 | list( $items_to_send, $skipped_items_ids, $items, $preprocess_duration ) = $this->get_items_to_send( $action_to_send, true ); // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
| 716 | 725 | Settings::set_is_sending( true ); |
| 717 | 726 | $processed_item_ids = apply_filters( 'jetpack_sync_send_data', $items_to_send, $this->get_codec()->name(), microtime( true ), 'immediate-send', 0, $preprocess_duration ); |
| @@ -737,13 +746,14 @@ | ||
| 737 | 746 | * @access private |
| 738 | 747 | * |
| 739 | 748 | * @param string $action_name The action. |
| 740 | 749 | * @param array $data The data associated with the action. |
| 750 | + * @param string $key The key to use for the action. | |
| 741 | 751 | * @return array An array of synthetic sync actions keyed by current microtime(true) |
| 742 | 752 | */ |
| 743 | - private function create_action_to_send( $action_name, $data ) { | |
| 753 | + private function create_action_to_send( $action_name, $data, $key = null ) { | |
| 744 | 754 | return array( |
| 745 | - (string) microtime( true ) => array( | |
| 755 | + $key ?? (string) microtime( true ) => array( | |
| 746 | 756 | $action_name, |
| 747 | 757 | $data, |
| 748 | 758 | get_current_user_id(), |
| 749 | 759 | microtime( true ), |
| @@ -789,9 +799,9 @@ | ||
| 789 | 799 | * Get the incremental sync queue object. |
| 790 | 800 | * |
| 791 | 801 | * @access public |
| 792 | 802 | * |
| 793 | - * @return Automattic\Jetpack\Sync\Queue Queue object. | |
| 803 | + * @return \Automattic\Jetpack\Sync\Queue Queue object. | |
| 794 | 804 | */ |
| 795 | 805 | public function get_sync_queue() { |
| 796 | 806 | return $this->sync_queue; |
| 797 | 807 | } |
| @@ -800,9 +810,9 @@ | ||
| 800 | 810 | * Get the full sync queue object. |
| 801 | 811 | * |
| 802 | 812 | * @access public |
| 803 | 813 | * |
| 804 | - * @return Automattic\Jetpack\Sync\Queue Queue object. | |
| 814 | + * @return \Automattic\Jetpack\Sync\Queue Queue object. | |
| 805 | 815 | */ |
| 806 | 816 | public function get_full_sync_queue() { |
| 807 | 817 | return $this->full_sync_queue; |
| 808 | 818 | } |
| @@ -811,9 +821,9 @@ | ||
| 811 | 821 | * Get the codec object. |
| 812 | 822 | * |
| 813 | 823 | * @access public |
| 814 | 824 | * |
| 815 | - * @return Automattic\Jetpack\Sync\Codec_Interface Codec object. | |
| 825 | + * @return \Automattic\Jetpack\Sync\Codec_Interface Codec object. | |
| 816 | 826 | */ |
| 817 | 827 | public function get_codec() { |
| 818 | 828 | return $this->codec; |
| 819 | 829 | } |