]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/OpportunisticQM/lib/opportunisticqueuemanager.php
Did the OpportunisticQM fixes in the wrong order
[quix0rs-gnu-social.git] / plugins / OpportunisticQM / lib / opportunisticqueuemanager.php
1 <?php
2 /**
3  * GNU social queue-manager-on-visit class
4  *
5  * Will run events for a certain time, or until finished.
6  *
7  * Configure remote key if wanted with $config['opportunisticqm']['qmkey'] and
8  * use with /main/runqueue?qmkey=abc123
9  *
10  * @category  Cron
11  * @package   GNUsocial
12  * @author    Mikael Nordfeldth <mmn@hethane.se>
13  * @copyright 2013 Free Software Foundation, Inc.
14  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
15  * @link      http://status.net/
16  */
17 class OpportunisticQueueManager extends DBQueueManager
18 {
19     protected $qmkey = false;
20     protected $max_execution_time = null;
21     protected $max_execution_margin = null; // margin to PHP's max_execution_time
22     protected $max_queue_items = null;
23
24     protected $started_at = null;
25     protected $handled_items = 0;
26
27     const MAXEXECTIME = 10; // typically just used for the /main/cron action, only used if php.ini max_execution_time is 0
28
29     public function __construct(array $args=array()) {
30         foreach (get_class_vars(get_class($this)) as $key=>$val) {
31             if (array_key_exists($key, $args)) {
32                 $this->$key = $args[$key];
33             }
34         }
35         $this->verifyKey();
36
37         if ($this->started_at === null) {
38             $this->started_at = time();
39         }
40
41         if ($this->max_execution_time === null) {
42             $this->max_execution_time = ini_get('max_execution_time') ?: self::MAXEXECTIME;
43         }
44
45         if ($this->max_execution_margin === null) {
46             $this->max_execution_margin = common_config('http', 'connect_timeout') + 1;   // think PHP's max exec time, minus this value to have time for timeouts etc.
47         }
48
49         return parent::__construct();
50     }
51
52     protected function verifyKey()
53     {
54         if ($this->qmkey !== common_config('opportunisticqm', 'qmkey')) {
55             throw new RunQueueBadKeyException($this->qmkey);
56         }
57     }
58
59     public function canContinue()
60     {
61         $time_passed = time() - $this->started_at;
62         
63         // Only continue if limit values are sane
64         if ($time_passed <= 0 && (!is_null($this->max_queue_items) && $this->max_queue_items <= 0)) {
65             return false;
66         }
67         // If too much time has passed, stop
68         if ($time_passed >= $this->max_execution_time || $time_passed > ini_get('max_execution_time') - $this->max_execution_margin) {
69             return false;
70         }
71         // If we have a max-item-limit, check if it has been passed
72         if (!is_null($this->max_queue_items) && $this->handled_items >= $this->max_queue_items) {
73             return false;
74         }
75
76         return true;
77     }
78
79     public function poll()
80     {
81         $this->handled_items++;
82         if (!parent::poll()) {
83             throw new RunQueueOutOfWorkException();
84         }
85         return true;
86     }
87
88     // OpportunisticQM shouldn't discard items it can't handle, we're
89     // only here to take care of what we _can_ handle!
90     protected function noHandlerFound(Queue_item $qi, $rep=null) {
91         $this->_log(LOG_WARNING, "[{$qi->transport}:item {$qi->id}] Releasing claim for queue item without a handler");              
92         $this->_fail($qi, true);    // true here means "releaseOnly", so no error statistics since it's not an _error_
93     }
94
95     protected function _fail(Queue_item $qi, $releaseOnly=false)
96     {
97         parent::_fail($qi, $releaseOnly);
98         $this->_log(LOG_DEBUG, "[{$qi->transport}:item {$qi->id}] Ignoring this transport for the rest of this execution");
99         $this->ignoreTransport($qi->transport);
100     }
101
102     /**
103      * Takes care of running through the queue items, returning when
104      * the limits setup in __construct are met.
105      *
106      * @return true on workqueue finished, false if there are still items in the queue
107      */
108     public function runQueue()
109     {
110         while ($this->canContinue()) {
111             try {
112                 $this->poll();
113             } catch (RunQueueOutOfWorkException $e) {
114                 return true;
115             }
116         }
117         common_debug('Opportunistic queue manager passed execution time/item handling limit without being out of work.');
118         return false;
119     }
120 }