2013-11-21 05:20:42 +09:00
|
|
|
<?php
|
2020-01-08 01:48:13 +09:00
|
|
|
// This file is part of GNU social - https://www.gnu.org/software/social
|
|
|
|
//
|
|
|
|
// GNU social is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// GNU social is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with GNU social. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2013-11-21 05:20:42 +09:00
|
|
|
/**
|
|
|
|
* GNU social queue-manager-on-visit class
|
|
|
|
*
|
|
|
|
* Will run events for a certain time, or until finished.
|
|
|
|
*
|
|
|
|
* Configure remote key if wanted with $config['opportunisticqm']['qmkey'] and
|
|
|
|
* use with /main/runqueue?qmkey=abc123
|
|
|
|
*
|
|
|
|
* @category Cron
|
|
|
|
* @package GNUsocial
|
|
|
|
* @author Mikael Nordfeldth <mmn@hethane.se>
|
2020-01-08 01:48:13 +09:00
|
|
|
* @copyright 2013 Free Software Foundation, Inc http://www.fsf.org
|
|
|
|
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
|
2013-11-21 05:20:42 +09:00
|
|
|
*/
|
2020-01-08 01:48:13 +09:00
|
|
|
|
|
|
|
defined('GNUSOCIAL') || die();
|
|
|
|
|
2019-09-03 05:39:55 +09:00
|
|
|
class OpportunisticQueueManager extends QueueManager
|
2013-11-21 05:20:42 +09:00
|
|
|
{
|
|
|
|
protected $qmkey = false;
|
|
|
|
protected $max_execution_time = null;
|
2016-01-11 21:35:29 +09:00
|
|
|
protected $max_execution_margin = null; // margin to PHP's max_execution_time
|
2013-11-21 05:20:42 +09:00
|
|
|
protected $max_queue_items = null;
|
|
|
|
|
2020-01-08 01:48:13 +09:00
|
|
|
protected $start_cpu_time = null;
|
2013-11-21 05:20:42 +09:00
|
|
|
protected $handled_items = 0;
|
|
|
|
|
2016-01-10 08:51:25 +09:00
|
|
|
protected $verbosity = null;
|
|
|
|
|
2019-09-03 05:39:55 +09:00
|
|
|
// typically just used for the /main/cron action, only used if php.ini max_execution_time is 0
|
|
|
|
const MAXEXECTIME = 20;
|
2013-11-21 05:20:42 +09:00
|
|
|
|
2020-01-08 01:48:13 +09:00
|
|
|
public function __construct(array $args = [])
|
|
|
|
{
|
2013-11-21 05:20:42 +09:00
|
|
|
foreach (get_class_vars(get_class($this)) as $key=>$val) {
|
|
|
|
if (array_key_exists($key, $args)) {
|
|
|
|
$this->$key = $args[$key];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$this->verifyKey();
|
|
|
|
|
2020-01-08 01:48:13 +09:00
|
|
|
if (is_null($this->start_cpu_time)) {
|
|
|
|
$this->start_cpu_time = hrtime(true);
|
2013-11-21 05:20:42 +09:00
|
|
|
}
|
|
|
|
|
2020-01-08 01:48:13 +09:00
|
|
|
if (is_null($this->max_execution_time)) {
|
2013-11-21 05:20:42 +09:00
|
|
|
$this->max_execution_time = ini_get('max_execution_time') ?: self::MAXEXECTIME;
|
|
|
|
}
|
|
|
|
|
2020-01-08 01:48:13 +09:00
|
|
|
if (is_null($this->max_execution_margin)) {
|
2019-09-03 05:39:55 +09:00
|
|
|
// think PHP's max exec time, minus this value to have time for timeouts etc.
|
|
|
|
$this->max_execution_margin = common_config('http', 'connect_timeout') + 1;
|
2016-01-07 19:58:09 +09:00
|
|
|
}
|
|
|
|
|
2013-11-21 05:20:42 +09:00
|
|
|
return parent::__construct();
|
|
|
|
}
|
|
|
|
|
2019-09-03 05:39:55 +09:00
|
|
|
|
2013-11-21 05:20:42 +09:00
|
|
|
protected function verifyKey()
|
|
|
|
{
|
|
|
|
if ($this->qmkey !== common_config('opportunisticqm', 'qmkey')) {
|
|
|
|
throw new RunQueueBadKeyException($this->qmkey);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-03 05:39:55 +09:00
|
|
|
public function enqueue($object, $key)
|
|
|
|
{
|
|
|
|
// Nothing to do, should never get called
|
|
|
|
throw new ServerException('OpportunisticQueueManager::enqueue should never be called');
|
|
|
|
}
|
|
|
|
|
2013-11-21 05:20:42 +09:00
|
|
|
public function canContinue()
|
|
|
|
{
|
2020-01-08 01:48:13 +09:00
|
|
|
$time_passed = (hrtime(true) - $this->start_cpu_time) / 1000000000;
|
2016-01-10 08:48:04 +09:00
|
|
|
|
2013-11-21 05:20:42 +09:00
|
|
|
// Only continue if limit values are sane
|
|
|
|
if ($time_passed <= 0 && (!is_null($this->max_queue_items) && $this->max_queue_items <= 0)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// If too much time has passed, stop
|
2019-09-03 05:39:55 +09:00
|
|
|
if ($time_passed >= $this->max_execution_time ||
|
|
|
|
$time_passed > ini_get('max_execution_time') - $this->max_execution_margin) {
|
2013-11-21 05:20:42 +09:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// If we have a max-item-limit, check if it has been passed
|
|
|
|
if (!is_null($this->max_queue_items) && $this->handled_items >= $this->max_queue_items) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function poll()
|
|
|
|
{
|
2019-09-03 05:39:55 +09:00
|
|
|
$qm = $this->get();
|
|
|
|
if ($qm->pollInterval() <= 0 && ! $qm instanceof UnQueueManager) {
|
|
|
|
// Special case for UnQueueManager as it is a default plugin
|
|
|
|
// and does not require queues, since it does everything immediately
|
|
|
|
throw new ServerException('OpportunisticQM cannot work together' .
|
|
|
|
'with queues that do not implement polling');
|
2013-11-21 05:20:42 +09:00
|
|
|
}
|
2019-09-03 05:39:55 +09:00
|
|
|
++$this->handled_items;
|
|
|
|
return $qm->poll();
|
2015-07-18 08:09:50 +09:00
|
|
|
}
|
|
|
|
|
2013-11-21 05:20:42 +09:00
|
|
|
/**
|
|
|
|
* Takes care of running through the queue items, returning when
|
|
|
|
* the limits setup in __construct are met.
|
|
|
|
*
|
|
|
|
* @return true on workqueue finished, false if there are still items in the queue
|
|
|
|
*/
|
|
|
|
public function runQueue()
|
|
|
|
{
|
|
|
|
while ($this->canContinue()) {
|
2019-09-03 05:39:55 +09:00
|
|
|
if (!$this->poll()) {
|
|
|
|
// Out of work
|
2013-11-21 05:20:42 +09:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2019-09-03 05:39:55 +09:00
|
|
|
|
2016-01-10 08:48:04 +09:00
|
|
|
if ($this->handled_items > 0) {
|
2019-09-03 05:39:55 +09:00
|
|
|
common_debug('Opportunistic queue manager passed execution time/item ' .
|
|
|
|
'handling limit without being out of work.');
|
|
|
|
return true;
|
2016-01-10 08:51:25 +09:00
|
|
|
} elseif ($this->verbosity > 1) {
|
2020-01-08 01:48:13 +09:00
|
|
|
$time_passed = (hrtime(true) - $this->start_cpu_time) / 1000000000;
|
2019-09-03 05:39:55 +09:00
|
|
|
common_debug('Opportunistic queue manager did not have time to start ' .
|
2020-01-08 01:48:13 +09:00
|
|
|
"on this action (max: {$this->max_execution_time}" .
|
|
|
|
" exceeded: {$time_passed}).");
|
2016-01-10 08:48:04 +09:00
|
|
|
}
|
2019-09-03 05:39:55 +09:00
|
|
|
|
2013-11-21 05:20:42 +09:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|