mirror of
https://github.com/RaspbianFrance/raspisms.git
synced 2025-04-22 09:26:27 +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
|
@ -11,6 +11,7 @@
|
|||
|
||||
namespace daemons;
|
||||
|
||||
use controllers\internals\Queue;
|
||||
use Monolog\Handler\StreamHandler;
|
||||
use Monolog\Logger;
|
||||
|
||||
|
@ -19,7 +20,7 @@ use Monolog\Logger;
|
|||
*/
|
||||
class Mailer extends AbstractDaemon
|
||||
{
|
||||
private $mailer_queue;
|
||||
private ?Queue $mailer_queue;
|
||||
private $last_message_at;
|
||||
private $bdd;
|
||||
|
||||
|
@ -49,27 +50,15 @@ class Mailer extends AbstractDaemon
|
|||
$find_message = true;
|
||||
while ($find_message)
|
||||
{
|
||||
//Call message
|
||||
$msgtype = null;
|
||||
$maxsize = 409600;
|
||||
$message = null;
|
||||
$message = $this->mailer_queue->read(QUEUE_TYPE_EMAIL);
|
||||
|
||||
$error_code = null;
|
||||
$success = msg_receive($this->mailer_queue, QUEUE_TYPE_EMAIL, $msgtype, $maxsize, $message, true, MSG_IPC_NOWAIT, $error_code); //MSG_IPC_NOWAIT == dont wait if no message found
|
||||
if (!$success && MSG_ENOMSG !== $error_code)
|
||||
if ($message === null)
|
||||
{
|
||||
$this->logger->critical('Error for mailer queue reading, error code : ' . $error_code);
|
||||
$find_message = false;
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!$message)
|
||||
{
|
||||
$find_message = false;
|
||||
|
||||
continue;
|
||||
}
|
||||
$message = json_decode($message, true);
|
||||
|
||||
$this->logger->info('Try sending email : ' . json_encode($message));
|
||||
|
||||
|
@ -92,7 +81,7 @@ class Mailer extends AbstractDaemon
|
|||
public function on_start()
|
||||
{
|
||||
//Set last message at to construct time
|
||||
$this->mailer_queue = msg_get_queue(QUEUE_ID_EMAIL);
|
||||
$this->mailer_queue = new Queue(QUEUE_ID_EMAIL);
|
||||
|
||||
$this->logger->info('Starting Mailer daemon with pid ' . getmypid());
|
||||
}
|
||||
|
@ -101,8 +90,6 @@ class Mailer extends AbstractDaemon
|
|||
{
|
||||
//Delete queue on daemon close
|
||||
$this->logger->info('Closing queue : ' . QUEUE_ID_EMAIL);
|
||||
msg_remove_queue($this->mailer_queue);
|
||||
|
||||
$this->logger->info('Stopping Mailer daemon with pid ' . getmypid());
|
||||
}
|
||||
|
||||
|
|
|
@ -11,6 +11,7 @@
|
|||
|
||||
namespace daemons;
|
||||
|
||||
use controllers\internals\Queue;
|
||||
use Monolog\Handler\StreamHandler;
|
||||
use Monolog\Logger;
|
||||
|
||||
|
@ -22,7 +23,7 @@ class Phone extends AbstractDaemon
|
|||
private $max_inactivity = 5 * 60;
|
||||
private $read_delay = 20 / 0.5;
|
||||
private $read_tick = 0;
|
||||
private $msg_queue;
|
||||
private ?Queue $queue;
|
||||
private $webhook_queue;
|
||||
private $last_message_at;
|
||||
private $phone;
|
||||
|
@ -85,7 +86,7 @@ class Phone extends AbstractDaemon
|
|||
//Set last message at to construct time
|
||||
$this->last_message_at = microtime(true);
|
||||
|
||||
$this->msg_queue = msg_get_queue(QUEUE_ID_PHONE);
|
||||
$this->queue = new Queue(QUEUE_ID_PHONE);
|
||||
|
||||
//Instanciate adapter
|
||||
$adapter_class = $this->phone['adapter'];
|
||||
|
@ -96,7 +97,7 @@ class Phone extends AbstractDaemon
|
|||
|
||||
public function on_stop()
|
||||
{
|
||||
$this->logger->info('Stopping Phone daemon with pid ' . getmypid());
|
||||
$this->logger->info('Stopping Phone daemon with pid ' . getmypid());
|
||||
}
|
||||
|
||||
public function handle_other_signals($signal)
|
||||
|
@ -114,30 +115,17 @@ class Phone extends AbstractDaemon
|
|||
$find_message = true;
|
||||
while ($find_message)
|
||||
{
|
||||
//Call message
|
||||
$msgtype = null;
|
||||
$maxsize = 409600;
|
||||
$message = null;
|
||||
|
||||
// Message type is forged from a prefix concat with the phone ID
|
||||
$message_type = (int) QUEUE_TYPE_SEND_MSG_PREFIX . $this->phone['id'];
|
||||
$error_code = null;
|
||||
$success = msg_receive($this->msg_queue, $message_type, $msgtype, $maxsize, $message, true, MSG_IPC_NOWAIT, $error_code); //MSG_IPC_NOWAIT == dont wait if no message found
|
||||
$message = $this->queue->read($message_type);
|
||||
|
||||
if (!$success && MSG_ENOMSG !== $error_code)
|
||||
{
|
||||
$this->logger->critical('Error reading MSG SEND Queue, error code : ' . $error_code);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!$message)
|
||||
if ($message === null)
|
||||
{
|
||||
$find_message = false;
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
$message = json_decode($message, true);
|
||||
|
||||
//Update last message time
|
||||
$this->last_message_at = microtime(true);
|
||||
|
||||
|
|
|
@ -11,6 +11,8 @@
|
|||
|
||||
namespace daemons;
|
||||
|
||||
use controllers\internals\Queue;
|
||||
use Exception;
|
||||
use Monolog\Handler\StreamHandler;
|
||||
use Monolog\Logger;
|
||||
|
||||
|
@ -19,12 +21,9 @@ use Monolog\Logger;
|
|||
*/
|
||||
class Sender extends AbstractDaemon
|
||||
{
|
||||
private $internal_phone;
|
||||
private $internal_scheduled;
|
||||
private $internal_received;
|
||||
private $internal_sended;
|
||||
private $bdd;
|
||||
private $msg_queue;
|
||||
private ?Queue $queue;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
|
@ -44,9 +43,7 @@ class Sender extends AbstractDaemon
|
|||
|
||||
public function run()
|
||||
{
|
||||
//Create the internal controllers
|
||||
$this->internal_scheduled = new \controllers\internals\Scheduled($this->bdd);
|
||||
$this->internal_sended = new \controllers\internals\Sended($this->bdd);
|
||||
|
||||
//Get smss and transmit order to send to appropriate phone daemon
|
||||
$smss_per_scheduled = $this->internal_scheduled->get_smss_to_send();
|
||||
|
@ -64,12 +61,6 @@ class Sender extends AbstractDaemon
|
|||
{
|
||||
foreach ($smss_per_scheduled as $id_scheduled => $smss)
|
||||
{
|
||||
//If queue not already exists
|
||||
if (!msg_queue_exists(QUEUE_ID_PHONE) || !isset($this->msg_queue))
|
||||
{
|
||||
$this->msg_queue = msg_get_queue(QUEUE_ID_PHONE);
|
||||
}
|
||||
|
||||
foreach ($smss as $sms)
|
||||
{
|
||||
$msg = [
|
||||
|
@ -84,9 +75,10 @@ class Sender extends AbstractDaemon
|
|||
'medias' => $sms['medias'] ?? [],
|
||||
];
|
||||
|
||||
|
||||
// Message type is forged from a prefix concat with the phone ID
|
||||
$message_type = (int) QUEUE_TYPE_SEND_MSG_PREFIX . $sms['id_phone'];
|
||||
msg_send($this->msg_queue, $message_type, $msg);
|
||||
$this->queue->push(json_encode($msg), $message_type);
|
||||
$this->logger->info('Transmit sms send signal to phone ' . $sms['id_phone'] . ' on queue ' . QUEUE_ID_PHONE . ' with message type ' . $message_type . '.');
|
||||
}
|
||||
|
||||
|
@ -97,16 +89,25 @@ class Sender extends AbstractDaemon
|
|||
|
||||
public function on_start()
|
||||
{
|
||||
$this->logger->info('Starting Sender with pid ' . getmypid());
|
||||
$this->bdd = \descartes\Model::_connect(DATABASE_HOST, DATABASE_NAME, DATABASE_USER, DATABASE_PASSWORD);
|
||||
try
|
||||
{
|
||||
$this->logger->info('Starting Sender with pid ' . getmypid());
|
||||
$this->bdd = \descartes\Model::_connect(DATABASE_HOST, DATABASE_NAME, DATABASE_USER, DATABASE_PASSWORD);
|
||||
$this->queue = new Queue(QUEUE_ID_PHONE);
|
||||
}
|
||||
catch (Exception $e)
|
||||
{
|
||||
$this->logger->error('Failed to start sender daemon : ' . $e->getMessage());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public function on_stop()
|
||||
{
|
||||
//Delete queue on daemon close
|
||||
$this->logger->info('Closing queue : ' . $this->msg_queue);
|
||||
msg_remove_queue($this->msg_queue);
|
||||
|
||||
$this->logger->info('Closing queue : ' . QUEUE_ID_PHONE);
|
||||
$this->queue->close();
|
||||
|
||||
$this->logger->info('Stopping Sender with pid ' . getmypid());
|
||||
}
|
||||
|
||||
|
|
|
@ -11,6 +11,7 @@
|
|||
|
||||
namespace daemons;
|
||||
|
||||
use controllers\internals\Queue;
|
||||
use GuzzleHttp\Promise\Utils;
|
||||
use Monolog\Handler\StreamHandler;
|
||||
use Monolog\Logger;
|
||||
|
@ -20,11 +21,7 @@ use Monolog\Logger;
|
|||
*/
|
||||
class Webhook extends AbstractDaemon
|
||||
{
|
||||
private $webhook_queue;
|
||||
private $last_message_at;
|
||||
private $phone;
|
||||
private $adapter;
|
||||
private $bdd;
|
||||
private ?Queue $webhook_queue;
|
||||
private $guzzle_client;
|
||||
|
||||
/**
|
||||
|
@ -56,30 +53,17 @@ class Webhook extends AbstractDaemon
|
|||
$promises = [];
|
||||
while ($find_message)
|
||||
{
|
||||
//Call message
|
||||
$msgtype = null;
|
||||
$maxsize = 409600;
|
||||
$message = null;
|
||||
$message = $this->webhook_queue->read(QUEUE_TYPE_WEBHOOK);
|
||||
|
||||
$error_code = null;
|
||||
$success = msg_receive($this->webhook_queue, QUEUE_TYPE_WEBHOOK, $msgtype, $maxsize, $message, true, MSG_IPC_NOWAIT, $error_code); //MSG_IPC_NOWAIT == dont wait if no message found
|
||||
if (!$success && MSG_ENOMSG !== $error_code)
|
||||
if ($message === null)
|
||||
{
|
||||
$this->logger->critical('Error for webhook queue reading, error code : ' . $error_code);
|
||||
$find_message = false;
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!$message)
|
||||
{
|
||||
$find_message = false;
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
$this->logger->info('Trigger webhook : ' . json_encode($message));
|
||||
$this->logger->info('Trigger webhook : ' . $message);
|
||||
|
||||
$message = json_decode($message, true);
|
||||
$promises[] = $this->guzzle_client->postAsync($message['url'], ['form_params' => $message['data']]);
|
||||
}
|
||||
|
||||
|
@ -97,10 +81,13 @@ class Webhook extends AbstractDaemon
|
|||
|
||||
public function on_start()
|
||||
{
|
||||
//Set last message at to construct time
|
||||
$this->last_message_at = microtime(true);
|
||||
|
||||
$this->webhook_queue = msg_get_queue(QUEUE_ID_WEBHOOK);
|
||||
try{
|
||||
$this->webhook_queue = new Queue(QUEUE_ID_WEBHOOK);
|
||||
}
|
||||
catch (\Exception $e)
|
||||
{
|
||||
$this->logger->info('Webhook : failed with ' . $e->getMessage());
|
||||
}
|
||||
|
||||
$this->logger->info('Starting Webhook daemon with pid ' . getmypid());
|
||||
}
|
||||
|
@ -109,7 +96,7 @@ class Webhook extends AbstractDaemon
|
|||
{
|
||||
//Delete queue on daemon close
|
||||
$this->logger->info('Closing queue : ' . QUEUE_ID_WEBHOOK);
|
||||
msg_remove_queue($this->webhook_queue);
|
||||
unset($this->webhook_queue);
|
||||
|
||||
$this->logger->info('Stopping Webhook daemon with pid ' . getmypid());
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue