Jetpack_Sync_Module_Full_Sync

This class does a full resync of the database by enqueuing an outbound action for every single object that we care about.

Defined (1)

The class is defined in the following location(s).

/sync/class.jetpack-sync-module-full-sync.php  
  1. class Jetpack_Sync_Module_Full_Sync extends Jetpack_Sync_Module { 
  2. const STATUS_OPTION_PREFIX = 'jetpack_sync_full_'; 
  3. const FULL_SYNC_TIMEOUT = 3600; 
  4.  
  5. public function name() { 
  6. return 'full-sync'; 
  7.  
  8. function init_full_sync_listeners( $callable ) { 
  9. // synthetic actions for full sync 
  10. add_action( 'jetpack_full_sync_start', $callable ); 
  11. add_action( 'jetpack_full_sync_end', $callable ); 
  12. add_action( 'jetpack_full_sync_cancelled', $callable ); 
  13.  
  14. function init_before_send() { 
  15. // this is triggered after actions have been processed on the server 
  16. add_action( 'jetpack_sync_processed_actions', array( $this, 'update_sent_progress_action' ) ); 
  17.  
  18. function start( $module_configs = null ) { 
  19. $was_already_running = $this->is_started() && ! $this->is_finished(); 
  20.  
  21. // remove all evidence of previous full sync items and status 
  22. $this->reset_data(); 
  23.  
  24. if ( $was_already_running ) { 
  25. /** 
  26. * Fires when a full sync is cancelled. 
  27. * @since 4.2.0 
  28. */ 
  29. do_action( 'jetpack_full_sync_cancelled' ); 
  30.  
  31. $this->update_status_option( 'started', time() ); 
  32. $this->update_status_option( 'params', $module_configs ); 
  33.  
  34. $enqueue_status = array(); 
  35. $full_sync_config = array(); 
  36.  
  37. // default value is full sync 
  38. if ( ! is_array( $module_configs ) ) { 
  39. $module_configs = array(); 
  40. foreach ( Jetpack_Sync_Modules::get_modules() as $module ) { 
  41. $module_configs[ $module->name() ] = true; 
  42.  
  43. // set default configuration, calculate totals, and save configuration if totals > 0 
  44. foreach ( Jetpack_Sync_Modules::get_modules() as $module ) { 
  45. $module_name = $module->name(); 
  46. $module_config = isset( $module_configs[ $module_name ] ) ? $module_configs[ $module_name ] : false; 
  47.  
  48. if ( ! $module_config ) { 
  49. continue; 
  50.  
  51. if ( 'users' === $module_name && 'initial' === $module_config ) { 
  52. $module_config = $module->get_initial_sync_user_config(); 
  53.  
  54. $enqueue_status[ $module_name ] = false; 
  55.  
  56. $total_items = $module->estimate_full_sync_actions( $module_config ); 
  57.  
  58. // if there's information to process, configure this module 
  59. if ( ! is_null( $total_items ) && $total_items > 0 ) { 
  60. $full_sync_config[ $module_name ] = $module_config; 
  61. $enqueue_status[ $module_name ] = array( 
  62. $total_items, // total 
  63. 0, // queued 
  64. false, // current state 
  65. ); 
  66.  
  67. $this->set_config( $full_sync_config ); 
  68. $this->set_enqueue_status( $enqueue_status ); 
  69.  
  70. /** 
  71. * Fires when a full sync begins. This action is serialized 
  72. * and sent to the server so that it knows a full sync is coming. 
  73. * @since 4.2.0 
  74. */ 
  75. do_action( 'jetpack_full_sync_start', $full_sync_config ); 
  76.  
  77. $this->continue_enqueuing( $full_sync_config, $enqueue_status ); 
  78.  
  79. return true; 
  80.  
  81. function continue_enqueuing( $configs = null, $enqueue_status = null ) { 
  82. if ( ! $this->is_started() || $this->get_status_option( 'queue_finished' ) ) { 
  83. return; 
  84.  
  85. // if full sync queue is full, don't enqueue more items 
  86. $max_queue_size_full_sync = Jetpack_Sync_Settings::get_setting( 'max_queue_size_full_sync' ); 
  87. $full_sync_queue = new Jetpack_Sync_Queue( 'full_sync' ); 
  88.  
  89. $available_queue_slots = $max_queue_size_full_sync - $full_sync_queue->size(); 
  90.  
  91. if ( $available_queue_slots <= 0 ) { 
  92. return; 
  93. } else { 
  94. $remaining_items_to_enqueue = min( Jetpack_Sync_Settings::get_setting( 'max_enqueue_full_sync' ), $available_queue_slots ); 
  95.  
  96. if ( ! $configs ) { 
  97. $configs = $this->get_config(); 
  98.  
  99. if ( ! $enqueue_status ) { 
  100. $enqueue_status = $this->get_enqueue_status(); 
  101.  
  102. foreach ( Jetpack_Sync_Modules::get_modules() as $module ) { 
  103. $module_name = $module->name(); 
  104.  
  105. // skip module if not configured for this sync or module is done 
  106. if ( ! isset( $configs[ $module_name ] )  
  107. || // no module config 
  108. ! $configs[ $module_name ]  
  109. || // no enqueue status 
  110. ! $enqueue_status[ $module_name ]  
  111. || // finished enqueuing this module 
  112. true === $enqueue_status[ $module_name ][ 2 ] ) { 
  113. continue; 
  114.  
  115. list( $items_enqueued, $next_enqueue_state ) = $module->enqueue_full_sync_actions( $configs[ $module_name ], $remaining_items_to_enqueue, $enqueue_status[ $module_name ][ 2 ] ); 
  116.  
  117. $enqueue_status[ $module_name ][ 2 ] = $next_enqueue_state; 
  118.  
  119. // if items were processed, subtract them from the limit 
  120. if ( ! is_null( $items_enqueued ) && $items_enqueued > 0 ) { 
  121. $enqueue_status[ $module_name ][ 1 ] += $items_enqueued; 
  122. $remaining_items_to_enqueue -= $items_enqueued; 
  123.  
  124. // stop processing if we've reached our limit of items to enqueue 
  125. if ( 0 >= $remaining_items_to_enqueue ) { 
  126. $this->set_enqueue_status( $enqueue_status ); 
  127. return; 
  128.  
  129. $this->set_enqueue_status( $enqueue_status ); 
  130.  
  131. // setting autoload to true means that it's faster to check whether we should continue enqueuing 
  132. $this->update_status_option( 'queue_finished', time(), true ); 
  133.  
  134. /** 
  135. * Fires when a full sync ends. This action is serialized 
  136. * and sent to the server. 
  137. * @since 4.2.0 
  138. */ 
  139. do_action( 'jetpack_full_sync_end', '' ); 
  140.  
  141. function update_sent_progress_action( $actions ) { 
  142.  
  143. // quick way to map to first items with an array of arrays 
  144. $actions_with_counts = array_count_values( array_filter( array_map( array( $this, 'get_action_name' ), $actions ) ) ); 
  145.  
  146. if ( ! $this->is_started() || $this->is_finished() ) { 
  147. return; 
  148.  
  149. if ( isset( $actions_with_counts['jetpack_full_sync_start'] ) ) { 
  150. $this->update_status_option( 'send_started', time() ); 
  151.  
  152. foreach ( Jetpack_Sync_Modules::get_modules() as $module ) { 
  153. $module_actions = $module->get_full_sync_actions(); 
  154. $status_option_name = "{$module->name()}_sent"; 
  155. $items_sent = $this->get_status_option( $status_option_name, 0 ); 
  156.  
  157. foreach ( $module_actions as $module_action ) { 
  158. if ( isset( $actions_with_counts[ $module_action ] ) ) { 
  159. $items_sent += $actions_with_counts[ $module_action ]; 
  160.  
  161. if ( $items_sent > 0 ) { 
  162. $this->update_status_option( $status_option_name, $items_sent ); 
  163. }  
  164.  
  165. if ( isset( $actions_with_counts['jetpack_full_sync_end'] ) ) { 
  166. $this->update_status_option( 'finished', time() ); 
  167.  
  168. public function get_action_name( $queue_item ) { 
  169. if ( is_array( $queue_item ) && isset( $queue_item[0] ) ) { 
  170. return $queue_item[0]; 
  171. return false; 
  172.  
  173. public function is_started() { 
  174. return !! $this->get_status_option( 'started' ); 
  175.  
  176. public function is_finished() { 
  177. return !! $this->get_status_option( 'finished' ); 
  178.  
  179. public function get_status() { 
  180. $status = array( 
  181. 'started' => $this->get_status_option( 'started' ),  
  182. 'queue_finished' => $this->get_status_option( 'queue_finished' ),  
  183. 'send_started' => $this->get_status_option( 'send_started' ),  
  184. 'finished' => $this->get_status_option( 'finished' ),  
  185. 'sent' => array(),  
  186. 'queue' => array(),  
  187. 'config' => $this->get_status_option( 'params' ),  
  188. 'total' => array(),  
  189. ); 
  190.  
  191. $enqueue_status = $this->get_enqueue_status(); 
  192.  
  193. foreach ( Jetpack_Sync_Modules::get_modules() as $module ) { 
  194. $name = $module->name(); 
  195.  
  196. if ( ! isset( $enqueue_status[ $name ] ) ) { 
  197. continue; 
  198.  
  199. list( $total, $queued, $state ) = $enqueue_status[ $name ]; 
  200.  
  201. if ( $total ) { 
  202. $status[ 'total' ][ $name ] = $total; 
  203.  
  204. if ( $queued ) { 
  205. $status[ 'queue' ][ $name ] = $queued; 
  206.  
  207. if ( $sent = $this->get_status_option( "{$name}_sent" ) ) { 
  208. $status[ 'sent' ][ $name ] = $sent; 
  209.  
  210. return $status; 
  211.  
  212. public function clear_status() { 
  213. $prefix = self::STATUS_OPTION_PREFIX; 
  214. Jetpack_Sync_Options::delete_option( "{$prefix}_started" ); 
  215. Jetpack_Sync_Options::delete_option( "{$prefix}_params" ); 
  216. Jetpack_Sync_Options::delete_option( "{$prefix}_queue_finished" ); 
  217. Jetpack_Sync_Options::delete_option( "{$prefix}_send_started" ); 
  218. Jetpack_Sync_Options::delete_option( "{$prefix}_finished" ); 
  219.  
  220. foreach ( Jetpack_Sync_Modules::get_modules() as $module ) { 
  221. Jetpack_Sync_Options::delete_option( "{$prefix}_{$module->name()}_sent" ); 
  222.  
  223. public function reset_data() { 
  224. $this->clear_status(); 
  225. require_once dirname( __FILE__ ) . '/class.jetpack-sync-listener.php'; 
  226. $listener = Jetpack_Sync_Listener::get_instance(); 
  227. $listener->get_full_sync_queue()->reset(); 
  228.  
  229. private function get_status_option( $name, $default = null ) { 
  230. $value = Jetpack_Sync_Options::get_option( self::STATUS_OPTION_PREFIX . "_$name", $default ); 
  231.  
  232. return is_numeric( $value ) ? intval( $value ) : $value; 
  233.  
  234. private function update_status_option( $name, $value, $autoload = false ) { 
  235. Jetpack_Sync_Options::update_option( self::STATUS_OPTION_PREFIX . "_$name", $value, $autoload ); 
  236.  
  237. private function set_enqueue_status( $new_status ) { 
  238. Jetpack_Sync_Options::update_option( 'jetpack_sync_full_enqueue_status', $new_status ); 
  239.  
  240. private function get_enqueue_status() { 
  241. return Jetpack_Sync_Options::get_option( 'jetpack_sync_full_enqueue_status' ); 
  242.  
  243. private function set_config( $config ) { 
  244. Jetpack_Sync_Options::update_option( 'jetpack_sync_full_config', $config ); 
  245.  
  246. private function get_config() { 
  247. return Jetpack_Sync_Options::get_option( 'jetpack_sync_full_config' ); 
  248.  
  249. private function write_option( $name, $value ) { 
  250. // we write our own option updating code to bypass filters/caching/etc on set_option/get_option 
  251. global $wpdb; 
  252. $serialized_value = maybe_serialize( $value ); 
  253. // try updating, if no update then insert 
  254. // TODO: try to deal with the fact that unchanged values can return updated_num = 0 
  255. // below we used "insert ignore" to at least suppress the resulting error 
  256. $updated_num = $wpdb->query( 
  257. $wpdb->prepare( 
  258. "UPDATE $wpdb->options SET option_value = %s WHERE option_name = %s",  
  259. $serialized_value,  
  260. $name 
  261. ); 
  262.  
  263. if ( ! $updated_num ) { 
  264. $updated_num = $wpdb->query( 
  265. $wpdb->prepare( 
  266. "INSERT IGNORE INTO $wpdb->options ( option_name, option_value, autoload ) VALUES ( %s, %s, 'no' )",  
  267. $name,  
  268. $serialized_value 
  269. ); 
  270. return $updated_num; 
  271.  
  272. private function read_option( $name, $default = null ) { 
  273. global $wpdb; 
  274. $value = $wpdb->get_var(  
  275. $wpdb->prepare( 
  276. "SELECT option_value FROM $wpdb->options WHERE option_name = %s LIMIT 1",  
  277. $name 
  278. ); 
  279. $value = maybe_unserialize( $value ); 
  280.  
  281. if ( $value === null && $default !== null ) { 
  282. return $default; 
  283.  
  284. return $value;