From ca9397b62d68905391f5f224c7d0a7378fbee403 Mon Sep 17 00:00:00 2001 From: osaajani <> Date: Wed, 4 Mar 2020 03:28:34 +0100 Subject: [PATCH] Fix invalid closing of queues from sender, improve cleaning of pid files --- bin/stop.sh | 2 ++ controllers/internals/Console.php | 2 +- controllers/internals/Scheduled.php | 2 +- daemons/Phone.php | 15 ++++++++------- daemons/Sender.php | 10 ++-------- 5 files changed, 14 insertions(+), 17 deletions(-) diff --git a/bin/stop.sh b/bin/stop.sh index a42beb9..46d7597 100755 --- a/bin/stop.sh +++ b/bin/stop.sh @@ -25,6 +25,7 @@ then if [ $RETURN -eq 0 ] then + rm -f "$DAEMON_LAUNCHER_PID_FILE" printf "success.\n" else printf "failed.\n" @@ -42,6 +43,7 @@ do printf "." PID=$(cat "$f") kill_process "$PID" + rm -f "$f" done printf "Done.\n" diff --git a/controllers/internals/Console.php b/controllers/internals/Console.php index e25bad4..1e4eabe 100644 --- a/controllers/internals/Console.php +++ b/controllers/internals/Console.php @@ -53,7 +53,7 @@ namespace controllers\internals; $phone = $internal_phone->get($id_phone); if (!$phone) { - return false; + exit(1); } new \daemons\Phone($phone); diff --git a/controllers/internals/Scheduled.php b/controllers/internals/Scheduled.php index c373e07..73a0550 100644 --- a/controllers/internals/Scheduled.php +++ b/controllers/internals/Scheduled.php @@ -328,7 +328,7 @@ namespace controllers\internals; 'id_user' => $scheduled['id_user'], 'id_scheduled' => $scheduled['id'], 'id_phone' => $phone_to_use['id'], - 'destination' => $number['number'], + 'destination' => $contact['number'], 'flash' => $scheduled['flash'], ]; diff --git a/daemons/Phone.php b/daemons/Phone.php index 066d3e3..55f5c58 100644 --- a/daemons/Phone.php +++ b/daemons/Phone.php @@ -19,6 +19,7 @@ use Monolog\Logger; */ class Phone extends AbstractDaemon { + private $max_inactivity = 5*60; private $msg_queue; private $msg_queue_id; private $webhook_queue; @@ -53,12 +54,7 @@ class Phone extends AbstractDaemon public function run() { - //Stop after 5 minutes of inactivity to avoid useless daemon - if ((microtime(true) - $this->last_message_at) > 5 * 60) - { - posix_kill(getmypid(), SIGTERM); //Send exit signal to the current process - return false; - } + usleep(0.5 * 1000000); //Micro sleep for perfs $this->bdd = \descartes\Model::_connect(DATABASE_HOST, DATABASE_NAME, DATABASE_USER, DATABASE_PASSWORD, 'UTF8'); @@ -68,7 +64,12 @@ class Phone extends AbstractDaemon //Read received smss $this->read_smss(); - usleep(0.5 * 1000000); + //Stop after 5 minutes of inactivity to avoid useless daemon + if ((microtime(true) - $this->last_message_at) > $this->max_inactivity) + { + posix_kill(getmypid(), SIGTERM); //Send exit signal to the current process + return false; + } } public function on_start() diff --git a/daemons/Sender.php b/daemons/Sender.php index 27cb8c8..9adb5ae 100644 --- a/daemons/Sender.php +++ b/daemons/Sender.php @@ -54,7 +54,7 @@ class Sender extends AbstractDaemon } /** - * Function to get messages to send and transfer theme to phones daemons. + * Function to transfer smss to send to phones daemons. * * @param array $smss : Smss to send */ @@ -80,6 +80,7 @@ class Sender extends AbstractDaemon msg_send($this->queues[$queue_id], QUEUE_TYPE_SEND_MSG, $msg); + $this->logger->info('Transmit sms send signal to phone ' . $sms['id_phone'] . ' on queue ' . $queue_id . '.'); $this->internal_scheduled->delete($sms['id_scheduled']); } } @@ -93,13 +94,6 @@ class Sender extends AbstractDaemon public function on_stop() { $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); - msg_remove_queue($queue); - } } public function handle_other_signals($signal)