Working daemons by extracting phone create and using cmd instead

This commit is contained in:
osaajani 2019-12-17 14:38:16 +01:00
parent c265e0d43c
commit 99cdf6516a
5 changed files with 29 additions and 21 deletions

View File

@ -21,4 +21,9 @@ namespace controllers\internals;
$server = new \daemons\Server(); $server = new \daemons\Server();
} }
public function phone ($number)
{
$server = new \daemons\Phone($number);
}
} }

View File

@ -341,7 +341,8 @@ namespace controllers\internals;
if ((int) ($users_settings[$scheduled['id_user']]['templating'] ?? false)) if ((int) ($users_settings[$scheduled['id_user']]['templating'] ?? false))
{ {
$contact['datas'] = json_decode($contact['datas'], true); $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']) if (!$render['success'])
{ {

View File

@ -97,7 +97,7 @@ abstract class AbstractDaemon
//If process must be uniq and a process with the same pid file is already running //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)
{ {
echo "Another process named " . $this->name . " is already running.\n"; $this->logger->info("Another process named " . $this->name . " is already running.");
return false; return false;
} }
@ -105,23 +105,26 @@ abstract class AbstractDaemon
if ($pid == -1) //Impossible to run script if ($pid == -1) //Impossible to run script
{ {
echo "Impossible to create a subprocess.\n"; $this->logger->critical("Impossible to create a subprocess.");
return false; return false;
} }
elseif ($pid) //Current script elseif ($pid) //Current script
{ {
echo "Child process started with pid " . $pid . ".\n";
return true; return true;
} }
$this->logger->info("Process $this->name started as a child with pid $pid.");
//Child script //Child script
$sid = posix_setsid(); //Try to make the child process a main process $sid = posix_setsid(); //Try to make the child process a main process
if ($sid == -1) //Error 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); exit(1);
} }
$this->logger->info("The child process with pid $pid is now independent.");
//Create pid dir if not exists //Create pid dir if not exists
if (!file_exists($this->pid_dir)) if (!file_exists($this->pid_dir))
{ {

View File

@ -13,11 +13,11 @@ class Phone extends AbstractDaemon
private $queue_id; private $queue_id;
private $last_message_at; 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 = new Logger($name);
$logger->pushHandler(new StreamHandler(PWD_LOGS . '/raspisms.log', Logger::DEBUG)); $logger->pushHandler(new StreamHandler(PWD_LOGS . '/raspisms.log', Logger::DEBUG));
@ -29,7 +29,6 @@ class Phone extends AbstractDaemon
//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);
//Start the daemon
parent::start(); parent::start();
} }
@ -68,7 +67,7 @@ class Phone extends AbstractDaemon
$this->msg_queue = msg_get_queue($this->queue_id); $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); $this->logger->info("Closing queue : " . $this->queue_id);
msg_remove_queue($this->msg_queue); //Delete queue on daemon close 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 ());
} }

View File

@ -27,8 +27,6 @@ class Server extends AbstractDaemon
//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);
//Start the daemon
parent::start(); parent::start();
} }
@ -46,7 +44,7 @@ class Server extends AbstractDaemon
$phones = $this->internal_phone->get_all(); $phones = $this->internal_phone->get_all();
foreach ($phones as $phone) foreach ($phones as $phone)
{ {
$phone_name = 'Phone ' . $phone['number']; $phone_name = 'RaspiSMS Phone ' . $phone['number'];
$pid_file = PWD_PID . '/' . $phone_name . '.pid'; $pid_file = PWD_PID . '/' . $phone_name . '.pid';
if (file_exists($pid_file)) if (file_exists($pid_file))
@ -54,8 +52,10 @@ class Server extends AbstractDaemon
continue; 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 //Create a new daemon for the phone
$phone = new \daemons\Phone($phone); //$phone = new \daemons\Phone($phone);
} }
$queues = []; $queues = [];
@ -94,13 +94,13 @@ class Server extends AbstractDaemon
public function on_start() 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() public function on_stop()
{ {
$this->logger->info("Stopping " . $this->name . " with pid " . getmypid ()); $this->logger->info("Stopping Server with pid " . getmypid ());
} }