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

@ -20,5 +20,10 @@ namespace controllers\internals;
{
$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))
{
$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'])
{

View File

@ -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))

View File

@ -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 ());
}

View File

@ -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 ());
}