Assembla home | Assembla project page
 

root/trunk/Phergie/Plugin/Abstract/Cron.php

Revision 275, 1.7 kB (checked in by Seldaek, 2 months ago)

fixes #45

Line 
1 <?php
2
3 /**
4  * Implements delaying of asynchronous task execution.
5  *
6  * The delay configuration setting should be set to the minimum amount
7  * of time (in seconds) between consecutive executions of the task being
8  * delayed.
9  */
10 abstract class Phergie_Plugin_Abstract_Cron extends Phergie_Plugin_Abstract_Command
11 {
12     /**
13      * Default value (in seconds) for delay setting should it not be specified
14      * in the configuration file, can be overriden by subclasses
15      *
16      * @var int
17      */
18     protected $defaultDelay = 60;
19
20     /**
21      * Time delay (in seconds) must pass between consecutive task executions
22      *
23      * @var int
24      */
25     private $delay = null;
26
27     /**
28      * UNIX timestamp representing the earliest time at which the occurrence of
29      * an event (PING or PRIVMSG) will trigger the task execution
30      *
31      * @var int
32      */
33     private $nextCall = null;
34
35     /**
36      * Performs a single execution of the task being delayed.
37      *
38      * @return void
39      */
40     abstract protected function run();
41
42     /**
43      * Checks if the run method should be executed and executes it if needed.
44      *
45      * @return void
46      */
47     final protected function check()
48     {
49         if ($this->delay === null) {
50             if ((($time = $this->getPluginIni('delay')) !== null) || $time = $this->defaultDelay) {
51                 $this->delay = $time;
52             }
53         }
54         if (time() > $this->nextCall) {
55             $this->run();
56             $this->nextCall = time() + $this->delay;
57         }
58     }
59
60     /**
61      * Checks to see if the run method should be called when a raw event
62      * occurs.
63      *
64      * @return void
65      */
66     public function onRaw()
67     {
68         $this->check();
69     }
70 }
71
Note: See TracBrowser for help on using the browser.