[PLUGINS] Added RedisQueue

This commit is contained in:
Miguel Dantas 2019-08-30 22:42:08 +01:00
parent 100a557c7c
commit a3b228399b
4 changed files with 137 additions and 2 deletions

View File

@ -164,7 +164,7 @@ abstract class QueueManager extends IoManager
* @param mixed $item * @param mixed $item
* @return string * @return string
*/ */
protected function encode($item) protected function encode($item): string
{ {
return serialize($item); return serialize($item);
} }
@ -176,7 +176,7 @@ abstract class QueueManager extends IoManager
* @param string * @param string
* @return mixed * @return mixed
*/ */
protected function decode($frame) protected function decode(string $frame)
{ {
$object = unserialize($frame); $object = unserialize($frame);

18
plugins/RedisQueue/README Normal file
View File

@ -0,0 +1,18 @@
RedisQueuePlugin wraps the RedisQueueManager class which is a queue manager
that uses Redis as it's backing storage.
Installation
============
This plugin is replaces other queue manager plugins, such as UnQueue,
which enabled by default and which should, but is not required to be
disabled.
addPlugin('RedisQueue', ['server' => 'your-redis-instance-and-port']);
Example
=======
In config.php
addPlugin('RedisQueue', ['server' => 'tcp://localhost:6379']);

View File

@ -0,0 +1,51 @@
<?php
// 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/>.
/**
* Redis interface for GNU social queues
*
* @package GNUsocial
* @author Miguel Dantas <biodantasgs@gmail.com>
* @copyright 2019 Free Software Foundation, Inc http://www.fsf.org
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
*/
defined('GNUSOCIAL') || die();
class RedisQueuePlugin extends Plugin
{
const PLUGIN_VERSION = '0.0.1';
// settings which can be set in config.php with addPlugin('RedisQueue', ['param'=>'value', ...]);
public $server = null;
public function onStartNewQueueManager(?QueueManager &$qm)
{
$qm = new RedisQueueManager($this->server);
return false;
}
public function onPluginVersion(array &$versions): bool
{
$versions[] = array('name' => 'RedisQueue',
'version' => self::PLUGIN_VERSION,
'author' => 'Miguel Dantas',
'description' =>
// TRANS: Plugin description.
_m('Plugin implementing Redis as a backend for GNU social queues'));
return true;
}
};

View File

@ -0,0 +1,66 @@
<?php
use Predis\Client;
class RedisQueueManager extends QueueManager
{
protected $queue;
protected $client = null;
protected $server;
public function __construct(string $server)
{
$this->server = $server;
$this->queue = 'gnusocial:' . common_config('site', 'name');
}
private function _ensureConn()
{
if ($this->client === null) {
$this->client = new Client($this->server);
}
}
public function pollInterval()
{
return 10;
}
public function enqueue($object, $queue)
{
$this->_ensureConn();
$ret = $this->client->rpush($this->queue, $this->encode([$queue, $object]));
if (empty($ret)) {
common_log(LOG_ERR, "Unable to insert object into Redis queue {$queue}");
} else {
common_debug("The Redis queue for {$queue} has length {$ret}");
}
}
public function poll()
{
common_debug("STARTING POLL");
try {
$this->_ensureConn();
$ret = $this->client->lpop($this->queue);
if (!empty($ret)) {
list($queue, $object) = $this->decode($ret);
} else {
return false;
}
} catch (Exception $e) {
$this->_log(LOG_INFO, "[Queue {$queue}] Discarding: " . _ve($e->getMessage()));
return false;
}
try {
$handler = $this->getHandler($queue);
$handler->handle($object);
common_debug("Redis Queue handled item from {$queue} queue");
return true;
} catch (Exception $e) {
$this->_log(LOG_ERR, "[Queue: {$queue}] `" . get_class($e) . '` thrown: ' . _ve($e->getMessage()));
return false;
}
}
};