Cron split into Cronish and OpportunisticQM
/main/cron changed to /main/runqueue The key-required functionality is not throughly tested yet.
This commit is contained in:
42
plugins/Cronish/CronishPlugin.php
Normal file
42
plugins/Cronish/CronishPlugin.php
Normal file
@@ -0,0 +1,42 @@
|
||||
<?php
|
||||
|
||||
class CronishPlugin extends Plugin {
|
||||
public function onCronHourly()
|
||||
{
|
||||
common_debug('CRON: Running hourly cron job!');
|
||||
}
|
||||
|
||||
public function onCronDaily()
|
||||
{
|
||||
common_debug('CRON: Running daily cron job!');
|
||||
}
|
||||
|
||||
public function onCronWeekly()
|
||||
{
|
||||
common_debug('CRON: Running weekly cron job!');
|
||||
}
|
||||
|
||||
/**
|
||||
* When the page has finished rendering, let's do some cron jobs
|
||||
* if we have the time.
|
||||
*/
|
||||
public function onEndActionExecute($status, Action $action)
|
||||
{
|
||||
$cron = new Cronish();
|
||||
$cron->callTimedEvents();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public function onPluginVersion(&$versions)
|
||||
{
|
||||
$versions[] = array('name' => 'Cronish',
|
||||
'version' => GNUSOCIAL_VERSION,
|
||||
'author' => 'Mikael Nordfeldth',
|
||||
'homepage' => 'http://www.gnu.org/software/social/',
|
||||
'description' =>
|
||||
// TRANS: Plugin description.
|
||||
_m('Cronish plugin that executes events on a near-hour/day/week basis.'));
|
||||
return true;
|
||||
}
|
||||
}
|
58
plugins/Cronish/lib/cronish.php
Normal file
58
plugins/Cronish/lib/cronish.php
Normal file
@@ -0,0 +1,58 @@
|
||||
<?php
|
||||
/**
|
||||
* GNU social cron-on-visit class
|
||||
*
|
||||
* Keeps track, through Config dataobject class, of relative time since the
|
||||
* last run in order to to run event handlers with certain intervals.
|
||||
*
|
||||
* @category Cron
|
||||
* @package GNUsocial
|
||||
* @author Mikael Nordfeldth <mmn@hethane.se>
|
||||
* @copyright 2013 Free Software Foundation, Inc.
|
||||
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
|
||||
* @link http://status.net/
|
||||
*/
|
||||
class Cronish
|
||||
{
|
||||
/**
|
||||
* Will call events as close as it gets to one hour. Event handlers
|
||||
* which use this MUST be as quick as possible, maybe only adding a
|
||||
* queue item to be handled later or something. Otherwise execution
|
||||
* will timeout for PHP - or at least cause unnecessary delays for
|
||||
* the unlucky user who visits the site exactly at one of these events.
|
||||
*/
|
||||
public function callTimedEvents()
|
||||
{
|
||||
$timers = array('hourly' => 3600,
|
||||
'daily' => 86400,
|
||||
'weekly' => 604800);
|
||||
|
||||
foreach($timers as $name=>$interval) {
|
||||
$run = false;
|
||||
|
||||
$lastrun = new Config();
|
||||
$lastrun->section = 'cron';
|
||||
$lastrun->setting = 'last_' . $name;
|
||||
$found = $lastrun->find(true);
|
||||
|
||||
if (!$found) {
|
||||
$lastrun->value = time();
|
||||
if ($lastrun->insert() === false) {
|
||||
common_log(LOG_WARNING, "Could not save 'cron' setting '{$name}'");
|
||||
continue;
|
||||
}
|
||||
$run = true;
|
||||
} elseif ($lastrun->value < time() - $interval) {
|
||||
$orig = clone($lastrun);
|
||||
$lastrun->value = time();
|
||||
$lastrun->update($orig);
|
||||
$run = true;
|
||||
}
|
||||
|
||||
if ($run === true) {
|
||||
// such as CronHourly, CronDaily, CronWeekly
|
||||
Event::handle('Cron' . ucfirst($name));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user