mirror of
https://github.com/newnius/YAO-portal.git
synced 2025-12-16 17:56:45 +00:00
init & add agent & add job
This commit is contained in:
219
predis/src/PubSub/AbstractConsumer.php
Normal file
219
predis/src/PubSub/AbstractConsumer.php
Normal file
@@ -0,0 +1,219 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Predis package.
|
||||
*
|
||||
* (c) Daniele Alessandri <suppakilla@gmail.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Predis\PubSub;
|
||||
|
||||
/**
|
||||
* Base implementation of a PUB/SUB consumer abstraction based on PHP iterators.
|
||||
*
|
||||
* @author Daniele Alessandri <suppakilla@gmail.com>
|
||||
*/
|
||||
abstract class AbstractConsumer implements \Iterator
|
||||
{
|
||||
const SUBSCRIBE = 'subscribe';
|
||||
const UNSUBSCRIBE = 'unsubscribe';
|
||||
const PSUBSCRIBE = 'psubscribe';
|
||||
const PUNSUBSCRIBE = 'punsubscribe';
|
||||
const MESSAGE = 'message';
|
||||
const PMESSAGE = 'pmessage';
|
||||
const PONG = 'pong';
|
||||
|
||||
const STATUS_VALID = 1; // 0b0001
|
||||
const STATUS_SUBSCRIBED = 2; // 0b0010
|
||||
const STATUS_PSUBSCRIBED = 4; // 0b0100
|
||||
|
||||
private $position = null;
|
||||
private $statusFlags = self::STATUS_VALID;
|
||||
|
||||
/**
|
||||
* Automatically stops the consumer when the garbage collector kicks in.
|
||||
*/
|
||||
public function __destruct()
|
||||
{
|
||||
$this->stop(true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the specified flag is valid based on the state of the consumer.
|
||||
*
|
||||
* @param int $value Flag.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
protected function isFlagSet($value)
|
||||
{
|
||||
return ($this->statusFlags & $value) === $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Subscribes to the specified channels.
|
||||
*
|
||||
* @param mixed $channel,... One or more channel names.
|
||||
*/
|
||||
public function subscribe($channel /*, ... */)
|
||||
{
|
||||
$this->writeRequest(self::SUBSCRIBE, func_get_args());
|
||||
$this->statusFlags |= self::STATUS_SUBSCRIBED;
|
||||
}
|
||||
|
||||
/**
|
||||
* Unsubscribes from the specified channels.
|
||||
*
|
||||
* @param string ... One or more channel names.
|
||||
*/
|
||||
public function unsubscribe(/* ... */)
|
||||
{
|
||||
$this->writeRequest(self::UNSUBSCRIBE, func_get_args());
|
||||
}
|
||||
|
||||
/**
|
||||
* Subscribes to the specified channels using a pattern.
|
||||
*
|
||||
* @param mixed $pattern,... One or more channel name patterns.
|
||||
*/
|
||||
public function psubscribe($pattern /* ... */)
|
||||
{
|
||||
$this->writeRequest(self::PSUBSCRIBE, func_get_args());
|
||||
$this->statusFlags |= self::STATUS_PSUBSCRIBED;
|
||||
}
|
||||
|
||||
/**
|
||||
* Unsubscribes from the specified channels using a pattern.
|
||||
*
|
||||
* @param string ... One or more channel name patterns.
|
||||
*/
|
||||
public function punsubscribe(/* ... */)
|
||||
{
|
||||
$this->writeRequest(self::PUNSUBSCRIBE, func_get_args());
|
||||
}
|
||||
|
||||
/**
|
||||
* PING the server with an optional payload that will be echoed as a
|
||||
* PONG message in the pub/sub loop.
|
||||
*
|
||||
* @param string $payload Optional PING payload.
|
||||
*/
|
||||
public function ping($payload = null)
|
||||
{
|
||||
$this->writeRequest('PING', array($payload));
|
||||
}
|
||||
|
||||
/**
|
||||
* Closes the context by unsubscribing from all the subscribed channels. The
|
||||
* context can be forcefully closed by dropping the underlying connection.
|
||||
*
|
||||
* @param bool $drop Indicates if the context should be closed by dropping the connection.
|
||||
*
|
||||
* @return bool Returns false when there are no pending messages.
|
||||
*/
|
||||
public function stop($drop = false)
|
||||
{
|
||||
if (!$this->valid()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ($drop) {
|
||||
$this->invalidate();
|
||||
$this->disconnect();
|
||||
} else {
|
||||
if ($this->isFlagSet(self::STATUS_SUBSCRIBED)) {
|
||||
$this->unsubscribe();
|
||||
}
|
||||
if ($this->isFlagSet(self::STATUS_PSUBSCRIBED)) {
|
||||
$this->punsubscribe();
|
||||
}
|
||||
}
|
||||
|
||||
return !$drop;
|
||||
}
|
||||
|
||||
/**
|
||||
* Closes the underlying connection when forcing a disconnection.
|
||||
*/
|
||||
abstract protected function disconnect();
|
||||
|
||||
/**
|
||||
* Writes a Redis command on the underlying connection.
|
||||
*
|
||||
* @param string $method Command ID.
|
||||
* @param array $arguments Arguments for the command.
|
||||
*/
|
||||
abstract protected function writeRequest($method, $arguments);
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function rewind()
|
||||
{
|
||||
// NOOP
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the last message payload retrieved from the server and generated
|
||||
* by one of the active subscriptions.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function current()
|
||||
{
|
||||
return $this->getValue();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function key()
|
||||
{
|
||||
return $this->position;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function next()
|
||||
{
|
||||
if ($this->valid()) {
|
||||
++$this->position;
|
||||
}
|
||||
|
||||
return $this->position;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the the consumer is still in a valid state to continue.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function valid()
|
||||
{
|
||||
$isValid = $this->isFlagSet(self::STATUS_VALID);
|
||||
$subscriptionFlags = self::STATUS_SUBSCRIBED | self::STATUS_PSUBSCRIBED;
|
||||
$hasSubscriptions = ($this->statusFlags & $subscriptionFlags) > 0;
|
||||
|
||||
return $isValid && $hasSubscriptions;
|
||||
}
|
||||
|
||||
/**
|
||||
* Resets the state of the consumer.
|
||||
*/
|
||||
protected function invalidate()
|
||||
{
|
||||
$this->statusFlags = 0; // 0b0000;
|
||||
}
|
||||
|
||||
/**
|
||||
* Waits for a new message from the server generated by one of the active
|
||||
* subscriptions and returns it when available.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
abstract protected function getValue();
|
||||
}
|
||||
158
predis/src/PubSub/Consumer.php
Normal file
158
predis/src/PubSub/Consumer.php
Normal file
@@ -0,0 +1,158 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Predis package.
|
||||
*
|
||||
* (c) Daniele Alessandri <suppakilla@gmail.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Predis\PubSub;
|
||||
|
||||
use Predis\ClientException;
|
||||
use Predis\ClientInterface;
|
||||
use Predis\Command\Command;
|
||||
use Predis\Connection\AggregateConnectionInterface;
|
||||
use Predis\NotSupportedException;
|
||||
|
||||
/**
|
||||
* PUB/SUB consumer abstraction.
|
||||
*
|
||||
* @author Daniele Alessandri <suppakilla@gmail.com>
|
||||
*/
|
||||
class Consumer extends AbstractConsumer
|
||||
{
|
||||
private $client;
|
||||
private $options;
|
||||
|
||||
/**
|
||||
* @param ClientInterface $client Client instance used by the consumer.
|
||||
* @param array $options Options for the consumer initialization.
|
||||
*/
|
||||
public function __construct(ClientInterface $client, array $options = null)
|
||||
{
|
||||
$this->checkCapabilities($client);
|
||||
|
||||
$this->options = $options ?: array();
|
||||
$this->client = $client;
|
||||
|
||||
$this->genericSubscribeInit('subscribe');
|
||||
$this->genericSubscribeInit('psubscribe');
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the underlying client instance used by the pub/sub iterator.
|
||||
*
|
||||
* @return ClientInterface
|
||||
*/
|
||||
public function getClient()
|
||||
{
|
||||
return $this->client;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the client instance satisfies the required conditions needed to
|
||||
* initialize a PUB/SUB consumer.
|
||||
*
|
||||
* @param ClientInterface $client Client instance used by the consumer.
|
||||
*
|
||||
* @throws NotSupportedException
|
||||
*/
|
||||
private function checkCapabilities(ClientInterface $client)
|
||||
{
|
||||
if ($client->getConnection() instanceof AggregateConnectionInterface) {
|
||||
throw new NotSupportedException(
|
||||
'Cannot initialize a PUB/SUB consumer over aggregate connections.'
|
||||
);
|
||||
}
|
||||
|
||||
$commands = array('publish', 'subscribe', 'unsubscribe', 'psubscribe', 'punsubscribe');
|
||||
|
||||
if ($client->getProfile()->supportsCommands($commands) === false) {
|
||||
throw new NotSupportedException(
|
||||
'The current profile does not support PUB/SUB related commands.'
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This method shares the logic to handle both SUBSCRIBE and PSUBSCRIBE.
|
||||
*
|
||||
* @param string $subscribeAction Type of subscription.
|
||||
*/
|
||||
private function genericSubscribeInit($subscribeAction)
|
||||
{
|
||||
if (isset($this->options[$subscribeAction])) {
|
||||
$this->$subscribeAction($this->options[$subscribeAction]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function writeRequest($method, $arguments)
|
||||
{
|
||||
$this->client->getConnection()->writeRequest(
|
||||
$this->client->createCommand($method,
|
||||
Command::normalizeArguments($arguments)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function disconnect()
|
||||
{
|
||||
$this->client->disconnect();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function getValue()
|
||||
{
|
||||
$response = $this->client->getConnection()->read();
|
||||
|
||||
switch ($response[0]) {
|
||||
case self::SUBSCRIBE:
|
||||
case self::UNSUBSCRIBE:
|
||||
case self::PSUBSCRIBE:
|
||||
case self::PUNSUBSCRIBE:
|
||||
if ($response[2] === 0) {
|
||||
$this->invalidate();
|
||||
}
|
||||
// The missing break here is intentional as we must process
|
||||
// subscriptions and unsubscriptions as standard messages.
|
||||
// no break
|
||||
|
||||
case self::MESSAGE:
|
||||
return (object) array(
|
||||
'kind' => $response[0],
|
||||
'channel' => $response[1],
|
||||
'payload' => $response[2],
|
||||
);
|
||||
|
||||
case self::PMESSAGE:
|
||||
return (object) array(
|
||||
'kind' => $response[0],
|
||||
'pattern' => $response[1],
|
||||
'channel' => $response[2],
|
||||
'payload' => $response[3],
|
||||
);
|
||||
|
||||
case self::PONG:
|
||||
return (object) array(
|
||||
'kind' => $response[0],
|
||||
'payload' => $response[1],
|
||||
);
|
||||
|
||||
default:
|
||||
throw new ClientException(
|
||||
"Unknown message type '{$response[0]}' received in the PUB/SUB context."
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
170
predis/src/PubSub/DispatcherLoop.php
Normal file
170
predis/src/PubSub/DispatcherLoop.php
Normal file
@@ -0,0 +1,170 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Predis package.
|
||||
*
|
||||
* (c) Daniele Alessandri <suppakilla@gmail.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Predis\PubSub;
|
||||
|
||||
/**
|
||||
* Method-dispatcher loop built around the client-side abstraction of a Redis
|
||||
* PUB / SUB context.
|
||||
*
|
||||
* @author Daniele Alessandri <suppakilla@gmail.com>
|
||||
*/
|
||||
class DispatcherLoop
|
||||
{
|
||||
private $pubsub;
|
||||
|
||||
protected $callbacks;
|
||||
protected $defaultCallback;
|
||||
protected $subscriptionCallback;
|
||||
|
||||
/**
|
||||
* @param Consumer $pubsub PubSub consumer instance used by the loop.
|
||||
*/
|
||||
public function __construct(Consumer $pubsub)
|
||||
{
|
||||
$this->callbacks = array();
|
||||
$this->pubsub = $pubsub;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the passed argument is a valid callback.
|
||||
*
|
||||
* @param mixed $callable A callback.
|
||||
*
|
||||
* @throws \InvalidArgumentException
|
||||
*/
|
||||
protected function assertCallback($callable)
|
||||
{
|
||||
if (!is_callable($callable)) {
|
||||
throw new \InvalidArgumentException('The given argument must be a callable object.');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the underlying PUB / SUB context.
|
||||
*
|
||||
* @return Consumer
|
||||
*/
|
||||
public function getPubSubConsumer()
|
||||
{
|
||||
return $this->pubsub;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets a callback that gets invoked upon new subscriptions.
|
||||
*
|
||||
* @param mixed $callable A callback.
|
||||
*/
|
||||
public function subscriptionCallback($callable = null)
|
||||
{
|
||||
if (isset($callable)) {
|
||||
$this->assertCallback($callable);
|
||||
}
|
||||
|
||||
$this->subscriptionCallback = $callable;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets a callback that gets invoked when a message is received on a
|
||||
* channel that does not have an associated callback.
|
||||
*
|
||||
* @param mixed $callable A callback.
|
||||
*/
|
||||
public function defaultCallback($callable = null)
|
||||
{
|
||||
if (isset($callable)) {
|
||||
$this->assertCallback($callable);
|
||||
}
|
||||
|
||||
$this->subscriptionCallback = $callable;
|
||||
}
|
||||
|
||||
/**
|
||||
* Binds a callback to a channel.
|
||||
*
|
||||
* @param string $channel Channel name.
|
||||
* @param callable $callback A callback.
|
||||
*/
|
||||
public function attachCallback($channel, $callback)
|
||||
{
|
||||
$callbackName = $this->getPrefixKeys().$channel;
|
||||
|
||||
$this->assertCallback($callback);
|
||||
$this->callbacks[$callbackName] = $callback;
|
||||
$this->pubsub->subscribe($channel);
|
||||
}
|
||||
|
||||
/**
|
||||
* Stops listening to a channel and removes the associated callback.
|
||||
*
|
||||
* @param string $channel Redis channel.
|
||||
*/
|
||||
public function detachCallback($channel)
|
||||
{
|
||||
$callbackName = $this->getPrefixKeys().$channel;
|
||||
|
||||
if (isset($this->callbacks[$callbackName])) {
|
||||
unset($this->callbacks[$callbackName]);
|
||||
$this->pubsub->unsubscribe($channel);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Starts the dispatcher loop.
|
||||
*/
|
||||
public function run()
|
||||
{
|
||||
foreach ($this->pubsub as $message) {
|
||||
$kind = $message->kind;
|
||||
|
||||
if ($kind !== Consumer::MESSAGE && $kind !== Consumer::PMESSAGE) {
|
||||
if (isset($this->subscriptionCallback)) {
|
||||
$callback = $this->subscriptionCallback;
|
||||
call_user_func($callback, $message);
|
||||
}
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
if (isset($this->callbacks[$message->channel])) {
|
||||
$callback = $this->callbacks[$message->channel];
|
||||
call_user_func($callback, $message->payload);
|
||||
} elseif (isset($this->defaultCallback)) {
|
||||
$callback = $this->defaultCallback;
|
||||
call_user_func($callback, $message);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Terminates the dispatcher loop.
|
||||
*/
|
||||
public function stop()
|
||||
{
|
||||
$this->pubsub->stop();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the prefix used for keys.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function getPrefixKeys()
|
||||
{
|
||||
$options = $this->pubsub->getClient()->getOptions();
|
||||
|
||||
if (isset($options->prefix)) {
|
||||
return $options->prefix->getPrefix();
|
||||
}
|
||||
|
||||
return '';
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user