Rename manager to launcher, add option for no background daemon
This commit is contained in:
parent
929ea695e1
commit
c790b47c14
|
@ -17,11 +17,11 @@ namespace controllers\internals;
|
|||
class Console extends \descartes\InternalController
|
||||
{
|
||||
/**
|
||||
* Start manager daemon
|
||||
* Start launcher daemon
|
||||
*/
|
||||
public function manager ()
|
||||
public function launcher ()
|
||||
{
|
||||
new \daemons\Manager();
|
||||
new \daemons\Launcher();
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -9,7 +9,8 @@ abstract class AbstractDaemon
|
|||
{
|
||||
protected $name;
|
||||
protected $uniq;
|
||||
protected $logger;
|
||||
protected $logger;
|
||||
protected $no_parent;
|
||||
private $is_running = true;
|
||||
private $signals = array (
|
||||
SIGTERM,
|
||||
|
@ -24,16 +25,18 @@ abstract class AbstractDaemon
|
|||
* @param string $name : The name of the class
|
||||
* @param object $logger : A PSR3 logger instance
|
||||
* @param string $pid_dir : Directory for the pid files
|
||||
* @param bool $no_parent : Should the daemon be disconnected from his parent process
|
||||
* @param array $signals :An array containing additional POSIX signals to handle [optionel]
|
||||
* @param bool $uniq : Must the process be uniq ?
|
||||
*/
|
||||
protected function __construct (string $name, object $logger, string $pid_dir = '/var/run', array $signals = [], bool $uniq = false)
|
||||
protected function __construct (string $name, object $logger, string $pid_dir = '/var/run', $no_parent = false, array $signals = [], bool $uniq = false)
|
||||
{
|
||||
$this->name = $name;
|
||||
$this->logger = $logger;
|
||||
$this->no_parent = $no_parent;
|
||||
$this->pid_dir = $pid_dir;
|
||||
$this->signals = array_merge($this->signals, $signals);
|
||||
$this->uniq = $uniq;
|
||||
$this->pid_dir = $pid_dir;
|
||||
|
||||
//Allow script to run indefinitly
|
||||
set_time_limit(0);
|
||||
|
@ -101,29 +104,35 @@ abstract class AbstractDaemon
|
|||
return false;
|
||||
}
|
||||
|
||||
$pid = pcntl_fork(); //Fork current process into a child, so we will be able to later make the child indepedant, kill current process and keep only the child
|
||||
|
||||
if ($pid == -1) //Impossible to run script
|
||||
//If we must make the daemon independant from any parent, we do a fork and die operation
|
||||
if ($this->no_parent)
|
||||
{
|
||||
$this->logger->critical("Impossible to create a subprocess.");
|
||||
return false;
|
||||
}
|
||||
elseif ($pid) //Current script
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
$this->logger->info("Process $this->name started as a child with pid $pid.");
|
||||
$pid = pcntl_fork(); //Fork current process into a child, so we can kill current process and keep only the child with parent PID = 1
|
||||
|
||||
//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 with pid $pid independent.");
|
||||
exit(1);
|
||||
if ($pid == -1) //Impossible to run script
|
||||
{
|
||||
$this->logger->critical("Impossible to create a subprocess.");
|
||||
return false;
|
||||
}
|
||||
elseif ($pid) //Current script
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
$this->logger->info("Process $this->name started as a child with pid " . getmypid() . ".");
|
||||
|
||||
//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 with pid $pid independent.");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
$this->logger->info("The child process with pid " . getmypid() . " is now independent.");
|
||||
}
|
||||
|
||||
$this->logger->info("The child process with pid $pid is now independent.");
|
||||
|
||||
|
||||
//Create pid dir if not exists
|
||||
if (!file_exists($this->pid_dir))
|
||||
|
@ -136,14 +145,17 @@ abstract class AbstractDaemon
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
//Set process name
|
||||
cli_set_process_title($this->name);
|
||||
|
||||
//Write the pid of the process into a file
|
||||
file_put_contents($this->pid_dir . '/' . $this->name . '.pid', getmypid());
|
||||
|
||||
|
||||
//Really start the daemon
|
||||
$this->on_start();
|
||||
|
||||
|
||||
try
|
||||
{
|
||||
while ($this->is_running)
|
||||
|
@ -156,9 +168,11 @@ abstract class AbstractDaemon
|
|||
{
|
||||
$this->logger->critical('Exception : ' . $e->getMessage() . " in " . $e->getFile() . " line " . $e->getLine());
|
||||
}
|
||||
|
||||
|
||||
//Stop the daemon
|
||||
$this->on_stop();
|
||||
|
||||
|
||||
//Delete pid file
|
||||
if (file_exists($this->pid_dir . '/' . $this->name . '.pid'))
|
||||
{
|
||||
|
|
|
@ -7,7 +7,7 @@ use \Monolog\Handler\StreamHandler;
|
|||
/**
|
||||
* Main daemon class
|
||||
*/
|
||||
class Manager extends AbstractDaemon
|
||||
class Launcher extends AbstractDaemon
|
||||
{
|
||||
private $internal_phone;
|
||||
private $internal_scheduled;
|
||||
|
@ -16,16 +16,17 @@ class Manager extends AbstractDaemon
|
|||
|
||||
public function __construct()
|
||||
{
|
||||
$logger = new Logger('Daemon Manager');
|
||||
$logger = new Logger('Daemon Launcher');
|
||||
$logger->pushHandler(new StreamHandler(PWD_LOGS . '/raspisms.log', Logger::DEBUG));
|
||||
|
||||
$name = "RaspiSMS Daemon Manager";
|
||||
$name = "RaspiSMS Daemon Launcher";
|
||||
$pid_dir = PWD_PID;
|
||||
$no_parent = true; //Launcher should be rattach to PID 1
|
||||
$additional_signals = [];
|
||||
$uniq = true; //Main server should be uniq
|
||||
|
||||
//Construct the server and add SIGUSR1 and SIGUSR2
|
||||
parent::__construct($name, $logger, $pid_dir, $additional_signals, $uniq);
|
||||
parent::__construct($name, $logger, $pid_dir, $no_parent, $additional_signals, $uniq);
|
||||
|
||||
parent::start();
|
||||
}
|
||||
|
@ -112,13 +113,13 @@ class Manager extends AbstractDaemon
|
|||
|
||||
public function on_start()
|
||||
{
|
||||
$this->logger->info("Starting Manager with pid " . getmypid());
|
||||
$this->logger->info("Starting Launcher with pid " . getmypid());
|
||||
}
|
||||
|
||||
|
||||
public function on_stop()
|
||||
{
|
||||
$this->logger->info("Stopping Manager with pid " . getmypid ());
|
||||
$this->logger->info("Stopping Launcher with pid " . getmypid ());
|
||||
}
|
||||
|
||||
|
|
@ -27,16 +27,15 @@ class Phone extends AbstractDaemon
|
|||
$this->msg_queue_id = (int) mb_substr($this->phone['number'], 1);
|
||||
|
||||
$name = 'RaspiSMS Daemon Phone ' . $this->phone['number'];
|
||||
|
||||
$logger = new Logger($name);
|
||||
$logger->pushHandler(new StreamHandler(PWD_LOGS . '/raspisms.log', Logger::DEBUG));
|
||||
|
||||
$pid_dir = PWD_PID;
|
||||
$no_parent = false; //Phone should be rattach to manager, so manager can stop him easily
|
||||
$additional_signals = [];
|
||||
$uniq = true; //Main server should be uniq
|
||||
$uniq = true; //Each phone should be uniq
|
||||
|
||||
//Construct the server and add SIGUSR1 and SIGUSR2
|
||||
parent::__construct($name, $logger, $pid_dir, $additional_signals, $uniq);
|
||||
//Construct the daemon
|
||||
parent::__construct($name, $logger, $pid_dir, $no_parent, $additional_signals, $uniq);
|
||||
|
||||
parent::start();
|
||||
}
|
||||
|
|
|
@ -17,16 +17,16 @@ class Sender extends AbstractDaemon
|
|||
|
||||
public function __construct()
|
||||
{
|
||||
$logger = new Logger('Daemon Sender');
|
||||
$logger->pushHandler(new StreamHandler(PWD_LOGS . '/raspisms.log', Logger::DEBUG));
|
||||
|
||||
$name = "RaspiSMS Daemon Sender";
|
||||
$logger = new Logger($name);
|
||||
$logger->pushHandler(new StreamHandler(PWD_LOGS . '/raspisms.log', Logger::DEBUG));
|
||||
$pid_dir = PWD_PID;
|
||||
$no_parent = false; //Webhook should be rattach to manager, so manager can stop him easily
|
||||
$additional_signals = [];
|
||||
$uniq = true; //Sender should be uniq
|
||||
$uniq = true; //Webhook should be uniq
|
||||
|
||||
//Construct the server and add SIGUSR1 and SIGUSR2
|
||||
parent::__construct($name, $logger, $pid_dir, $additional_signals, $uniq);
|
||||
//Construct the daemon
|
||||
parent::__construct($name, $logger, $pid_dir, $no_parent, $additional_signals, $uniq);
|
||||
|
||||
parent::start();
|
||||
}
|
||||
|
|
|
@ -21,18 +21,17 @@ class Webhook extends AbstractDaemon
|
|||
*/
|
||||
public function __construct()
|
||||
{
|
||||
$name = 'RaspiSMS Daemon Webhook';
|
||||
|
||||
$name = "RaspiSMS Daemon Webhook";
|
||||
$logger = new Logger($name);
|
||||
$logger->pushHandler(new StreamHandler(PWD_LOGS . '/raspisms.log', Logger::DEBUG));
|
||||
|
||||
$pid_dir = PWD_PID;
|
||||
$no_parent = false; //Sended should be rattach to manager, so manager can stop him easily
|
||||
$additional_signals = [];
|
||||
$uniq = true; //Main server should be uniq
|
||||
$uniq = true; //Sender should be uniq
|
||||
|
||||
//Construct the daemon
|
||||
parent::__construct($name, $logger, $pid_dir, $no_parent, $additional_signals, $uniq);
|
||||
|
||||
//Construct the server and add SIGUSR1 and SIGUSR2
|
||||
parent::__construct($name, $logger, $pid_dir, $additional_signals, $uniq);
|
||||
|
||||
parent::start();
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue