Skip to content

Instantly share code, notes, and snippets.

@derak-kilgo
Created December 18, 2014 16:19
Show Gist options
  • Save derak-kilgo/6fc572d69e6dc4713a4b to your computer and use it in GitHub Desktop.
Save derak-kilgo/6fc572d69e6dc4713a4b to your computer and use it in GitHub Desktop.
Wordpres Multisite Cron aware of system load
<?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