|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* A buffer of items from the queue that can be checked out |
|
5
|
|
|
*/ |
|
6
|
|
|
class Jetpack_Sync_Queue_Buffer { |
|
7
|
|
|
public $id; |
|
8
|
|
|
public $items_with_ids; |
|
9
|
|
|
|
|
10
|
|
|
public function __construct( $items_with_ids ) { |
|
11
|
|
|
$this->id = uniqid(); |
|
12
|
|
|
$this->items_with_ids = $items_with_ids; |
|
13
|
|
|
} |
|
14
|
|
|
|
|
15
|
|
|
public function get_items() { |
|
16
|
|
|
return Jetpack_Sync_Utils::get_item_values( $this->items_with_ids ); |
|
17
|
|
|
} |
|
18
|
|
|
|
|
19
|
|
|
public function get_item_ids() { |
|
20
|
|
|
return Jetpack_Sync_Utils::get_item_ids( $this->items_with_ids ); |
|
21
|
|
|
} |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* A persistent queue that can be flushed in increments of N items, |
|
26
|
|
|
* and which blocks reads until checked-out buffers are checked in or |
|
27
|
|
|
* closed. This uses raw SQL for two reasons: speed, and not triggering |
|
28
|
|
|
* tons of added_option callbacks. |
|
29
|
|
|
*/ |
|
30
|
|
|
class Jetpack_Sync_Queue { |
|
|
|
|
|
|
31
|
|
|
public $id; |
|
32
|
|
|
private $checkout_size; |
|
33
|
|
|
private $row_iterator; |
|
34
|
|
|
|
|
35
|
|
|
function __construct( $id, $checkout_size = 10 ) { |
|
36
|
|
|
$this->id = str_replace( '-', '_', $id ); // necessary to ensure we don't have ID collisions in the SQL |
|
37
|
|
|
$this->checkout_size = $checkout_size; |
|
38
|
|
|
$this->memory_limit = 5000000; // 5MB |
|
|
|
|
|
|
39
|
|
|
$this->row_iterator = 0; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
function add( $item ) { |
|
43
|
|
|
global $wpdb; |
|
44
|
|
|
$added = false; |
|
45
|
|
|
// this basically tries to add the option until enough time has elapsed that |
|
46
|
|
|
// it has a unique (microtime-based) option key |
|
47
|
|
|
while ( ! $added ) { |
|
48
|
|
|
$rows_added = $wpdb->query( $wpdb->prepare( |
|
49
|
|
|
"INSERT INTO $wpdb->options (option_name, option_value,autoload) VALUES (%s, %s,%s)", |
|
50
|
|
|
$this->get_next_data_row_option_name(), |
|
51
|
|
|
serialize( $item ), |
|
52
|
|
|
'no' |
|
53
|
|
|
) ); |
|
54
|
|
|
$added = ( $rows_added !== 0 ); |
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
// Attempts to insert all the items in a single SQL query. May be subject to query size limits! |
|
59
|
|
|
function add_all( $items ) { |
|
60
|
|
|
global $wpdb; |
|
61
|
|
|
$base_option_name = $this->get_next_data_row_option_name(); |
|
62
|
|
|
|
|
63
|
|
|
$query = "INSERT INTO $wpdb->options (option_name, option_value,autoload) VALUES "; |
|
64
|
|
|
|
|
65
|
|
|
$rows = array(); |
|
66
|
|
|
|
|
67
|
|
|
for ( $i = 0; $i < count( $items ); $i += 1 ) { |
|
|
|
|
|
|
68
|
|
|
$option_name = esc_sql( $base_option_name . '-' . $i ); |
|
69
|
|
|
$option_value = esc_sql( serialize( $items[ $i ] ) ); |
|
70
|
|
|
$rows[] = "('$option_name', '$option_value', 'no')"; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
$rows_added = $wpdb->query( $query . join( ',', $rows ) ); |
|
74
|
|
|
|
|
75
|
|
|
if ( $rows_added !== count( $items ) ) { |
|
76
|
|
|
return new WP_Error( 'row_count_mismatch', "The number of rows inserted didn't match the size of the input array" ); |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
// Peek at the front-most item on the queue without checking it out |
|
81
|
|
|
function peek( $count = 1 ) { |
|
82
|
|
|
$items = $this->fetch_items( $count ); |
|
83
|
|
|
if ( $items ) { |
|
84
|
|
|
return Jetpack_Sync_Utils::get_item_values( $items ); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
return array(); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
// lag is the difference in time between the age of the oldest item and the current time |
|
91
|
|
|
function lag() { |
|
92
|
|
|
global $wpdb; |
|
93
|
|
|
|
|
94
|
|
|
$last_item_name = $wpdb->get_var( $wpdb->prepare( |
|
95
|
|
|
"SELECT option_name FROM $wpdb->options WHERE option_name LIKE %s ORDER BY option_name ASC LIMIT 1", |
|
96
|
|
|
"jpsq_{$this->id}-%" |
|
97
|
|
|
) ); |
|
98
|
|
|
|
|
99
|
|
|
if ( ! $last_item_name ) { |
|
100
|
|
|
return null; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
// break apart the item name to get the timestamp |
|
104
|
|
|
$matches = null; |
|
105
|
|
|
if ( preg_match( '/^jpsq_'.$this->id.'-(\d+\.\d+)-/', $last_item_name, $matches ) ) { |
|
106
|
|
|
return microtime(true) - floatval($matches[1]); |
|
107
|
|
|
} else { |
|
108
|
|
|
return null; |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
function reset() { |
|
113
|
|
|
global $wpdb; |
|
114
|
|
|
$this->delete_checkout_id(); |
|
115
|
|
|
$wpdb->query( $wpdb->prepare( |
|
116
|
|
|
"DELETE FROM $wpdb->options WHERE option_name LIKE %s", "jpsq_{$this->id}-%" |
|
117
|
|
|
) ); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
function size() { |
|
121
|
|
|
global $wpdb; |
|
122
|
|
|
|
|
123
|
|
|
return $wpdb->get_var( $wpdb->prepare( |
|
124
|
|
|
"SELECT count(*) FROM $wpdb->options WHERE option_name LIKE %s", "jpsq_{$this->id}-%" |
|
125
|
|
|
) ); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
// we use this peculiar implementation because it's much faster than count(*) |
|
129
|
|
|
function has_any_items() { |
|
130
|
|
|
global $wpdb; |
|
131
|
|
|
$value = $wpdb->get_var( $wpdb->prepare( |
|
132
|
|
|
"SELECT exists( SELECT option_name FROM $wpdb->options WHERE option_name LIKE %s )", "jpsq_{$this->id}-%" |
|
133
|
|
|
) ); |
|
134
|
|
|
|
|
135
|
|
|
return ( $value === "1" ); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
function checkout() { |
|
139
|
|
|
if ( $this->get_checkout_id() ) { |
|
140
|
|
|
return new WP_Error( 'unclosed_buffer', 'There is an unclosed buffer' ); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
$limit = $this->checkout_size; |
|
144
|
|
|
|
|
145
|
|
|
$before_usage = memory_get_usage(); |
|
146
|
|
|
$items = $this->fetch_items( $limit ); |
|
147
|
|
|
$after_usage = memory_get_usage(); |
|
148
|
|
|
$current_count = count( $items ); |
|
149
|
|
|
if ( $current_count === 0 ) { |
|
150
|
|
|
return false; |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
while( ( $after_usage - $before_usage < $this->memory_limit && $limit == $current_count ) ) { |
|
154
|
|
|
$limit = $current_count; |
|
155
|
|
|
$items = array_merge( $items, $this->fetch_items( $this->checkout_size, $limit ) ); |
|
156
|
|
|
$after_usage = memory_get_usage(); |
|
157
|
|
|
$current_count = count( $items ); |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
$buffer = new Jetpack_Sync_Queue_Buffer( array_slice( $items, 0, $current_count) ); |
|
161
|
|
|
|
|
162
|
|
|
$result = $this->set_checkout_id( $buffer->id ); |
|
163
|
|
|
|
|
164
|
|
|
if ( ! $result || is_wp_error( $result ) ) { |
|
165
|
|
|
error_log( "Badness setting checkout ID (this should not happen)" ); |
|
166
|
|
|
return $result; |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
return $buffer; |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
function _get_item_values( $limit = null, $offset = null ) { |
|
173
|
|
|
|
|
174
|
|
|
if ( is_null( $limit ) ) { |
|
175
|
|
|
$limit = $this->checkout_size; |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
$items = $this->fetch_items( $limit, $offset ); |
|
179
|
|
|
|
|
180
|
|
|
$buffer = new Jetpack_Sync_Queue_Buffer( array_slice( $items, 0, $limit ) ); |
|
181
|
|
|
|
|
182
|
|
|
return $buffer->get_items(); |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
function checkin( $buffer ) { |
|
186
|
|
|
$is_valid = $this->validate_checkout( $buffer ); |
|
187
|
|
|
|
|
188
|
|
|
if ( is_wp_error( $is_valid ) ) { |
|
189
|
|
|
error_log("Invalid checkin: ".$is_valid->get_error_message()); |
|
190
|
|
|
return $is_valid; |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
$this->delete_checkout_id(); |
|
194
|
|
|
|
|
195
|
|
|
return true; |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
function close( $buffer ) { |
|
199
|
|
|
$is_valid = $this->validate_checkout( $buffer ); |
|
200
|
|
|
|
|
201
|
|
|
if ( is_wp_error( $is_valid ) ) { |
|
202
|
|
|
error_log("Invalid close: ".$is_valid->get_error_message()); |
|
203
|
|
|
return $is_valid; |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
$this->delete_checkout_id(); |
|
207
|
|
|
|
|
208
|
|
|
global $wpdb; |
|
209
|
|
|
|
|
210
|
|
|
// all this fanciness is basically so we can prepare a statement with an IN(id1, id2, id3) clause |
|
211
|
|
|
$ids_to_remove = $buffer->get_item_ids(); |
|
212
|
|
|
if ( count( $ids_to_remove ) > 0 ) { |
|
213
|
|
|
$sql = "DELETE FROM $wpdb->options WHERE option_name IN (" . implode( ', ', array_fill( 0, count( $ids_to_remove ), '%s' ) ) . ')'; |
|
214
|
|
|
$query = call_user_func_array( array( $wpdb, 'prepare' ), array_merge( array( $sql ), $ids_to_remove ) ); |
|
215
|
|
|
$wpdb->query( $query ); |
|
216
|
|
|
} |
|
217
|
|
|
|
|
218
|
|
|
return true; |
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
|
|
function flush_all() { |
|
222
|
|
|
$items = Jetpack_Sync_Utils::get_item_values( $this->fetch_items() ); |
|
223
|
|
|
$this->reset(); |
|
224
|
|
|
|
|
225
|
|
|
return $items; |
|
226
|
|
|
} |
|
227
|
|
|
|
|
228
|
|
|
function get_all() { |
|
229
|
|
|
return $this->fetch_items(); |
|
230
|
|
|
} |
|
231
|
|
|
|
|
232
|
|
|
function set_checkout_size( $new_size ) { |
|
233
|
|
|
$this->checkout_size = $new_size; |
|
234
|
|
|
} |
|
235
|
|
|
|
|
236
|
|
|
function set_memory_limit( $new_memory_limit ) { |
|
237
|
|
|
$this->memory_limit = $new_memory_limit; |
|
238
|
|
|
} |
|
239
|
|
|
|
|
240
|
|
|
// use with caution, this could allow multiple processes to delete |
|
241
|
|
|
// and send from the queue at the same time |
|
242
|
|
|
function force_checkin() { |
|
243
|
|
|
$this->delete_checkout_id(); |
|
244
|
|
|
} |
|
245
|
|
|
|
|
246
|
|
|
// used to lock checkouts from the queue. |
|
247
|
|
|
// tries to wait up to $timeout seconds for the queue to be empty |
|
248
|
|
|
function lock( $timeout = 30 ) { |
|
249
|
|
|
$tries = 0; |
|
250
|
|
|
|
|
251
|
|
|
while( $this->has_any_items() && $tries < $timeout ) { |
|
252
|
|
|
sleep(1); |
|
253
|
|
|
$tries += 1; |
|
254
|
|
|
} |
|
255
|
|
|
|
|
256
|
|
|
if ( $tries === 30 ) { |
|
257
|
|
|
return new WP_Error( 'lock_timeout', 'Timeout waiting for sync queue to empty' ); |
|
258
|
|
|
} |
|
259
|
|
|
|
|
260
|
|
|
if ( $this->get_checkout_id() ) { |
|
261
|
|
|
return new WP_Error( 'unclosed_buffer', 'There is an unclosed buffer' ); |
|
262
|
|
|
} |
|
263
|
|
|
|
|
264
|
|
|
// hopefully this means we can acquire a checkout? |
|
265
|
|
|
$result = $this->set_checkout_id( 'lock' ); |
|
266
|
|
|
|
|
267
|
|
|
if ( ! $result || is_wp_error( $result ) ) { |
|
268
|
|
|
error_log( "badness setting checkout ID (this should not happen)" ); |
|
269
|
|
|
return $result; |
|
270
|
|
|
} |
|
271
|
|
|
|
|
272
|
|
|
return true; |
|
273
|
|
|
} |
|
274
|
|
|
|
|
275
|
|
|
function unlock() { |
|
276
|
|
|
$this->delete_checkout_id(); |
|
277
|
|
|
} |
|
278
|
|
|
|
|
279
|
|
|
private function get_checkout_id() { |
|
280
|
|
|
return get_transient( $this->get_checkout_transient_name() ); |
|
281
|
|
|
} |
|
282
|
|
|
|
|
283
|
|
|
private function set_checkout_id( $checkout_id ) { |
|
284
|
|
|
return set_transient( $this->get_checkout_transient_name(), $checkout_id, 5*60 ); // 5 minute timeout |
|
285
|
|
|
} |
|
286
|
|
|
|
|
287
|
|
|
private function delete_checkout_id() { |
|
288
|
|
|
delete_transient( $this->get_checkout_transient_name() ); |
|
289
|
|
|
} |
|
290
|
|
|
|
|
291
|
|
|
private function get_checkout_transient_name() { |
|
292
|
|
|
return "jpsq_{$this->id}_checkout"; |
|
293
|
|
|
} |
|
294
|
|
|
|
|
295
|
|
|
private function get_next_data_row_option_name() { |
|
296
|
|
|
// this option is specifically chosen to, as much as possible, preserve time order |
|
297
|
|
|
// and minimise the possibility of collisions between multiple processes working |
|
298
|
|
|
// at the same time |
|
299
|
|
|
// TODO: confirm we only need to support PHP 5.05+ (otherwise we'll need to emulate microtime as float, and avoid PHP_INT_MAX) |
|
|
|
|
|
|
300
|
|
|
// @see: http://php.net/manual/en/function.microtime.php |
|
301
|
|
|
$timestamp = sprintf( '%.6f', microtime( true ) ); |
|
302
|
|
|
|
|
303
|
|
|
// row iterator is used to avoid collisions where we're writing data waaay fast in a single process |
|
304
|
|
|
if ( $this->row_iterator === PHP_INT_MAX ) { |
|
305
|
|
|
$this->row_iterator = 0; |
|
306
|
|
|
} else { |
|
307
|
|
|
$this->row_iterator += 1; |
|
308
|
|
|
} |
|
309
|
|
|
|
|
310
|
|
|
return 'jpsq_' . $this->id . '-' . $timestamp . '-' . getmypid() . '-' . $this->row_iterator; |
|
311
|
|
|
} |
|
312
|
|
|
|
|
313
|
|
|
private function fetch_items( $limit = null, $offset = null ) { |
|
314
|
|
|
global $wpdb; |
|
315
|
|
|
|
|
316
|
|
|
if ( $limit ) { |
|
317
|
|
|
if ( $offset ) { |
|
318
|
|
|
$query_sql = $wpdb->prepare( "SELECT option_name AS id, option_value AS value FROM $wpdb->options WHERE option_name LIKE %s ORDER BY option_name ASC LIMIT %d, %d", "jpsq_{$this->id}-%", $offset, $limit ); |
|
319
|
|
|
} else { |
|
320
|
|
|
$query_sql = $wpdb->prepare( "SELECT option_name AS id, option_value AS value FROM $wpdb->options WHERE option_name LIKE %s ORDER BY option_name ASC LIMIT %d", "jpsq_{$this->id}-%", $limit ); |
|
321
|
|
|
} |
|
322
|
|
|
} else { |
|
323
|
|
|
$query_sql = $wpdb->prepare( "SELECT option_name AS id, option_value AS value FROM $wpdb->options WHERE option_name LIKE %s ORDER BY option_name ASC", "jpsq_{$this->id}-%" ); |
|
324
|
|
|
} |
|
325
|
|
|
|
|
326
|
|
|
$items = $wpdb->get_results( $query_sql ); |
|
327
|
|
|
foreach ( $items as $item ) { |
|
328
|
|
|
$item->value = maybe_unserialize( $item->value ); |
|
329
|
|
|
} |
|
330
|
|
|
|
|
331
|
|
|
return $items; |
|
332
|
|
|
} |
|
333
|
|
|
|
|
334
|
|
|
private function validate_checkout( $buffer ) { |
|
335
|
|
|
if ( ! $buffer instanceof Jetpack_Sync_Queue_Buffer ) { |
|
336
|
|
|
return new WP_Error( 'not_a_buffer', 'You must checkin an instance of Jetpack_Sync_Queue_Buffer' ); |
|
337
|
|
|
} |
|
338
|
|
|
|
|
339
|
|
|
$checkout_id = $this->get_checkout_id(); |
|
340
|
|
|
|
|
341
|
|
|
if ( ! $checkout_id ) { |
|
342
|
|
|
return new WP_Error( 'buffer_not_checked_out', 'There are no checked out buffers' ); |
|
343
|
|
|
} |
|
344
|
|
|
|
|
345
|
|
|
if ( $checkout_id != $buffer->id ) { |
|
346
|
|
|
return new WP_Error( 'buffer_mismatch', 'The buffer you checked in was not checked out' ); |
|
347
|
|
|
} |
|
348
|
|
|
|
|
349
|
|
|
return true; |
|
350
|
|
|
} |
|
351
|
|
|
} |
|
352
|
|
|
|
|
353
|
|
|
class Jetpack_Sync_Utils { |
|
|
|
|
|
|
354
|
|
|
|
|
355
|
|
|
static function get_item_values( $items ) { |
|
356
|
|
|
return array_map( array( __CLASS__, 'get_item_value' ), $items ); |
|
357
|
|
|
} |
|
358
|
|
|
|
|
359
|
|
|
static function get_item_ids( $items ) { |
|
360
|
|
|
return array_map( array( __CLASS__, 'get_item_id' ), $items ); |
|
361
|
|
|
} |
|
362
|
|
|
|
|
363
|
|
|
static private function get_item_value( $item ) { |
|
|
|
|
|
|
364
|
|
|
return $item->value; |
|
365
|
|
|
} |
|
366
|
|
|
|
|
367
|
|
|
static private function get_item_id( $item ) { |
|
|
|
|
|
|
368
|
|
|
return $item->id; |
|
369
|
|
|
} |
|
370
|
|
|
} |
|
371
|
|
|
|
Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.