From 99cdf6516a15464bc1ddae75d10588358e2fa194 Mon Sep 17 00:00:00 2001 From: osaajani Date: Tue, 17 Dec 2019 14:38:16 +0100 Subject: [PATCH] Working daemons by extracting phone create and using cmd instead --- controllers/internals/Console.php | 7 ++++++- controllers/internals/Scheduled.php | 3 ++- daemons/AbstractDaemon.php | 15 +++++++++------ daemons/Phone.php | 13 ++++++------- daemons/Server.php | 12 ++++++------ 5 files changed, 29 insertions(+), 21 deletions(-) diff --git a/controllers/internals/Console.php b/controllers/internals/Console.php index 1e5e99b..9577164 100755 --- a/controllers/internals/Console.php +++ b/controllers/internals/Console.php @@ -20,5 +20,10 @@ namespace controllers\internals; { $server = new \daemons\Server(); } - + + + public function phone ($number) + { + $server = new \daemons\Phone($number); + } } diff --git a/controllers/internals/Scheduled.php b/controllers/internals/Scheduled.php index 03f5af9..3cc4705 100755 --- a/controllers/internals/Scheduled.php +++ b/controllers/internals/Scheduled.php @@ -341,7 +341,8 @@ namespace controllers\internals; if ((int) ($users_settings[$scheduled['id_user']]['templating'] ?? false)) { $contact['datas'] = json_decode($contact['datas'], true); - $render = $internal_templating->render($scheduled['text'], $contact); + $datas = ['contact' => $contact]; + $render = $internal_templating->render($scheduled['text'], $datas); if (!$render['success']) { diff --git a/daemons/AbstractDaemon.php b/daemons/AbstractDaemon.php index 9f53a65..38764c2 100644 --- a/daemons/AbstractDaemon.php +++ b/daemons/AbstractDaemon.php @@ -71,7 +71,7 @@ abstract class AbstractDaemon { if ($signal == SIGTERM || $signal == SIGINT) //Stop the daemon { - $this->is_running = false; + $this->is_running = false; } else if ($signal == SIGHUP) //Restart the daemon { @@ -80,7 +80,7 @@ abstract class AbstractDaemon } else if ($signal == SIGCHLD) //On daemon child stopping { - pcntl_waitpid(-1, $status, WNOHANG); + pcntl_waitpid(-1, $status, WNOHANG); } else //All the other signals { @@ -97,7 +97,7 @@ abstract class AbstractDaemon //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) { - echo "Another process named " . $this->name . " is already running.\n"; + $this->logger->info("Another process named " . $this->name . " is already running."); return false; } @@ -105,22 +105,25 @@ abstract class AbstractDaemon if ($pid == -1) //Impossible to run script { - echo "Impossible to create a subprocess.\n"; + $this->logger->critical("Impossible to create a subprocess."); return false; } elseif ($pid) //Current script { - echo "Child process started with pid " . $pid . ".\n"; return true; } + + $this->logger->info("Process $this->name started as a child with pid $pid."); //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 independent.'); + $this->logger->critical("Cannot make the child process with pid $pid independent."); exit(1); } + + $this->logger->info("The child process with pid $pid is now independent."); //Create pid dir if not exists if (!file_exists($this->pid_dir)) diff --git a/daemons/Phone.php b/daemons/Phone.php index 61e2f74..319747c 100644 --- a/daemons/Phone.php +++ b/daemons/Phone.php @@ -13,11 +13,11 @@ class Phone extends AbstractDaemon private $queue_id; private $last_message_at; - public function __construct($phone) + public function __construct($phone_number) { - $this->queue_id = (int) mb_substr($phone['number'], 1); + $this->queue_id = (int) mb_substr($phone_number, 1); - $name = 'Phone ' . $phone['number']; + $name = 'RaspiSMS Phone ' . $phone_number; $logger = new Logger($name); $logger->pushHandler(new StreamHandler(PWD_LOGS . '/raspisms.log', Logger::DEBUG)); @@ -28,8 +28,7 @@ class Phone extends AbstractDaemon //Construct the server and add SIGUSR1 and SIGUSR2 parent::__construct($name, $logger, $pid_dir, $additional_signals, $uniq); - - //Start the daemon + parent::start(); } @@ -68,7 +67,7 @@ class Phone extends AbstractDaemon $this->msg_queue = msg_get_queue($this->queue_id); - $this->logger->info("Starting " . $this->name . " with pid " . getmypid()); + $this->logger->info("Starting Phone with pid " . getmypid()); } @@ -77,7 +76,7 @@ class Phone extends AbstractDaemon $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 Phone with pid " . getmypid ()); } diff --git a/daemons/Server.php b/daemons/Server.php index 802431a..715d5f0 100644 --- a/daemons/Server.php +++ b/daemons/Server.php @@ -27,8 +27,6 @@ class Server extends AbstractDaemon //Construct the server and add SIGUSR1 and SIGUSR2 parent::__construct($name, $logger, $pid_dir, $additional_signals, $uniq); - - //Start the daemon parent::start(); } @@ -46,7 +44,7 @@ class Server extends AbstractDaemon $phones = $this->internal_phone->get_all(); foreach ($phones as $phone) { - $phone_name = 'Phone ' . $phone['number']; + $phone_name = 'RaspiSMS Phone ' . $phone['number']; $pid_file = PWD_PID . '/' . $phone_name . '.pid'; if (file_exists($pid_file)) @@ -54,8 +52,10 @@ class Server extends AbstractDaemon continue; } + exec('php ' . PWD . '/console.php controllers/internals/Console.php phone number=\'' . $phone['number'] . '\' > /dev/null &'); + $this->logger->info('Command : ' . 'php ' . PWD . '/console.php controllers/internals/Console.php phone number=\'' . $phone['number'] . '\' > /dev/null &'); //Create a new daemon for the phone - $phone = new \daemons\Phone($phone); + //$phone = new \daemons\Phone($phone); } $queues = []; @@ -94,13 +94,13 @@ class Server extends AbstractDaemon public function on_start() { - $this->logger->info("Starting " . $this->name . " with pid " . getmypid()); + $this->logger->info("Starting Server with pid " . getmypid()); } public function on_stop() { - $this->logger->info("Stopping " . $this->name . " with pid " . getmypid ()); + $this->logger->info("Stopping Server with pid " . getmypid ()); }