mirror of
https://github.com/RaspbianFrance/raspisms.git
synced 2025-04-20 16:37:48 +02:00
Add support for Redis in addition to System V queues
This commit is contained in:
parent
5c697b5240
commit
36c5d7ec0c
11 changed files with 407 additions and 93 deletions
86
controllers/internals/Queue.php
Normal file
86
controllers/internals/Queue.php
Normal file
|
@ -0,0 +1,86 @@
|
|||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of RaspiSMS.
|
||||
*
|
||||
* (c) Pierre-Lin Bonnemaison <plebwebsas@gmail.com>
|
||||
*
|
||||
* This source file is subject to the GPL-3.0 license that is bundled
|
||||
* with this source code in the file LICENSE.
|
||||
*/
|
||||
|
||||
namespace controllers\internals;
|
||||
|
||||
use Exception;
|
||||
use models\RedisQueue;
|
||||
use models\SystemVQueue;
|
||||
|
||||
class Queue extends \descartes\InternalController
|
||||
{
|
||||
private $queue;
|
||||
|
||||
/**
|
||||
* A class to interact with queue, the class is in charge to choose the type of queue (redis/system v) to use
|
||||
*/
|
||||
public function __construct($id)
|
||||
{
|
||||
if (USE_REDIS_QUEUES ?? false)
|
||||
{
|
||||
$params = [];
|
||||
if (REDIS_HOST ?? false)
|
||||
{
|
||||
$params['host'] = REDIS_HOST;
|
||||
}
|
||||
|
||||
if (REDIS_PORT ?? false)
|
||||
{
|
||||
$params['port'] = REDIS_PORT;
|
||||
}
|
||||
|
||||
if (REDIS_PASSWORD ?? false)
|
||||
{
|
||||
$params['auth'] = REDIS_PASSWORD;
|
||||
}
|
||||
|
||||
$this->queue = new RedisQueue($id, $params, 'raspisms', 'raspisms');
|
||||
}
|
||||
else
|
||||
{
|
||||
$this->queue = new SystemVQueue($id);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a message to the queue
|
||||
*
|
||||
* @param string $message : The message to add to the queue
|
||||
* @param ?string $tag : A tag to associate to the message for routing purposes, if null will add to general queue
|
||||
*/
|
||||
public function push($message, ?string $tag = null)
|
||||
{
|
||||
return $this->queue->push($message, $tag);
|
||||
}
|
||||
|
||||
/**
|
||||
* Read the older message in the queue
|
||||
*
|
||||
* @return mixed $message : The oldest message or null if no message found, can be anything
|
||||
* @param ?string $tag : A tag to associate to the message for routing purposes, if null will read from general queue
|
||||
* @param mixed : The message to add to the queue, can be anything, the queue will have to treat it by itself
|
||||
*/
|
||||
public function read(?string $tag = null)
|
||||
{
|
||||
return $this->queue->read($tag);
|
||||
}
|
||||
|
||||
/**
|
||||
* Function to close system V queue for cleaning resources, usefull only if system V queue
|
||||
*/
|
||||
public function close()
|
||||
{
|
||||
if ($this->queue instanceof SystemVQueue)
|
||||
{
|
||||
$this->queue->close();
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue