public static function do_sync()
{
global $wpdb;
$debug_mode = defined('KIGO_DEBUG') && KIGO_DEBUG;
// Do not log into New Relic, because this function is slow and we know why
if (extension_loaded('newrelic')) {
newrelic_ignore_transaction();
}
//Check that cron is "enabled" and that the secret is correct
if (!defined('KIGO_CRON_SECRET') || !isset($_GET[self::GET_PARAM_CRON_SECRET]) || $_GET[self::GET_PARAM_CRON_SECRET] !== KIGO_CRON_SECRET) {
self::log(array('message' => 'Missing/Invalid cron secret', 'info' => $_SERVER));
self::handle_logs($debug_mode);
exit;
}
// Ensure that no other cron will run concurrently by acquiring an advisory lock (at MySQL database)
if (!$wpdb->get_var($wpdb->prepare('SELECT GET_LOCK(%s, 0)', self::ADV_LOCK_PROCESSING))) {
self::log('Previous cron execution is not finished, could not acquire cron lock');
self::handle_logs($debug_mode);
exit;
}
$prevTimeTotal = microtime(true);
if (is_multisite()) {
require_once dirname(__FILE__) . '/ext/class-zebra-curl.php';
// Change the default value of wp_is_large_network necessary if # of sites reach the 10000
add_filter('wp_is_large_network', array('Kigo_Network_Cron', 'custom_wp_is_large_network'), 1, 3);
// Initialize the list of sites
$sites = wp_get_sites(array('limit' => self::CUSTOM_WP_IS_LARGE_NETWORK, 'deleted' => 0, 'archived' => 0));
shuffle($sites);
// Filter the sites, not to trigger a sync for site where the solution data have not been updated since X months
self::filter_old_sites($sites);
self::log(array('nb_sites' => count($sites)));
//Do the Zebra cURL call (asynchronous calls)
$curl = new Zebra_cURL();
$curl->option(CURLOPT_TIMEOUT, self::CURL_TIMEOUT);
$curl->threads = self::CURL_PARALLEL_CALLS;
//Prepare URLs to be called
$urls = array_map(array('Kigo_Network_Cron', 'generate_curl_urls'), $sites);
$urls = array_filter($urls, function ($url) {
return is_string($url);
});
$curl->get($urls, array('Kigo_Network_Cron', 'zebra_curl_callback'));
} else {
set_error_handler(array('Kigo_Network_Cron', 'php_error_handler'));
// Add our custom handler for wp_die() because some functions die on error, and we don't want the script to die !
add_filter('wp_die_ajax_handler', array('Kigo_Network_Cron', 'kigo_cron_wp_die_handler_filter'));
$site_cron = new Kigo_Site_Cron();
self::log($site_cron->sync_entities() ? true : $site_cron->_errors);
restore_error_handler();
}
self::log(array('total_execution_time' => microtime(true) - $prevTimeTotal));
if (!$wpdb->query($wpdb->prepare('SELECT RELEASE_LOCK(%s)', self::ADV_LOCK_PROCESSING))) {
self::log('Could not release cron lock');
}
// Echo the logs in debug mode or send them by mail
self::handle_logs($debug_mode);
exit;
}