Skip to content

Commit

Permalink
Sync: adapt checkout endpoint for immediate full sync (#14158)
Browse files Browse the repository at this point in the history
* fix checkout endpoint for immediate full sync[not verified]

* codeclimate

* update error message

* move callables to variables

* $this->

* we have the queue name already[not verified]

* don't block do_full_sync
  • Loading branch information
lezama authored and kraftbj committed Dec 6, 2019
1 parent 2043615 commit 15f2054
Showing 1 changed file with 40 additions and 2 deletions.
42 changes: 40 additions & 2 deletions json-endpoints/jetpack/class.jetpack-json-api-sync-endpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ protected function validate_queue( $query ) {
return new WP_Error( 'invalid_queue', 'Queue name is required', 400 );
}

if ( ! in_array( $query, array( 'sync', 'full_sync' ) ) ) {
return new WP_Error( 'invalid_queue', 'Queue name should be sync or full_sync', 400 );
if ( ! in_array( $query, array( 'sync', 'full_sync', 'immediate' ) ) ) {
return new WP_Error( 'invalid_queue', 'Queue name should be sync, full_sync or immediate', 400 );
}
return $query;
}
Expand Down Expand Up @@ -191,6 +191,14 @@ protected function result() {

$number_of_items = absint( $args['number_of_items'] );

if ( 'immediate' === $queue_name ) {
return $this->immediate_full_sync_pull( $number_of_items );
}

return $this->queue_pull( $queue_name, $number_of_items, $args );
}

function queue_pull( $queue_name, $number_of_items, $args ){
$queue = new Queue( $queue_name );

if ( 0 === $queue->size() ) {
Expand Down Expand Up @@ -233,6 +241,36 @@ protected function result() {
);
}

public $items = [];

public function jetpack_sync_send_data_listener() {
foreach ( func_get_args()[0] as $key => $item ) {
$this->items[ $key ] = $item;
}
}

public function immediate_full_sync_pull( $number_of_items ) {
// try to give ourselves as much time as possible.
set_time_limit( 0 );

$original_send_data_cb = array( 'Automattic\Jetpack\Sync\Actions', 'send_data' );
$temp_send_data_cb = array( $this, 'jetpack_sync_send_data_listener' );

Sender::get_instance()->set_enqueue_wait_time( 0 );
remove_filter( 'jetpack_sync_send_data', $original_send_data_cb );
add_filter( 'jetpack_sync_send_data', $temp_send_data_cb, 10, 6 );
Sender::get_instance()->do_full_sync();
remove_filter( 'jetpack_sync_send_data', $temp_send_data_cb );
add_filter( 'jetpack_sync_send_data', $original_send_data_cb, 10, 6 );

return array(
'items' => $this->items,
'codec' => Sender::get_instance()->get_codec()->name(),
'sent_timestamp' => time(),
'status' => Actions::get_sync_status(),
);
}

protected function get_buffer( $queue, $number_of_items ) {
$start = time();
$max_duration = 5; // this will try to get the buffer
Expand Down

0 comments on commit 15f2054

Please sign in to comment.