← All changes
|
jetpack_vendor/automattic/jetpack-sync/src/class-sender.php
+26
-16
13.4.5
→
16.3-beta
View file →
| @@ -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 | /** |
| @@ -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; |
| @@ -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 | |
| @@ -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 | 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 ), |