This commit is contained in:
osaajani 2019-12-17 07:47:11 +01:00
parent 6614c2cfeb
commit c265e0d43c
2 changed files with 37 additions and 13 deletions

View File

@ -10,19 +10,21 @@ use \Monolog\Handler\StreamHandler;
class Phone extends AbstractDaemon class Phone extends AbstractDaemon
{ {
private $msg_queue; private $msg_queue;
private $queue_id;
private $last_message_at;
public function __construct($phone) public function __construct($phone)
{ {
$name = 'Phone ' . $phone['number']; $this->queue_id = (int) mb_substr($phone['number'], 1);
$pid_dir = PWD_PID;
$additional_signals = [SIGUSR1, SIGUSR2];
$uniq = true; //Main server should be uniq
$queue_id = (int) mb_substr($phone['number'], 1); $name = 'Phone ' . $phone['number'];
$this->msg_queue = msg_get_queue($queue_id);
$logger = new Logger($name); $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;
$additional_signals = [];
$uniq = true; //Main server should be uniq
//Construct the server and add SIGUSR1 and SIGUSR2 //Construct the server and add SIGUSR1 and SIGUSR2
parent::__construct($name, $logger, $pid_dir, $additional_signals, $uniq); parent::__construct($name, $logger, $pid_dir, $additional_signals, $uniq);
@ -34,6 +36,13 @@ class Phone extends AbstractDaemon
public function run() public function run()
{ {
if ( (microtime(true) - $this->last_message_at) > 5 * 60 )
{
$this->is_running = false;
$this->logger->info("End running");
return true;
}
$msgtype = null; $msgtype = null;
$maxsize = 409600; $maxsize = 409600;
$message = null; $message = null;
@ -45,18 +54,29 @@ class Phone extends AbstractDaemon
return true; return true;
} }
//If message received, update last message time
$this->last_message_at = microtime(true);
$this->logger->debug(json_encode($message)); $this->logger->debug(json_encode($message));
} }
public function on_start() public function on_start()
{ {
//Set last message at to construct time
$this->last_message_at = microtime(true);
$this->msg_queue = msg_get_queue($this->queue_id);
$this->logger->info("Starting " . $this->name . " with pid " . getmypid()); $this->logger->info("Starting " . $this->name . " with pid " . getmypid());
} }
public function on_stop() public function on_stop()
{ {
$this->logger->info("Closing queue : " . $this->queue_id);
msg_remove_queue($this->msg_queue); //Delete queue on daemon close
$this->logger->info("Stopping " . $this->name . " with pid " . getmypid ()); $this->logger->info("Stopping " . $this->name . " with pid " . getmypid ());
} }

View File

@ -21,7 +21,7 @@ class Server extends AbstractDaemon
$name = "RaspiSMS Server"; $name = "RaspiSMS Server";
$pid_dir = PWD_PID; $pid_dir = PWD_PID;
$additional_signals = [SIGUSR1, SIGUSR2]; $additional_signals = [];
$uniq = true; //Main server should be uniq $uniq = true; //Main server should be uniq
//Construct the server and add SIGUSR1 and SIGUSR2 //Construct the server and add SIGUSR1 and SIGUSR2
@ -54,7 +54,7 @@ class Server extends AbstractDaemon
continue; continue;
} }
//Create a new daemon for the phone and a new queue //Create a new daemon for the phone
$phone = new \daemons\Phone($phone); $phone = new \daemons\Phone($phone);
} }
@ -64,13 +64,18 @@ class Server extends AbstractDaemon
$smss = $this->internal_scheduled->get_smss_to_send(); $smss = $this->internal_scheduled->get_smss_to_send();
foreach ($smss as $sms) foreach ($smss as $sms)
{ {
if (!isset($queues[$sms['origin']])) //If queue has been deleted or does not exist, create a new one
$queue_id = (int) mb_substr($sms['origin'], 1);
if (!msg_queue_exists($queue_id))
{ {
$queue_id = (int) mb_substr($sms['origin'], 1); $queues[$queue_id] = msg_get_queue($queue_id);
$queues[$sms['origin']] = msg_get_queue($queue_id); }
elseif (!isset($queues[$queue_id]))
{
$queues[$queue_id] = msg_get_queue($queue_id);
} }
$queue = $queues[$sms['origin']]; $queue = $queues[$queue_id];
$msg = [ $msg = [
'text' => (string) $sms['text'], 'text' => (string) $sms['text'],
@ -83,7 +88,6 @@ class Server extends AbstractDaemon
} }
sleep(0.5); sleep(0.5);
} }