]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Cronish/lib/cronish.php
Cron split into Cronish and OpportunisticQM
[quix0rs-gnu-social.git] / plugins / Cronish / lib / cronish.php
1 <?php
2 /**
3  * GNU social cron-on-visit class
4  *
5  * Keeps track, through Config dataobject class, of relative time since the 
6  * last run in order to to run event handlers with certain intervals.
7  *
8  * @category  Cron
9  * @package   GNUsocial
10  * @author    Mikael Nordfeldth <mmn@hethane.se>
11  * @copyright 2013 Free Software Foundation, Inc.
12  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
13  * @link      http://status.net/
14  */
15 class Cronish
16 {
17     /**
18      * Will call events as close as it gets to one hour. Event handlers
19      * which use this MUST be as quick as possible, maybe only adding a
20      * queue item to be handled later or something. Otherwise execution
21      * will timeout for PHP - or at least cause unnecessary delays for
22      * the unlucky user who visits the site exactly at one of these events.
23      */
24     public function callTimedEvents()
25     {
26         $timers = array('hourly' => 3600,
27                         'daily'  => 86400,
28                         'weekly' => 604800);
29
30         foreach($timers as $name=>$interval) {
31             $run = false;
32
33             $lastrun = new Config();
34             $lastrun->section = 'cron';
35             $lastrun->setting = 'last_' . $name;
36             $found = $lastrun->find(true);
37
38             if (!$found) {
39                 $lastrun->value = time();
40                 if ($lastrun->insert() === false) {
41                     common_log(LOG_WARNING, "Could not save 'cron' setting '{$name}'");
42                     continue;
43                 }
44                 $run = true;
45             } elseif ($lastrun->value < time() - $interval) {
46                 $orig    = clone($lastrun);
47                 $lastrun->value = time();
48                 $lastrun->update($orig);
49                 $run = true;
50             }
51
52             if ($run === true) {
53                 // such as CronHourly, CronDaily, CronWeekly
54                 Event::handle('Cron' . ucfirst($name));
55             }
56         }
57     }
58 }