Cron split into Cronish and OpportunisticQM

/main/cron changed to /main/runqueue

The key-required functionality is not throughly tested yet.
This commit is contained in:
Mikael Nordfeldth
2013-11-20 21:20:42 +01:00
parent 0cd93c2761
commit 9400795a5f
9 changed files with 388 additions and 3 deletions

View File

@@ -0,0 +1,102 @@
<?php
/**
* 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>
* @copyright 2013 Free Software Foundation, Inc.
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
* @link http://status.net/
*/
class OpportunisticQueueManager extends DBQueueManager
{
protected $qmkey = false;
protected $max_execution_time = null;
protected $max_queue_items = null;
protected $started_at = null;
protected $handled_items = 0;
const MAXEXECTIME = 30; // typically just used for the /main/cron action
public function __construct(array $args=array()) {
foreach (get_class_vars(get_class($this)) as $key=>$val) {
if (array_key_exists($key, $args)) {
$this->$key = $args[$key];
}
}
$this->verifyKey();
if ($this->started_at === null) {
$this->started_at = time();
}
if ($this->max_execution_time === null) {
$this->max_execution_time = ini_get('max_execution_time') ?: self::MAXEXECTIME;
}
return parent::__construct();
}
protected function verifyKey()
{
if ($this->qmkey !== common_config('opportunisticqm', 'qmkey')) {
throw new RunQueueBadKeyException($this->qmkey);
}
}
public function canContinue()
{
$time_passed = time() - $this->started_at;
// 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
if ($time_passed >= $this->max_execution_time) {
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()
{
$this->handled_items++;
if (!parent::poll()) {
throw new RunQueueOutOfWorkException();
}
return true;
}
/**
* 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()) {
try {
$this->poll();
} catch (RunQueueOutOfWorkException $e) {
common_debug('Opportunistic queue manager finished.');
return true;
}
}
common_debug('Opportunistic queue manager passed execution time/item handling limit without being out of work.');
return false;
}
}

View File

@@ -0,0 +1,39 @@
<?php
/**
* Class for the GNU social cron exception when a bad key is used
*
* PHP version 5
*
* LICENCE: This program 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.
*
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*
* @category Exception
* @package GNUsocial
* @author Mikael Nordfeldth <mmn@hethane.se>
* @copyright 2013 Free Software Foundation, Inc.
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPLv3
* @link http://www.gnu.org/software/social/
*/
if (!defined('GNUSOCIAL')) { exit(1); }
class RunQueueBadKeyException extends ClientException
{
public $qmkey;
public function __construct($qmkey)
{
$this->qmkey = $qmkey;
parent::__construct(_('Bad queue manager key was used.'));
}
}

View File

@@ -0,0 +1,36 @@
<?php
/**
* Class for the GNU social cron exception when there is no more work to be done
*
* PHP version 5
*
* LICENCE: This program 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.
*
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*
* @category Exception
* @package GNUsocial
* @author Mikael Nordfeldth <mmn@hethane.se>
* @copyright 2013 Free Software Foundation, Inc.
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPLv3
* @link http://www.gnu.org/software/social/
*/
if (!defined('GNUSOCIAL')) { exit(1); }
class RunQueueOutOfWorkException extends ServerException
{
public function __construct()
{
parent::__construct(_('Opportunistic queue manager is out of work (no more items).'));
}
}