Completed
Push — fix/save-full-sync-status-duri... ( 8ffb13...5a5ed4 )
by
unknown
10:50
created

Jetpack_Sync_Module_Full_Sync::read_option()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 10
nc 2
nop 2
dl 0
loc 16
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * This class does a full resync of the database by
5
 * enqueuing an outbound action for every single object
6
 * that we care about.
7
 *
8
 * This class, and its related class Jetpack_Sync_Module, contain a few non-obvious optimisations that should be explained:
9
 * - we fire an action called jetpack_full_sync_start so that WPCOM can erase the contents of the cached database
10
 * - for each object type, we page through the object IDs and enqueue them by firing some monitored actions
11
 * - we load the full objects for those IDs in chunks of Jetpack_Sync_Module::ARRAY_CHUNK_SIZE (to reduce the number of MySQL calls)
12
 * - we fire a trigger for the entire array which the Jetpack_Sync_Listener then serializes and queues.
13
 */
14
15
class Jetpack_Sync_Module_Full_Sync extends Jetpack_Sync_Module {
16
	const STATUS_OPTION_PREFIX = 'jetpack_sync_full_';
17
	const FULL_SYNC_TIMEOUT = 3600;
18
19
	private $items_added_since_last_pause;
20
	private $last_pause_time;
21
	private $queue_rate_limit;
22
23
	public function name() {
24
		return 'full-sync';
25
	}
26
27
	function init_full_sync_listeners( $callable ) {
28
		// synthetic actions for full sync
29
		add_action( 'jetpack_full_sync_start', $callable );
30
		add_action( 'jetpack_full_sync_end', $callable );
31
		add_action( 'jetpack_full_sync_cancelled', $callable );
32
	}
33
34
	function init_before_send() {
35
		// this is triggered after actions have been processed on the server
36
		add_action( 'jetpack_sync_processed_actions', array( $this, 'update_sent_progress_action' ) );
37
	}
38
39
	function start( $module_configs = null ) {
40
		$was_already_running = $this->is_started() && ! $this->is_finished();
41
42
		// remove all evidence of previous full sync items and status
43
		$this->reset_data();
44
45
		if ( $was_already_running ) {
46
			/**
47
			 * Fires when a full sync is cancelled.
48
			 *
49
			 * @since 4.2.0
50
			 */
51
			do_action( 'jetpack_full_sync_cancelled' );
52
		}
53
54
		// TODO: migrate old status options to new single status array
0 ignored issues
show
Coding Style Best Practice introduced by
Comments for TODO tasks are often forgotten in the code; it might be better to use a dedicated issue tracker.
Loading history...
55
		// OR separate sent-status from enqueue status
56
		$this->update_status_option( 'started', time() );
57
		$enqueue_status = array();
58
		$full_sync_config = array();
59
60
		// configure modules
61
		if ( ! is_array( $module_configs ) ) {
62
			$module_configs = array();
63
		}
64
65
		if ( isset( $module_configs['users'] ) && 'initial' === $module_configs['users'] ) {
66
			$user_module = Jetpack_Sync_Modules::get_module( 'users' );
67
			$module_configs['users'] = $user_module->get_initial_sync_user_config();
68
		}
69
70
		// by default, all modules are fully enabled
71
		if ( count( $module_configs ) === 0 ) {
72
			$default_module_config = true;
73
		} else {
74
			$default_module_config = false;
75
		}
76
77
		// set default configuration, calculate totals, and save configuration if totals > 0
78
		foreach ( Jetpack_Sync_Modules::get_modules() as $module ) {
79
			$module_name = $module->name();
80
			if ( ! isset( $module_configs[ $module_name ] ) ) {
81
				$module_configs[ $module_name ] = $default_module_config;
82
			}
83
84
			$enqueue_status[ $module_name ] = false;
85
86
			// check if this module is enabled
87
			if ( ! ( $module_config = $module_configs[ $module_name ] ) ) {
88
				continue;
89
			}
90
91
			$total_items = $module->estimate_full_sync_actions( $module_config );
92
93
			if ( ! is_null( $total_items ) && $total_items > 0 ) {
94
				$full_sync_config[ $module_name ] = $module_config;
95
				$enqueue_status[ $module_name ] = array(
96
					$total_items,   // total
97
					0,              // queued
98
					false,          // current state
99
				);
100
			}
101
		}
102
103
		$this->set_config( $full_sync_config );
104
		$this->set_enqueue_status( $enqueue_status );
105
106
		/**
107
		 * Fires when a full sync begins. This action is serialized
108
		 * and sent to the server so that it knows a full sync is coming.
109
		 *
110
		 * @since 4.2.0
111
		 */
112
		do_action( 'jetpack_full_sync_start', $module_configs );
113
114
		$this->continue_enqueuing( $full_sync_config, $enqueue_status );
115
116
		return true;
117
	}
118
119
	function continue_enqueuing( $configs = null, $enqueue_status = null ) {
120
		if ( ! $configs ) {
121
			$configs = $this->get_config();
122
		}
123
124
		if ( ! $enqueue_status ) {
125
			$enqueue_status = $this->get_enqueue_status();
126
		}
127
128
		// error_log(print_r($enqueue_status,1));
129
130
		$this->enable_queue_rate_limit();
131
132
		$remaining_items_to_enqueue = Jetpack_Sync_Settings::get_setting( 'max_enqueue_full_sync' );
133
134
		foreach ( Jetpack_Sync_Modules::get_modules() as $module ) {
135
			$module_name = $module->name();
136
137
			if ( ! isset( $configs[ $module_name ] ) ) {
138
				continue;
139
			}
140
141
			if ( 0 >= $remaining_items_to_enqueue ) {
142
				// drop out, we're not allowed to process more items than this
143
				$this->disable_queue_rate_limit();
144
				$this->set_enqueue_status( $enqueue_status );
145
				return;
146
			}
147
148
			// skip module if not configured for this sync or module is done
149
			if ( ! $configs[ $module_name ] || ! $enqueue_status[ $module_name ] || true === $enqueue_status[ $module_name ][ 2 ] ) {
150
				continue;
151
			}
152
153
			list( $items_enqueued, $next_enqueue_state ) = $module->enqueue_full_sync_actions( $configs[ $module_name ], $remaining_items_to_enqueue, $enqueue_status[ $module_name ][ 2 ] );
154
155
			$enqueue_status[ $module_name ][ 2 ] = $next_enqueue_state;
156
157
			// if items were processed, subtract them from the limit
158
			if ( ! is_null( $items_enqueued ) && $items_enqueued > 0 ) {
159
				$enqueue_status[ $module_name ][ 1 ] += $items_enqueued;
160
				$remaining_items_to_enqueue -= $items_enqueued;
161
			}
162
		}
163
164
		$this->disable_queue_rate_limit();
165
		$this->set_enqueue_status( $enqueue_status );
166
		$this->update_status_option( 'queue_finished', time() );
167
168
		/**
169
		 * Fires when a full sync ends. This action is serialized
170
		 * and sent to the server with checksums so that we can confirm the
171
		 * sync was successful.
172
		 *
173
		 * @since 4.2.0
174
		 */
175
		do_action( 'jetpack_full_sync_end', '' );
176
	}
177
178
	function update_sent_progress_action( $actions ) {
179
180
		// quick way to map to first items with an array of arrays
181
		$actions_with_counts = array_count_values( array_filter( array_map( array( $this, 'get_action_name' ), $actions ) ) );
182
183
		if ( ! $this->is_started() || $this->is_finished() ) {
184
			return;
185
		}
186
187
		if ( isset( $actions_with_counts['jetpack_full_sync_start'] ) ) {
188
			$this->update_status_option( 'sent_started', time() );
189
		}
190
191
		foreach ( Jetpack_Sync_Modules::get_modules() as $module ) {
192
			$module_actions     = $module->get_full_sync_actions();
193
			$status_option_name = "{$module->name()}_sent";
194
			$items_sent         = $this->get_status_option( $status_option_name, 0 );
195
196
			foreach ( $module_actions as $module_action ) {
197
				if ( isset( $actions_with_counts[ $module_action ] ) ) {
198
					$items_sent += $actions_with_counts[ $module_action ];
199
				}
200
			}
201
202
			if ( $items_sent > 0 ) {
203
				$this->update_status_option( $status_option_name, $items_sent );
204
			}	
205
		}
206
207
		if ( isset( $actions_with_counts['jetpack_full_sync_end'] ) ) {
208
			$this->update_status_option( 'finished', time() );
209
		}
210
	}
211
212
	public function get_action_name( $queue_item ) {
213
		if ( is_array( $queue_item ) && isset( $queue_item[0] ) ) {
214
			return $queue_item[0];
215
		}
216
		return false;
217
	}
218
219
	public function is_started() {
220
		return !! $this->get_status_option( 'started' );
221
	}
222
223
	public function is_finished() {
224
		return !! $this->get_status_option( 'finished' );
225
	}
226
227
	public function get_status() {
228
		$status = array(
229
			'started'        => $this->get_status_option( 'started' ),
230
			'queue_finished' => $this->get_status_option( 'queue_finished' ),
231
			'sent_started'   => $this->get_status_option( 'sent_started' ),
232
			'finished'       => $this->get_status_option( 'finished' ),
233
			'sent'           => array(),
234
			'queue'          => array(),
235
			'config'         => array(),
236
			'total'          => array(),
237
		);
238
239
		$enqueue_status = $this->get_enqueue_status();
240
		$module_config = $this->get_config();
241
242
		foreach ( Jetpack_Sync_Modules::get_modules() as $module ) {
243
			$name = $module->name();
244
245
			if ( ! isset( $module_config[ $name ] ) ) {
246
				continue;
247
			} else if ( $config = $module_config[ $name ] ) {
248
				$status[ 'config' ][ $name ] = $config;
249
			}
250
251
			if ( false === $enqueue_status[ $name ] ) {
252
				continue;
253
			}
254
255
			list( $total, $queued, $state ) = $enqueue_status[ $name ];
0 ignored issues
show
Unused Code introduced by
The assignment to $state is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
256
257
			if ( $total ) {
258
				$status[ 'total' ][ $name ] = $total;
259
			}
260
261
			if ( $queued ) {
262
				$status[ 'queue' ][ $name ] = $queued;
263
			}
264
			
265
			if ( $sent = $this->get_status_option( "{$name}_sent" ) ) {
266
				$status[ 'sent' ][ $name ] = $sent;
267
			}
268
		}
269
270
		return $status;
271
	}
272
273
	public function reset_data() {
274
		// $this->set_config( null ); // setting to null is quicker than deleting and re-adding
275
		// $this->set_status( null ); // TODO: not sure if clearing these is really necessary...
0 ignored issues
show
Coding Style Best Practice introduced by
Comments for TODO tasks are often forgotten in the code; it might be better to use a dedicated issue tracker.
Loading history...
276
		require_once dirname( __FILE__ ) . '/class.jetpack-sync-listener.php';
277
		$listener = Jetpack_Sync_Listener::get_instance();
278
		$listener->get_full_sync_queue()->reset();
279
	}
280
281
	private function get_status_option( $name, $default = null ) {
282
		$prefix = self::STATUS_OPTION_PREFIX;
283
284
		$value = get_option( "{$prefix}_{$name}", $default );
285
		
286
		if ( ! $value ) {
287
			// don't cast to int if we didn't find a value - we want to preserve null or false as sentinals
288
			return $default;
289
		}
290
291
		return is_numeric( $value ) ? intval( $value ) : $value;
292
	}
293
294
	private function update_status_option( $name, $value ) {
295
		$prefix = self::STATUS_OPTION_PREFIX;
296
		update_option( "{$prefix}_{$name}", $value, false );
297
	}
298
299
	private function set_enqueue_status( $new_status ) {
300
		$this->write_option( 'jetpack_sync_full_enqueue_status', $new_status );
301
	}
302
303
	private function get_enqueue_status() {
304
		return $this->read_option( 'jetpack_sync_full_enqueue_status' );
305
	}
306
307
	private function set_config( $config ) {
308
		$this->write_option( 'jetpack_sync_full_config', $config );
309
	}
310
	
311
	private function get_config() {
312
		return $this->read_option( 'jetpack_sync_full_config' );
313
	}
314
315
	private function write_option( $name, $value ) {
316
		// we write our own option updating code to bypass filters/caching/etc on set_option/get_option
317
		global $wpdb;
318
		$serialized_value = maybe_serialize( $value );
319
		// try updating, if no update then insert
320
		// TODO: try to deal with the fact that unchanged values can return updated_num = 0
0 ignored issues
show
Coding Style Best Practice introduced by
Comments for TODO tasks are often forgotten in the code; it might be better to use a dedicated issue tracker.
Loading history...
321
		// below we used "insert ignore" to at least suppress the resulting error
322
		$updated_num = $wpdb->query(
323
			$wpdb->prepare(
324
				"UPDATE $wpdb->options SET option_value = %s WHERE option_name = %s", 
325
				$serialized_value,
326
				$name
327
			)
328
		);
329
		// error_log("updated $name: $updated_num for ".$wpdb->last_query." - ".$wpdb->last_error);
330
		if ( ! $updated_num ) {
331
			$updated_num = $wpdb->query(
332
				$wpdb->prepare(
333
					"INSERT IGNORE INTO $wpdb->options ( option_name, option_value, autoload ) VALUES ( %s, %s, 'no' )", 
334
					$name,
335
					$serialized_value
336
				)
337
			);
338
		}
339
		return $updated_num;
340
	}
341
342
	private function read_option( $name, $default = null ) {
343
		global $wpdb;
344
		$value = $wpdb->get_var( 
345
			$wpdb->prepare(
346
				"SELECT option_value FROM $wpdb->options WHERE option_name = %s LIMIT 1", 
347
				$name
348
			)
349
		);
350
		$value = maybe_unserialize( $value );
351
352
		if ( $value === null && $default !== null ) {
353
			return $default;
354
		}
355
356
		return $value;
357
	}
358
359
	private function enable_queue_rate_limit() {
360
		$this->queue_rate_limit = Jetpack_Sync_Settings::get_setting( 'queue_max_writes_sec' );
361
		$this->items_added_since_last_pause = 0;
362
		$this->last_pause_time = microtime( true );
363
364
		add_action( 'jpsq_item_added', array( $this, 'queue_item_added' ) );
365
		add_action( 'jpsq_items_added', array( $this, 'queue_items_added' ) );
366
	}
367
368
	private function disable_queue_rate_limit() {
369
		remove_action( 'jpsq_item_added', array( $this, 'queue_item_added' ) );
370
		remove_action( 'jpsq_items_added', array( $this, 'queue_items_added' ) );
371
	}
372
373
	public function queue_item_added() {
374
		$this->queue_items_added( 1 );
375
	}
376
377
	public function queue_items_added( $item_count ) {
0 ignored issues
show
Unused Code introduced by
The parameter $item_count is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
378
		// jpsq_item_added and jpsq_items_added both exec 1 db query, 
379
		// so we ignore $item_count and treat it as always 1
380
		$this->items_added_since_last_pause += 1; 
381
382
		if ( $this->items_added_since_last_pause > $this->queue_rate_limit ) {
383
			// sleep for the rest of the second
384
			$sleep_til = $this->last_pause_time + 1.0;
385
			$sleep_duration = $sleep_til - microtime( true );
386
			if ( $sleep_duration > 0.0 ) {
387
				usleep( $sleep_duration * 1000000 );
388
				$this->last_pause_time = microtime( true );
389
			}
390
			$this->items_added_since_last_pause = 0;
391
		}
392
	}
393
}
394