Created
December 18, 2014 16:19
-
-
Save derak-kilgo/6fc572d69e6dc4713a4b to your computer and use it in GitHub Desktop.
Wordpres Multisite Cron aware of system load
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Multisite Cron task. Hits the cron url for all enabled blogs so scheduled tasks function designed. | |
* @see http://www.lucasrolff.com/wordpress/why-wp-cron-sucks/ | |
*/ | |
require('./wp-load.php'); | |
//error_reporting(E_ALL); | |
//ini_set('display_errors',1); | |
ini_set('memory_limit','512M'); | |
echo '<pre>'; | |
global $wpdb; | |
$sql = $wpdb->prepare("SELECT domain, path FROM $wpdb->blogs WHERE archived = '0' and deleted = 0", ''); | |
$blogs = $wpdb->get_results($sql); | |
foreach($blogs as $blog) { | |
set_time_limit(180); //3min per site max. | |
$command = "http://" . $blog->domain . ($blog->path ? $blog->path : '/') . 'wp-cron.php?doing_wp_cron'; | |
//Consider system load before running cron. | |
$load = sys_getloadavg(); | |
if ($load[0] <= 1) { | |
echo 'Running cron for ' . $blog->path . "\n"; | |
$ch = curl_init($command); | |
$rc = curl_setopt($ch, CURLOPT_RETURNTRANSFER, FALSE); | |
$rc = curl_exec($ch); | |
curl_close($ch); | |
}else{ | |
echo 'Too busy. Skipped ' . $blog->path . "\n"; | |
} | |
} | |
echo '</pre>'; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment