Fix php style

This commit is contained in:
osaajani 2020-01-17 18:19:25 +01:00
parent 461bd9c98d
commit b8bd067dc7
59 changed files with 2307 additions and 1868 deletions

View file

@ -1,35 +1,44 @@
<?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 daemons;
/**
* Class defining the global structur of a Linux Daemon
* Class defining the global structur of a Linux Daemon.
*/
abstract class AbstractDaemon
{
protected $name;
protected $uniq;
protected $name;
protected $uniq;
protected $logger;
protected $no_parent;
private $is_running = true;
private $signals = array (
private $is_running = true;
private $signals = [
SIGTERM,
SIGINT,
SIGCHLD,
SIGHUP
);
SIGHUP,
];
/**
* Class used to handle POSIX signals and fork from the current process
*
* @param string $name : The name of the class
* @param object $logger : A PSR3 logger instance
* @param string $pid_dir : Directory for the pid files
* @param bool $no_parent : Should the daemon be disconnected from his parent process
* @param array $signals :An array containing additional POSIX signals to handle [optionel]
* @param bool $uniq : Must the process be uniq ?
*/
protected function __construct (string $name, object $logger, string $pid_dir = '/var/run', $no_parent = false, array $signals = [], bool $uniq = false)
/**
* Class used to handle POSIX signals and fork from the current process.
*
* @param string $name : The name of the class
* @param object $logger : A PSR3 logger instance
* @param string $pid_dir : Directory for the pid files
* @param bool $no_parent : Should the daemon be disconnected from his parent process
* @param array $signals :An array containing additional POSIX signals to handle [optionel]
* @param bool $uniq : Must the process be uniq ?
*/
protected function __construct(string $name, object $logger, string $pid_dir = '/var/run', $no_parent = false, array $signals = [], bool $uniq = false)
{
$this->name = $name;
$this->logger = $logger;
@ -42,97 +51,85 @@ abstract class AbstractDaemon
set_time_limit(0);
//Register signals
$this->register_signals();
$this->register_signals();
}
/**
* Used to register POSIX signals
*/
private function register_signals()
/**
* True if the daemon is running.
*/
public function is_running()
{
//Enable a tick at every 1 instruction, allowing us to run a function frequently, for exemple looking at signal status
declare(ticks = 1);
foreach ($this->signals as $signal)
{
//For each signal define the method handle_signal of the current class as the way to handle it
@pcntl_signal($signal, [
'self',
'handle_signal'
]);
}
return $this->is_running;
}
/**
* Used to handle properly SIGINT, SIGTERM, SIGCHLD and SIGHUP
*
* @param int $signal
/**
* Used to handle properly SIGINT, SIGTERM, SIGCHLD and SIGHUP.
*
* @param int $signal
* @param mixed $signinfo
*/
*/
protected function handle_signal(int $signal, $signinfo)
{
if ($signal == SIGTERM || $signal == SIGINT) //Stop the daemon
{
if (SIGTERM === $signal || SIGINT === $signal)
{ //Stop the daemon
$this->is_running = false;
}
else if ($signal == SIGHUP) //Restart the daemon
{
$this->on_stop();
$this->on_start();
elseif (SIGHUP === $signal)
{ //Restart the daemon
$this->on_stop();
$this->on_start();
}
else if ($signal == SIGCHLD) //On daemon child stopping
{
elseif (SIGCHLD === $signal)
{ //On daemon child stopping
pcntl_waitpid(-1, $status, WNOHANG);
}
else //All the other signals
{
$this->handle_other_signals($signal);
}
else
{ //All the other signals
$this->handle_other_signals($signal);
}
}
/**
* Launch the infinite loop executing the "run" abstract method
*/
protected function start ()
/**
* Launch the infinite loop executing the "run" abstract method.
*/
protected function start()
{
//If process must be uniq and a process with the same pid file is already running
if (file_exists($this->pid_dir . '/' . $this->name . '.pid') && $this->uniq)
if (file_exists($this->pid_dir.'/'.$this->name.'.pid') && $this->uniq)
{
$this->logger->info("Another process named " . $this->name . " is already running.");
$this->logger->info('Another process named '.$this->name.' is already running.');
return false;
}
//If we must make the daemon independant from any parent, we do a fork and die operation
if ($this->no_parent)
{
$pid = pcntl_fork(); //Fork current process into a child, so we can kill current process and keep only the child with parent PID = 1
if ($pid == -1) //Impossible to run script
{
$this->logger->critical("Impossible to create a subprocess.");
if (-1 === $pid)
{ //Impossible to run script
$this->logger->critical('Impossible to create a subprocess.');
return false;
}
elseif ($pid) //Current script
{
if ($pid)
{ //Current script
return true;
}
$this->logger->info("Process $this->name started as a child with pid " . getmypid() . ".");
$this->logger->info("Process {$this->name} started as a child with pid ".getmypid().'.');
//Child script
$sid = posix_setsid(); //Try to make the child process a main process
if ($sid == -1) //Error
{
$this->logger->critical("Cannot make the child process with pid $pid independent.");
if (-1 === $sid)
{ //Error
$this->logger->critical("Cannot make the child process with pid {$pid} independent.");
exit(1);
}
$this->logger->info("The child process with pid " . getmypid() . " is now independent.");
}
$this->logger->info('The child process with pid '.getmypid().' is now independent.');
}
//Create pid dir if not exists
if (!file_exists($this->pid_dir))
@ -140,23 +137,21 @@ abstract class AbstractDaemon
$success = mkdir($this->pid_dir, 0777, true);
if (!$success)
{
$this->logger->critical('Cannot create PID directory : ' . $this->pid_dir);
$this->logger->critical('Cannot create PID directory : '.$this->pid_dir);
exit(2);
}
}
//Set process name
cli_set_process_title($this->name);
//Write the pid of the process into a file
file_put_contents($this->pid_dir . '/' . $this->name . '.pid', getmypid());
file_put_contents($this->pid_dir.'/'.$this->name.'.pid', getmypid());
//Really start the daemon
$this->on_start();
try
try
{
while ($this->is_running)
{
@ -166,51 +161,56 @@ abstract class AbstractDaemon
}
catch (\Exception $e)
{
$this->logger->critical('Exception : ' . $e->getMessage() . " in " . $e->getFile() . " line " . $e->getLine());
$this->logger->critical('Exception : '.$e->getMessage().' in '.$e->getFile().' line '.$e->getLine());
}
//Stop the daemon
//Stop the daemon
$this->on_stop();
//Delete pid file
if (file_exists($this->pid_dir . '/' . $this->name . '.pid'))
if (file_exists($this->pid_dir.'/'.$this->name.'.pid'))
{
unlink($this->pid_dir . '/' . $this->name . '.pid');
unlink($this->pid_dir.'/'.$this->name.'.pid');
}
}
/**
* True if the daemon is running
*/
public function is_running()
{
return $this->is_running;
}
/**
* Override to implement the code that run infinetly (actually, it run one time but repeat the operation infinetly
*/
protected abstract function run();
/**
* Override to execute code before the ''run'' method on daemon start
*/
protected abstract function on_start();
/**
* Override to execute code after the ''run'' method on daemon shutdown
*/
protected abstract function on_stop();
/**
* Override to handle additional POSIX signals
*
* @param int $signal : Signal sent by interrupt
*/
protected abstract function handle_other_signals(int $signal);
* Override to implement the code that run infinetly (actually, it run one time but repeat the operation infinetly.
*/
abstract protected function run();
/**
* Override to execute code before the ''run'' method on daemon start.
*/
abstract protected function on_start();
/**
* Override to execute code after the ''run'' method on daemon shutdown.
*/
abstract protected function on_stop();
/**
* Override to handle additional POSIX signals.
*
* @param int $signal : Signal sent by interrupt
*/
abstract protected function handle_other_signals(int $signal);
/**
* Used to register POSIX signals.
*/
private function register_signals()
{
//Enable a tick at every 1 instruction, allowing us to run a function frequently, for exemple looking at signal status
declare(ticks=1);
foreach ($this->signals as $signal)
{
//For each signal define the method handle_signal of the current class as the way to handle it
@pcntl_signal($signal, [
'self',
'handle_signal',
]);
}
}
}

View file

@ -1,11 +1,21 @@
<?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 daemons;
use \Monolog\Logger;
use \Monolog\Handler\StreamHandler;
use Monolog\Handler\StreamHandler;
use Monolog\Logger;
/**
* Main daemon class
* Main daemon class.
*/
class Launcher extends AbstractDaemon
{
@ -16,10 +26,10 @@ class Launcher extends AbstractDaemon
public function __construct()
{
$name = "RaspiSMS Daemon Launcher";
$name = 'RaspiSMS Daemon Launcher';
$logger = new Logger($name);
$logger->pushHandler(new StreamHandler(PWD_LOGS . '/raspisms.log', Logger::DEBUG));
$logger->pushHandler(new StreamHandler(PWD_LOGS.'/raspisms.log', Logger::DEBUG));
$pid_dir = PWD_PID;
$no_parent = true; //Launcher should be rattach to PID 1
$additional_signals = [];
@ -31,14 +41,12 @@ class Launcher extends AbstractDaemon
parent::start();
}
public function run()
{
//Create the internal controllers
$this->bdd = \descartes\Model::_connect(DATABASE_HOST, DATABASE_NAME, DATABASE_USER, DATABASE_PASSWORD, 'UTF8');
$this->internal_phone = new \controllers\internals\Phone($this->bdd);
$this->start_sender_daemon();
$this->start_webhook_daemon();
@ -48,17 +56,15 @@ class Launcher extends AbstractDaemon
sleep(1);
}
/**
* Function to start sender daemon
* @return void
* Function to start sender daemon.
*/
public function start_sender_daemon ()
public function start_sender_daemon()
{
$name = 'RaspiSMS Daemon Sender';
$pid_file = PWD_PID . '/' . $name . '.pid';
$pid_file = PWD_PID.'/'.$name.'.pid';
if (file_exists($pid_file))
{
return false;
@ -66,66 +72,60 @@ class Launcher extends AbstractDaemon
//Create a new daemon for sender
$pid = null;
exec('php ' . PWD . '/console.php controllers/internals/Console.php sender > /dev/null 2>&1 &');
exec('php '.PWD.'/console.php controllers/internals/Console.php sender > /dev/null 2>&1 &');
}
/**
* Function to start webhook daemon
* @return void
* Function to start webhook daemon.
*/
public function start_webhook_daemon ()
public function start_webhook_daemon()
{
$name = 'RaspiSMS Daemon Webhook';
$pid_file = PWD_PID . '/' . $name . '.pid';
$pid_file = PWD_PID.'/'.$name.'.pid';
if (file_exists($pid_file))
{
return false;
}
//Create a new daemon for webhook
exec('php ' . PWD . '/console.php controllers/internals/Console.php webhook > /dev/null 2>&1 &');
exec('php '.PWD.'/console.php controllers/internals/Console.php webhook > /dev/null 2>&1 &');
}
/**
* Function to start phones daemons
* Function to start phones daemons.
*
* @param array $phones : Phones to start daemon for if the daemon is not already started
* @return void
*/
public function start_phones_daemons (array $phones)
public function start_phones_daemons(array $phones)
{
foreach ($phones as $phone)
{
$phone_name = 'RaspiSMS Daemon Phone ' . $phone['number'];
$pid_file = PWD_PID . '/' . $phone_name . '.pid';
$phone_name = 'RaspiSMS Daemon Phone '.$phone['number'];
$pid_file = PWD_PID.'/'.$phone_name.'.pid';
if (file_exists($pid_file))
{
continue;
}
//Create a new daemon for the phone
exec('php ' . PWD . '/console.php controllers/internals/Console.php phone --id_phone=\'' . $phone['id'] . '\' > /dev/null 2>&1 &');
exec('php '.PWD.'/console.php controllers/internals/Console.php phone --id_phone=\''.$phone['id'].'\' > /dev/null 2>&1 &');
}
}
public function on_start()
{
$this->logger->info("Starting Launcher with pid " . getmypid());
$this->logger->info('Starting Launcher with pid '.getmypid());
}
public function on_stop()
public function on_stop()
{
$this->logger->info("Stopping Launcher with pid " . getmypid ());
$this->logger->info('Stopping Launcher with pid '.getmypid());
}
public function handle_other_signals($signal)
{
$this->logger->info("Signal not handled by " . $this->name . " Daemon : " . $signal);
$this->logger->info('Signal not handled by '.$this->name.' Daemon : '.$signal);
}
}

View file

@ -1,11 +1,21 @@
<?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 daemons;
use \Monolog\Logger;
use \Monolog\Handler\StreamHandler;
use Monolog\Handler\StreamHandler;
use Monolog\Logger;
/**
* Phone daemon class
* Phone daemon class.
*/
class Phone extends AbstractDaemon
{
@ -18,17 +28,18 @@ class Phone extends AbstractDaemon
private $bdd;
/**
* Constructor
* Constructor.
*
* @param array $phone : A phone table entry
*/
public function __construct(array $phone)
{
$this->phone = $phone;
$this->msg_queue_id = (int) mb_substr($this->phone['number'], 1);
$name = 'RaspiSMS Daemon Phone ' . $this->phone['number'];
$name = 'RaspiSMS Daemon Phone '.$this->phone['number'];
$logger = new Logger($name);
$logger->pushHandler(new StreamHandler(PWD_LOGS . '/raspisms.log', Logger::DEBUG));
$logger->pushHandler(new StreamHandler(PWD_LOGS.'/raspisms.log', Logger::DEBUG));
$pid_dir = PWD_PID;
$no_parent = false; //Phone should be rattach to manager, so manager can stop him easily
$additional_signals = [];
@ -36,22 +47,21 @@ class Phone extends AbstractDaemon
//Construct the daemon
parent::__construct($name, $logger, $pid_dir, $no_parent, $additional_signals, $uniq);
parent::start();
}
public function run()
{
//Stop after 5 minutes of inactivity to avoid useless daemon
if ( (microtime(true) - $this->last_message_at) > 5 * 60 )
if ((microtime(true) - $this->last_message_at) > 5 * 60)
{
posix_kill(getmypid(), SIGTERM); //Send exit signal to the current process
return false;
}
$this->bdd = \descartes\Model::_connect(DATABASE_HOST, DATABASE_NAME, DATABASE_USER, DATABASE_PASSWORD, 'UTF8');
//Send smss in queue
$this->send_smss();
@ -61,37 +71,67 @@ class Phone extends AbstractDaemon
usleep(0.5 * 1000000);
}
public function on_start()
{
//Set last message at to construct time
$this->last_message_at = microtime(true);
$this->msg_queue = msg_get_queue($this->msg_queue_id);
$this->webhook_queue = msg_get_queue(QUEUE_ID_WEBHOOK);
//Instanciate adapter
$adapter_class = $this->phone['adapter'];
$this->adapter = new $adapter_class($this->phone['number'], $this->phone['adapter_datas']);
$this->logger->info('Starting Phone daemon with pid '.getmypid());
}
public function on_stop()
{
//Delete queue on daemon close
$this->logger->info('Closing queue : '.$this->msg_queue_id);
msg_remove_queue($this->msg_queue);
$this->logger->info('Stopping Phone daemon with pid '.getmypid());
}
public function handle_other_signals($signal)
{
$this->logger->info('Signal not handled by '.$this->name.' Daemon : '.$signal);
}
/**
* Send sms
* Send sms.
*/
private function send_smss ()
private function send_smss()
{
$find_message = true;
while ($find_message)
{
//Call message
//Call message
$msgtype = null;
$maxsize = 409600;
$message = null;
$error_code = null;
$success = msg_receive($this->msg_queue, QUEUE_TYPE_SEND_MSG, $msgtype, $maxsize, $message, TRUE, MSG_IPC_NOWAIT, $error_code); //MSG_IPC_NOWAIT == dont wait if no message found
$success = msg_receive($this->msg_queue, QUEUE_TYPE_SEND_MSG, $msgtype, $maxsize, $message, true, MSG_IPC_NOWAIT, $error_code); //MSG_IPC_NOWAIT == dont wait if no message found
if (!$success && $error_code !== MSG_ENOMSG)
if (!$success && MSG_ENOMSG !== $error_code)
{
$this->logger->critical('Error reading MSG SEND Queue, error code : ' . $error);
$this->logger->critical('Error reading MSG SEND Queue, error code : '.$error);
return false;
}
if (!$message)
{
$find_message = false;
continue;
}
$internal_sended = new \controllers\internals\Sended($this->bdd);
//Update last message time
$this->last_message_at = microtime(true);
@ -99,14 +139,15 @@ class Phone extends AbstractDaemon
$at = $now->format('Y-m-d H:i:s');
$message['at'] = $at;
$this->logger->info('Try send message : ' . json_encode($message));
$this->logger->info('Try send message : '.json_encode($message));
$sended_sms_uid = $this->adapter->send($message['destination'], $message['text'], $message['flash']);
if (!$sended_sms_uid)
{
$this->logger->error('Failed send message : ' . json_encode($message));
$this->logger->error('Failed send message : '.json_encode($message));
$internal_sended->create($at, $message['text'], $message['origin'], $message['destination'], $sended_sms_uid, $this->phone['adapter'], $message['flash'], 'failed');
continue;
}
@ -115,21 +156,20 @@ class Phone extends AbstractDaemon
$user_settings = $internal_setting->gets_for_user($this->phone['id_user']);
$this->process_for_webhook($message, 'send_sms', $user_settings);
$this->logger->info('Successfully send message : ' . json_encode($message));
$this->logger->info('Successfully send message : '.json_encode($message));
$internal_sended->create($at, $message['text'], $message['origin'], $message['destination'], $sended_sms_uid, $this->phone['adapter'], $message['flash']);
}
}
/**
* Read smss for a number
* Read smss for a number.
*/
private function read_smss ()
private function read_smss()
{
$internal_received = new \controllers\internals\Received($this->bdd);
$internal_setting = new \controllers\internals\Setting($this->bdd);
$smss = $this->adapter->read();
if (!$smss)
{
@ -142,7 +182,7 @@ class Phone extends AbstractDaemon
//Process smss
foreach ($smss as $sms)
{
$this->logger->info('Receive message : ' . json_encode($sms));
$this->logger->info('Receive message : '.json_encode($sms));
$command_result = $this->process_for_command($sms);
$this->logger->info('after command');
@ -156,17 +196,18 @@ class Phone extends AbstractDaemon
$internal_received->create($sms['at'], $sms['text'], $sms['origin'], $sms['destination'], 'unread', $is_command);
}
}
/**
* Process a sms to find if its a command and so execute it
* Process a sms to find if its a command and so execute it.
*
* @param array $sms : The sms
*
* @return array : ['text' => new sms text, 'is_command' => bool]
*/
private function process_for_command (array $sms)
private function process_for_command(array $sms)
{
$internal_command = new \controllers\internals\Command($this->bdd);
$is_command = false;
$command = $internal_command->check_for_command($this->phone['id_user'], $sms['text']);
if ($command)
@ -179,22 +220,22 @@ class Phone extends AbstractDaemon
return ['text' => $sms['text'], 'is_command' => $is_command];
}
/**
* Process a sms to transmit a webhook query to webhook daemon if needed
* @param array $sms : The sms
* @param string $webhook_type : Type of webhook to trigger
* @param array $user_settings : Use settings
* Process a sms to transmit a webhook query to webhook daemon if needed.
*
* @param array $sms : The sms
* @param string $webhook_type : Type of webhook to trigger
* @param array $user_settings : Use settings
*/
private function process_for_webhook (array $sms, string $webhook_type, array $user_settings)
private function process_for_webhook(array $sms, string $webhook_type, array $user_settings)
{
if (!$user_settings['webhook'])
{
return false;
}
$internal_webhook = new \controllers\internals\Webhook($this->bdd);
$webhooks = $internal_webhook->gets_for_type_and_user($this->phone['id_user'], $webhook_type);
foreach ($webhooks as $webhook)
{
@ -210,21 +251,21 @@ class Phone extends AbstractDaemon
];
$error_code = null;
$success = msg_send($this->webhook_queue, QUEUE_TYPE_WEBHOOK, $message, TRUE, TRUE, $error_code);
$success = msg_send($this->webhook_queue, QUEUE_TYPE_WEBHOOK, $message, true, true, $error_code);
if (!$success)
{
$this->logger->critical("Failed send webhook message in queue, error code : " . $error_code);
$this->logger->critical('Failed send webhook message in queue, error code : '.$error_code);
}
}
}
/**
* Process a sms to transfer it by mail
* @param array $sms : The sms
* Process a sms to transfer it by mail.
*
* @param array $sms : The sms
* @param array $user_settings : Use settings
*/
private function process_for_transfer (array $sms, array $user_settings)
private function process_for_transfer(array $sms, array $user_settings)
{
if (!$user_settings['transfer'])
{
@ -238,41 +279,9 @@ class Phone extends AbstractDaemon
{
return false;
}
$this->logger->info('Transfer sms to ' . $user['email'] . ' : ' . json_encode($sms));
$this->logger->info('Transfer sms to '.$user['email'].' : '.json_encode($sms));
\controllers\internals\Tool::send_email($user['email'], EMAIL_TRANSFER_SMS, ['sms' => $sms]);
}
public function on_start()
{
//Set last message at to construct time
$this->last_message_at = microtime(true);
$this->msg_queue = msg_get_queue($this->msg_queue_id);
$this->webhook_queue = msg_get_queue(QUEUE_ID_WEBHOOK);
//Instanciate adapter
$adapter_class = $this->phone['adapter'];
$this->adapter = new $adapter_class($this->phone['number'], $this->phone['adapter_datas']);
$this->logger->info("Starting Phone daemon with pid " . getmypid());
}
public function on_stop()
{
//Delete queue on daemon close
$this->logger->info("Closing queue : " . $this->msg_queue_id);
msg_remove_queue($this->msg_queue);
$this->logger->info("Stopping Phone daemon with pid " . getmypid ());
}
public function handle_other_signals($signal)
{
$this->logger->info("Signal not handled by " . $this->name . " Daemon : " . $signal);
}
}

View file

@ -1,11 +1,21 @@
<?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 daemons;
use \Monolog\Logger;
use \Monolog\Handler\StreamHandler;
use Monolog\Handler\StreamHandler;
use Monolog\Logger;
/**
* Main daemon class
* Main daemon class.
*/
class Sender extends AbstractDaemon
{
@ -17,9 +27,9 @@ class Sender extends AbstractDaemon
public function __construct()
{
$name = "RaspiSMS Daemon Sender";
$name = 'RaspiSMS Daemon Sender';
$logger = new Logger($name);
$logger->pushHandler(new StreamHandler(PWD_LOGS . '/raspisms.log', Logger::DEBUG));
$logger->pushHandler(new StreamHandler(PWD_LOGS.'/raspisms.log', Logger::DEBUG));
$pid_dir = PWD_PID;
$no_parent = false; //Webhook should be rattach to manager, so manager can stop him easily
$additional_signals = [];
@ -31,7 +41,6 @@ class Sender extends AbstractDaemon
parent::start();
}
public function run()
{
//Create the internal controllers
@ -44,12 +53,12 @@ class Sender extends AbstractDaemon
usleep(0.5 * 1000000);
}
/**
* Function to get messages to send and transfer theme to phones daemons
* Function to get messages to send and transfer theme to phones daemons.
*
* @param array $smss : Smss to send
*/
public function transmit_smss (array $smss) : void
public function transmit_smss(array $smss): void
{
foreach ($smss as $sms)
{
@ -75,29 +84,26 @@ class Sender extends AbstractDaemon
}
}
public function on_start()
{
$this->logger->info("Starting Sender with pid " . getmypid());
$this->logger->info('Starting Sender with pid '.getmypid());
$this->bdd = \descartes\Model::_connect(DATABASE_HOST, DATABASE_NAME, DATABASE_USER, DATABASE_PASSWORD, 'UTF8');
}
public function on_stop()
public function on_stop()
{
$this->logger->info("Stopping Sender with pid " . getmypid ());
$this->logger->info('Stopping Sender with pid '.getmypid());
//Delete queues on daemon close
foreach ($this->queues as $queue_id => $queue)
{
$this->logger->info("Closing queue : " . $queue_id);
$this->logger->info('Closing queue : '.$queue_id);
msg_remove_queue($queue);
}
}
public function handle_other_signals($signal)
{
$this->logger->info("Signal not handled by " . $this->name . " Daemon : " . $signal);
$this->logger->info('Signal not handled by '.$this->name.' Daemon : '.$signal);
}
}

View file

@ -1,11 +1,21 @@
<?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 daemons;
use \Monolog\Logger;
use \Monolog\Handler\StreamHandler;
use Monolog\Handler\StreamHandler;
use Monolog\Logger;
/**
* Phone daemon class
* Phone daemon class.
*/
class Webhook extends AbstractDaemon
{
@ -16,14 +26,15 @@ class Webhook extends AbstractDaemon
private $bdd;
/**
* Constructor
* Constructor.
*
* @param array $phone : A phone table entry
*/
public function __construct()
{
$name = "RaspiSMS Daemon Webhook";
$name = 'RaspiSMS Daemon Webhook';
$logger = new Logger($name);
$logger->pushHandler(new StreamHandler(PWD_LOGS . '/raspisms.log', Logger::DEBUG));
$logger->pushHandler(new StreamHandler(PWD_LOGS.'/raspisms.log', Logger::DEBUG));
$pid_dir = PWD_PID;
$no_parent = false; //Sended should be rattach to manager, so manager can stop him easily
$additional_signals = [];
@ -35,13 +46,12 @@ class Webhook extends AbstractDaemon
parent::start();
}
public function run()
{
$find_message = true;
while ($find_message)
{
//Call message
//Call message
$msgtype = null;
$maxsize = 409600;
$message = null;
@ -50,16 +60,17 @@ class Webhook extends AbstractDaemon
$success = msg_receive($this->webhook_queue, QUEUE_TYPE_WEBHOOK, $msgtype, $maxsize, $message, true, 0, $error_code);
if (!$success)
{
$this->logger->critical('Error for webhook queue reading, error code : ' . $error_code);
$this->logger->critical('Error for webhook queue reading, error code : '.$error_code);
}
if (!$message)
{
$find_message = false;
continue;
}
$this->logger->info('Trigger webhook : ' . json_encode($message));
$this->logger->info('Trigger webhook : '.json_encode($message));
//Do the webhook http query
$curl = curl_init();
@ -74,30 +85,27 @@ class Webhook extends AbstractDaemon
usleep(0.5 * 1000000);
}
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);
$this->logger->info("Starting Webhook daemon with pid " . getmypid());
$this->logger->info('Starting Webhook daemon with pid '.getmypid());
}
public function on_stop()
public function on_stop()
{
//Delete queue on daemon close
$this->logger->info("Closing queue : " . QUEUE_ID_WEBHOOK);
$this->logger->info('Closing queue : '.QUEUE_ID_WEBHOOK);
msg_remove_queue($this->webhook_queue);
$this->logger->info("Stopping Webhook daemon with pid " . getmypid ());
$this->logger->info('Stopping Webhook daemon with pid '.getmypid());
}
public function handle_other_signals($signal)
{
$this->logger->info("Signal not handled by " . $this->name . " Daemon : " . $signal);
$this->logger->info('Signal not handled by '.$this->name.' Daemon : '.$signal);
}
}