Skip to content

Commit

Permalink
feature(cron): allows for a more systematic way of calling cron using…
Browse files Browse the repository at this point in the history
… one url

one cron url to call. http://YOUR.SITE/cron/run/
Where all the cron jobs will execute at the specified internval
when it's time window deadline has been reached. (i.e. 1min, 5min, 15min, ...)
  • Loading branch information
Wade Benson authored and wadebenson committed Sep 6, 2015
1 parent 369e9e2 commit 3c947fc
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 10 deletions.
76 changes: 66 additions & 10 deletions engine/lib/cron.php
Expand Up @@ -34,6 +34,58 @@ function _elgg_cron_init() {
elgg_register_admin_menu_item('administer', 'cron', 'statistics');
}

/**
* Cron run
*
* This function was designed to be called every one minute from a cron job to
* executes each Elgg cron period at the desired interval. Will also exeute
* any Elgg cron period that have not fired by the expected deadline.
*
* Can be called manually by: http://YOUR.SITE/cron/run/
*
* This will only execute cron periods at specified intervals to force execution
* of a specific period you will need to use http://YOUR.SITE/cron/<period>/
*
* @access private
*/
function _elgg_cron_run() {
$now = time();
$params = array();
$params['time'] = $now;

// Data to return to
$old_stdout = "";
$std_out = "";
ob_start();

$periods = array(
'minute' => 60,
'fiveminute' => 300,
'fifteenmin' => 900,
'halfhour' => 1800,
'hourly' => 3600,
'daily' => 86400,
'weekly' => 604800,
'monthly' => 2628000,
'yearly' => 31536000,
'reboot' => 31536000,
);

foreach ($periods as $period => $interval) {
$key = "cron_latest:$period:ts";
$ts = elgg_get_site_entity()->getPrivateSetting($key);
$deadline = $ts + $interval;
if ($now > $deadline) {
$old_stdout = elgg_trigger_plugin_hook('cron', $period, $params, $old_stdout);
$std_out .= ob_clean();
$std_out .= $old_stdout;
}
}

$std_out .= ob_get_clean();
echo $std_out;
}

/**
* Cron handler
*
Expand All @@ -52,22 +104,26 @@ function _elgg_cron_page_handler($page) {

$allowed_periods = elgg_get_config('elgg_cron_periods');

if (!in_array($period, $allowed_periods)) {
if (($period != 'run') && !in_array($period, $allowed_periods)) {
throw new \CronException("$period is not a recognized cron period.");
}

// Get a list of parameters
$params = array();
$params['time'] = time();
if ($period == 'run') {
_elgg_cron_run();
} else {
// Get a list of parameters
$params = array();
$params['time'] = time();

// Data to return to
$old_stdout = "";
ob_start();
// Data to return to
$old_stdout = "";
ob_start();

$old_stdout = elgg_trigger_plugin_hook('cron', $period, $params, $old_stdout);
$std_out = ob_get_clean();
$old_stdout = elgg_trigger_plugin_hook('cron', $period, $params, $old_stdout);
$std_out = ob_get_clean();

echo $std_out . $old_stdout;
echo $std_out . $old_stdout;
}
return true;
}

Expand Down
9 changes: 9 additions & 0 deletions install/config/osx/README.txt
@@ -0,0 +1,9 @@

Modify the elgg.cron.1min.plist and replace YOUR.DOMAIN.HERE with your domain:
<string>http://YOUR.DOMAIN.HERE/cron/run/</string>

copy the plist into /Library/LaunchAgents

From terminal load the plist like so:
launchctl load -w /Library/LaunchAgents/elgg.cron.1min.plist

21 changes: 21 additions & 0 deletions install/config/osx/elgg.cron.1min.plist
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>elgg.1min</string>
<key>ProgramArguments</key>
<array>
<string>/usr/bin/lwp-request</string>
<string>-m</string>
<string>GET</string>
<string>-d</string>
<string>http://YOUR.DOMAIN.HERE/cron/run/</string>
</array>
<key>RunAtLoad</key>
<true/>
<key>StartInterval</key>
<integer>60</integer>
</dict>
</plist>

0 comments on commit 3c947fc

Please sign in to comment.