Completed
Push — fix/backoff-on-server-error ( eb8a93 )
by
unknown
62:20 queued 49:55
created

Jetpack_Sync_Sender::get_next_sync_time()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
require_once dirname( __FILE__ ) . '/class.jetpack-sync-queue.php';
4
require_once dirname( __FILE__ ) . '/class.jetpack-sync-defaults.php';
5
require_once dirname( __FILE__ ) . '/class.jetpack-sync-json-deflate-codec.php';
6
require_once dirname( __FILE__ ) . '/class.jetpack-sync-modules.php';
7
require_once dirname( __FILE__ ) . '/class.jetpack-sync-settings.php';
8
9
/**
10
 * This class grabs pending actions from the queue and sends them
11
 */
12
class Jetpack_Sync_Sender {
13
14
	const SYNC_THROTTLE_OPTION_NAME = 'jetpack_sync_min_wait';
15
	const NEXT_SYNC_TIME_OPTION_NAME = 'jetpack_next_sync_time';
16
	const WPCOM_ERROR_SYNC_DELAY = 60;
17
18
	private $dequeue_max_bytes;
19
	private $upload_max_bytes;
20
	private $upload_max_rows;
21
	private $sync_wait_time;
22
	private $sync_queue;
23
	private $codec;
24
25
	// singleton functions
26
	private static $instance;
27
28
	public static function get_instance() {
29
		if ( null === self::$instance ) {
30
			self::$instance = new self();
31
		}
32
33
		return self::$instance;
34
	}
35
36
	// this is necessary because you can't use "new" when you declare instance properties >:(
37
	protected function __construct() {
38
		$this->set_defaults();
39
		$this->init();
40
	}
41
42
	private function init() {
43
		foreach ( Jetpack_Sync_Modules::get_modules() as $module ) {
44
			$module->init_before_send();
45
		}
46
	}
47
48
	public function get_next_sync_time() {
49
		return (double) get_option( self::NEXT_SYNC_TIME_OPTION_NAME, 0 );
50
	}
51
52
	public function set_next_sync_time( $time ) {
53
		return update_option( self::NEXT_SYNC_TIME_OPTION_NAME, $time, true );
54
	}
55
56
	public function do_sync() {
57
		// don't sync if importing
58
		if ( defined( 'WP_IMPORTING' ) && WP_IMPORTING ) {
59
			return false;
60
		}
61
62
		// don't sync if we are throttled
63
		if ( $this->get_next_sync_time() > microtime( true ) ) {
64
			return false;
65
		}
66
67
		do_action( 'jetpack_sync_before_send' );
68
69
		if ( $this->sync_queue->size() === 0 ) {
70
			return false;
71
		}
72
73
		// now that we're sure we are about to sync, try to
74
		// ignore user abort so we can avoid getting into a
75
		// bad state
76
		if ( function_exists( 'ignore_user_abort' ) ) {
77
			ignore_user_abort( true );
78
		}
79
80
		$buffer = $this->sync_queue->checkout_with_memory_limit( $this->dequeue_max_bytes, $this->upload_max_rows );
81
82
		if ( ! $buffer ) {
83
			// buffer has no items
84
			return false;
85
		}
86
87
		if ( is_wp_error( $buffer ) ) {
88
			// another buffer is currently sending
89
			return false;
90
		}
91
92
		$upload_size   = 0;
93
		$items_to_send = array();
94
		$items         = $buffer->get_items();
95
96
		// set up current screen to avoid errors rendering content
97
		require_once(ABSPATH . 'wp-admin/includes/class-wp-screen.php');
98
		require_once(ABSPATH . 'wp-admin/includes/screen.php');
99
		set_current_screen( 'sync' );
100
101
		// we estimate the total encoded size as we go by encoding each item individually
102
		// this is expensive, but the only way to really know :/
103
		foreach ( $items as $key => $item ) {
104
			/**
105
			 * Modify the data within an action before it is serialized and sent to the server
106
			 * For example, during full sync this expands Post ID's into full Post objects,
107
			 * so that we don't have to serialize the whole object into the queue.
108
			 *
109
			 * @since 4.2.0
110
			 *
111
			 * @param array The action parameters
112
			 */
113
			$item[1] = apply_filters( 'jetpack_sync_before_send_' . $item[0], $item[1], $item[2] );
114
115
			$encoded_item = $this->codec->encode( $item );
116
117
			$upload_size += strlen( $encoded_item );
118
119
			if ( $upload_size > $this->upload_max_bytes && count( $items_to_send ) > 0 ) {
120
				break;
121
			}
122
123
			$items_to_send[ $key ] = $encoded_item;
124
		}
125
126
		/**
127
		 * Fires when data is ready to send to the server.
128
		 * Return false or WP_Error to abort the sync (e.g. if there's an error)
129
		 * The items will be automatically re-sent later
130
		 *
131
		 * @since 4.2.0
132
		 *
133
		 * @param array $data The action buffer
134
		 */
135
		$processed_item_ids = apply_filters( 'jetpack_sync_send_data', $items_to_send, $this->codec->name(), microtime( true ) );
136
137
		if ( ! $processed_item_ids || is_wp_error( $processed_item_ids ) ) {
138
			$processed_item_ids = $this->sync_queue->checkin( $buffer );
139
140
			if ( is_wp_error( $processed_item_ids ) ) {
141
				error_log( 'Error checking in buffer: ' . $processed_item_ids->get_error_message() );
142
				$this->sync_queue->force_checkin();
143
			}
144
145
			$this->set_next_sync_time( time() + self::WPCOM_ERROR_SYNC_DELAY );
146
			
147
		} else {
148
149
			// detect if the last item ID was an error
150
			$had_wp_error = is_wp_error( end( $processed_item_ids ) );
151
152
			if ( $had_wp_error ) {
153
				$wp_error = array_pop( $processed_item_ids );
0 ignored issues
show
Unused Code introduced by
$wp_error is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
154
			}
155
156
			$processed_items = array_intersect_key( $items, array_flip( $processed_item_ids ) );
157
158
			/**
159
			 * Allows us to keep track of all the actions that have been sent.
160
			 * Allows us to calculate the progress of specific actions.
161
			 *
162
			 * @since 4.2.0
163
			 *
164
			 * @param array $processed_actions The actions that we send successfully.
165
			 */
166
			do_action( 'jetpack_sync_processed_actions', $processed_items );
167
168
			$this->sync_queue->close( $buffer, $processed_item_ids );
169
170
			// detect if the last item ID was an error
171
			if ( $had_wp_error ) {
172
				$this->set_next_sync_time( time() + self::WPCOM_ERROR_SYNC_DELAY );
173
			} else {
174
				$this->set_next_sync_time( time() + $this->get_sync_wait_time() );
175
			}
176
		}
177
		
178
		return true;
179
	}
180
181
	function get_sync_queue() {
182
		return $this->sync_queue;
183
	}
184
185
	function get_codec() {
186
		return $this->codec;
187
	}
188
189
	function send_checksum() {
190
		require_once 'class.jetpack-sync-wp-replicastore.php';
191
		$store = new Jetpack_Sync_WP_Replicastore();
192
		do_action( 'jetpack_sync_checksum', $store->checksum_all() );
193
	}
194
195
	function reset_sync_queue() {
196
		Jetpack_Sync_Modules::get_module( 'full-sync' )->clear_status();
197
		$this->sync_queue->reset();
198
	}
199
200
	function set_dequeue_max_bytes( $size ) {
201
		$this->dequeue_max_bytes = $size;
202
	}
203
204
	// in bytes
205
	function set_upload_max_bytes( $max_bytes ) {
206
		$this->upload_max_bytes = $max_bytes;
207
	}
208
209
	// in rows
210
	function set_upload_max_rows( $max_rows ) {
211
		$this->upload_max_rows = $max_rows;
212
	}
213
214
	// in seconds
215
	function set_sync_wait_time( $seconds ) {
216
		$this->sync_wait_time = $seconds;
217
	}
218
219
	function get_sync_wait_time() {
220
		return $this->sync_wait_time;
221
	}
222
223
	function set_defaults() {
224
		$this->sync_queue = new Jetpack_Sync_Queue( 'sync' );
225
		$this->codec      = new Jetpack_Sync_JSON_Deflate_Codec();
226
227
		// saved settings
228
		$settings = Jetpack_Sync_Settings::get_settings();
229
		$this->set_dequeue_max_bytes( $settings['dequeue_max_bytes'] );
230
		$this->set_upload_max_bytes( $settings['upload_max_bytes'] );
231
		$this->set_upload_max_rows( $settings['upload_max_rows'] );
232
		$this->set_sync_wait_time( $settings['sync_wait_time'] );
233
	}
234
235
	function reset_data() {
236
		$this->reset_sync_queue();
237
238
		foreach ( Jetpack_Sync_Modules::get_modules() as $module ) {
239
			$module->reset_data();
240
		}
241
242
		delete_option( self::SYNC_THROTTLE_OPTION_NAME );
243
		delete_option( self::NEXT_SYNC_TIME_OPTION_NAME );
244
245
		Jetpack_Sync_Settings::reset_data();
246
	}
247
248
	function uninstall() {
249
		// Lets delete all the other fun stuff like transient and option and the sync queue
250
		$this->reset_data();
251
252
		// delete the full sync status
253
		delete_option( 'jetpack_full_sync_status' );
254
255
		// clear the sync cron.
256
		wp_clear_scheduled_hook( 'jetpack_sync_cron' );
257
258
		// clear the checksum cron
259
		wp_clear_scheduled_hook( 'jetpack_send_db_checksum' );
260
	}
261
}
262