2020-01-07 01:31:34 +01:00
|
|
|
<?php
|
2020-01-17 18:19:25 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* This file is part of RaspiSMS.
|
|
|
|
*
|
|
|
|
* (c) Pierre-Lin Bonnemaison <plebwebsas@gmail.com>
|
|
|
|
*
|
|
|
|
* This source file is subject to the GPL-3.0 license that is bundled
|
|
|
|
* with this source code in the file LICENSE.
|
|
|
|
*/
|
|
|
|
|
2020-01-07 01:31:34 +01:00
|
|
|
namespace daemons;
|
|
|
|
|
2020-01-17 18:19:25 +01:00
|
|
|
use Monolog\Handler\StreamHandler;
|
|
|
|
use Monolog\Logger;
|
2020-01-07 01:31:34 +01:00
|
|
|
|
|
|
|
/**
|
2020-01-17 18:19:25 +01:00
|
|
|
* Main daemon class.
|
2020-01-07 01:31:34 +01:00
|
|
|
*/
|
2020-01-08 18:20:17 +01:00
|
|
|
class Launcher extends AbstractDaemon
|
2020-01-07 01:31:34 +01:00
|
|
|
{
|
|
|
|
private $internal_phone;
|
|
|
|
private $internal_scheduled;
|
|
|
|
private $internal_received;
|
|
|
|
private $bdd;
|
|
|
|
|
|
|
|
public function __construct()
|
|
|
|
{
|
2020-01-17 18:19:25 +01:00
|
|
|
$name = 'RaspiSMS Daemon Launcher';
|
|
|
|
|
2020-01-08 18:32:18 +01:00
|
|
|
$logger = new Logger($name);
|
2020-03-04 04:18:26 +01:00
|
|
|
$logger->pushHandler(new StreamHandler(PWD_LOGS . '/daemons.log', Logger::DEBUG));
|
2020-01-07 01:31:34 +01:00
|
|
|
$pid_dir = PWD_PID;
|
2020-01-08 18:20:17 +01:00
|
|
|
$no_parent = true; //Launcher should be rattach to PID 1
|
2020-01-07 01:31:34 +01:00
|
|
|
$additional_signals = [];
|
|
|
|
$uniq = true; //Main server should be uniq
|
|
|
|
|
|
|
|
//Construct the server and add SIGUSR1 and SIGUSR2
|
2020-01-08 18:20:17 +01:00
|
|
|
parent::__construct($name, $logger, $pid_dir, $no_parent, $additional_signals, $uniq);
|
2020-01-07 01:31:34 +01:00
|
|
|
|
|
|
|
parent::start();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function run()
|
|
|
|
{
|
|
|
|
//Create the internal controllers
|
|
|
|
$this->bdd = \descartes\Model::_connect(DATABASE_HOST, DATABASE_NAME, DATABASE_USER, DATABASE_PASSWORD, 'UTF8');
|
|
|
|
$this->internal_phone = new \controllers\internals\Phone($this->bdd);
|
|
|
|
|
|
|
|
$this->start_sender_daemon();
|
|
|
|
|
|
|
|
$this->start_webhook_daemon();
|
|
|
|
|
|
|
|
$phones = $this->internal_phone->get_all();
|
|
|
|
$this->start_phones_daemons($phones);
|
|
|
|
|
|
|
|
sleep(1);
|
|
|
|
}
|
2020-01-17 18:19:25 +01:00
|
|
|
|
2020-01-07 01:31:34 +01:00
|
|
|
/**
|
2020-01-17 18:19:25 +01:00
|
|
|
* Function to start sender daemon.
|
2020-01-07 01:31:34 +01:00
|
|
|
*/
|
2020-01-17 18:19:25 +01:00
|
|
|
public function start_sender_daemon()
|
2020-01-07 01:31:34 +01:00
|
|
|
{
|
|
|
|
$name = 'RaspiSMS Daemon Sender';
|
2020-01-17 18:47:08 +01:00
|
|
|
$pid_file = PWD_PID . '/' . $name . '.pid';
|
2020-01-17 18:19:25 +01:00
|
|
|
|
2020-01-07 01:31:34 +01:00
|
|
|
if (file_exists($pid_file))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Create a new daemon for sender
|
2020-01-08 18:32:18 +01:00
|
|
|
$pid = null;
|
2020-01-17 18:47:08 +01:00
|
|
|
exec('php ' . PWD . '/console.php controllers/internals/Console.php sender > /dev/null 2>&1 &');
|
2020-01-07 01:31:34 +01:00
|
|
|
}
|
2020-01-17 18:19:25 +01:00
|
|
|
|
2020-01-07 01:31:34 +01:00
|
|
|
/**
|
2020-01-17 18:19:25 +01:00
|
|
|
* Function to start webhook daemon.
|
2020-01-07 01:31:34 +01:00
|
|
|
*/
|
2020-01-17 18:19:25 +01:00
|
|
|
public function start_webhook_daemon()
|
2020-01-07 01:31:34 +01:00
|
|
|
{
|
|
|
|
$name = 'RaspiSMS Daemon Webhook';
|
2020-01-17 18:47:08 +01:00
|
|
|
$pid_file = PWD_PID . '/' . $name . '.pid';
|
2020-01-17 18:19:25 +01:00
|
|
|
|
2020-01-07 01:31:34 +01:00
|
|
|
if (file_exists($pid_file))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Create a new daemon for webhook
|
2020-01-17 18:47:08 +01:00
|
|
|
exec('php ' . PWD . '/console.php controllers/internals/Console.php webhook > /dev/null 2>&1 &');
|
2020-01-07 01:31:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-01-17 18:19:25 +01:00
|
|
|
* Function to start phones daemons.
|
|
|
|
*
|
2020-01-07 01:31:34 +01:00
|
|
|
* @param array $phones : Phones to start daemon for if the daemon is not already started
|
|
|
|
*/
|
2020-01-17 18:19:25 +01:00
|
|
|
public function start_phones_daemons(array $phones)
|
2020-01-07 01:31:34 +01:00
|
|
|
{
|
|
|
|
foreach ($phones as $phone)
|
|
|
|
{
|
2020-03-04 01:40:47 +01:00
|
|
|
$phone_name = 'RaspiSMS Daemon Phone ' . $phone['id'];
|
2020-01-17 18:47:08 +01:00
|
|
|
$pid_file = PWD_PID . '/' . $phone_name . '.pid';
|
2020-01-17 18:19:25 +01:00
|
|
|
|
2020-01-07 01:31:34 +01:00
|
|
|
if (file_exists($pid_file))
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Create a new daemon for the phone
|
2020-01-17 18:47:08 +01:00
|
|
|
exec('php ' . PWD . '/console.php controllers/internals/Console.php phone --id_phone=\'' . $phone['id'] . '\' > /dev/null 2>&1 &');
|
2020-01-07 01:31:34 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function on_start()
|
|
|
|
{
|
2020-01-17 18:47:08 +01:00
|
|
|
$this->logger->info('Starting Launcher with pid ' . getmypid());
|
2020-01-07 01:31:34 +01:00
|
|
|
}
|
|
|
|
|
2020-01-17 18:19:25 +01:00
|
|
|
public function on_stop()
|
2020-01-07 01:31:34 +01:00
|
|
|
{
|
2020-01-17 18:47:08 +01:00
|
|
|
$this->logger->info('Stopping Launcher with pid ' . getmypid());
|
2020-01-07 01:31:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function handle_other_signals($signal)
|
|
|
|
{
|
2020-01-17 18:47:08 +01:00
|
|
|
$this->logger->info('Signal not handled by ' . $this->name . ' Daemon : ' . $signal);
|
2020-01-07 01:31:34 +01:00
|
|
|
}
|
|
|
|
}
|