]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/OpportunisticQM/lib/opportunisticqueuemanager.php
Merge remote-tracking branch 'upstream/master' into social-master
[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_queue_items = null;
22
23     protected $started_at = null;
24     protected $handled_items = 0;
25
26     const MAXEXECTIME = 30; // typically just used for the /main/cron action
27
28     public function __construct(array $args=array()) {
29         foreach (get_class_vars(get_class($this)) as $key=>$val) {
30             if (array_key_exists($key, $args)) {
31                 $this->$key = $args[$key];
32             }
33         }
34         $this->verifyKey();
35
36         if ($this->started_at === null) {
37             $this->started_at = time();
38         }
39
40         if ($this->max_execution_time === null) {
41             $this->max_execution_time = ini_get('max_execution_time') ?: self::MAXEXECTIME;
42         }
43
44         return parent::__construct();
45     }
46
47     protected function verifyKey()
48     {
49         if ($this->qmkey !== common_config('opportunisticqm', 'qmkey')) {
50             throw new RunQueueBadKeyException($this->qmkey);
51         }
52     }
53
54     public function canContinue()
55     {
56         $time_passed = time() - $this->started_at;
57         
58         // Only continue if limit values are sane
59         if ($time_passed <= 0 && (!is_null($this->max_queue_items) && $this->max_queue_items <= 0)) {
60             return false;
61         }
62         // If too much time has passed, stop
63         if ($time_passed >= $this->max_execution_time) {
64             return false;
65         }
66         // If we have a max-item-limit, check if it has been passed
67         if (!is_null($this->max_queue_items) && $this->handled_items >= $this->max_queue_items) {
68             return false;
69         }
70
71         return true;
72     }
73
74     public function poll()
75     {
76         $this->handled_items++;
77         if (!parent::poll()) {
78             throw new RunQueueOutOfWorkException();
79         }
80         return true;
81     }
82
83     // OpportunisticQM shouldn't discard items it can't handle, we're
84     // only here to take care of what we _can_ handle!
85     protected function noHandlerFound(Queue_item $qi, $rep=null) {
86         $this->_log(LOG_WARNING, "[{$qi->transport}:item {$qi->id}] Releasing claim for queue item without a handler");              
87         $this->_fail($qi, true);    // true here means "releaseOnly", so no error statistics since it's not an _error_
88     }
89
90     protected function _fail(Queue_item $qi, $releaseOnly=false)
91     {
92         parent::_fail($qi, $releaseOnly);
93         $this->_log(LOG_DEBUG, "[{$qi->transport}:item {$qi->id}] Ignoring this transport for the rest of this execution");
94         $this->ignoreTransport($qi->transport);
95     }
96
97     /**
98      * Takes care of running through the queue items, returning when
99      * the limits setup in __construct are met.
100      *
101      * @return true on workqueue finished, false if there are still items in the queue
102      */
103     public function runQueue()
104     {
105         while ($this->canContinue()) {
106             try {
107                 $this->poll();
108             } catch (RunQueueOutOfWorkException $e) {
109                 common_debug('Opportunistic queue manager finished: ' . $e->getMessage());
110                 return true;
111             }
112         }
113         common_debug('Opportunistic queue manager passed execution time/item handling limit without being out of work.');
114         return false;
115     }
116 }