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
* Phone daemon class .
2020-01-07 01:31:34 +01:00
*/
class Webhook extends AbstractDaemon
{
private $webhook_queue ;
private $last_message_at ;
private $phone ;
private $adapter ;
private $bdd ;
/**
2020-01-17 18:19:25 +01:00
* Constructor .
*
2020-01-07 01:31:34 +01:00
* @ param array $phone : A phone table entry
*/
public function __construct ()
{
2020-01-17 18:19:25 +01:00
$name = 'RaspiSMS Daemon Webhook' ;
2020-01-07 01:31:34 +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 = false ; //Sended should be rattach to manager, so manager can stop him easily
2020-01-07 01:31:34 +01:00
$additional_signals = [];
2020-01-08 18:20:17 +01:00
$uniq = true ; //Sender should be uniq
//Construct the daemon
parent :: __construct ( $name , $logger , $pid_dir , $no_parent , $additional_signals , $uniq );
2020-01-07 01:31:34 +01:00
parent :: start ();
}
public function run ()
{
$find_message = true ;
while ( $find_message )
{
2020-01-17 18:19:25 +01:00
//Call message
2020-01-07 17:55:16 +01:00
$msgtype = null ;
$maxsize = 409600 ;
$message = null ;
$error_code = null ;
2020-03-05 22:15:59 +01:00
$success = msg_receive ( $this -> webhook_queue , QUEUE_TYPE_WEBHOOK , $msgtype , $maxsize , $message , true , MSG_IPC_NOWAIT , $error_code ); //MSG_IPC_NOWAIT == dont wait if no message found
if ( ! $success && MSG_ENOMSG !== $error_code )
2020-01-07 17:55:16 +01:00
{
2020-01-17 18:47:08 +01:00
$this -> logger -> critical ( 'Error for webhook queue reading, error code : ' . $error_code );
2020-04-02 18:40:39 +02:00
$find_message = false ;
2020-06-23 21:06:13 +02:00
2020-04-02 18:40:39 +02:00
continue ;
2020-01-07 17:55:16 +01:00
}
2020-01-17 18:19:25 +01:00
2020-01-07 01:31:34 +01:00
if ( ! $message )
{
$find_message = false ;
2020-01-17 18:19:25 +01:00
2020-01-07 01:31:34 +01:00
continue ;
}
2020-01-17 18:47:08 +01:00
$this -> logger -> info ( 'Trigger webhook : ' . json_encode ( $message ));
2020-01-07 01:31:34 +01:00
//Do the webhook http query
$curl = curl_init ();
curl_setopt ( $curl , CURLOPT_URL , $message [ 'url' ]);
curl_setopt ( $curl , CURLOPT_FOLLOWLOCATION , true );
curl_setopt ( $curl , CURLOPT_POST , true );
curl_setopt ( $curl , CURLOPT_POSTFIELDS , $message [ 'datas' ]);
curl_exec ( $curl );
curl_close ( $curl );
}
usleep ( 0.5 * 1000000 );
}
public function on_start ()
{
//Set last message at to construct time
$this -> last_message_at = microtime ( true );
2020-01-17 18:19:25 +01:00
2020-01-07 01:31:34 +01:00
$this -> webhook_queue = msg_get_queue ( QUEUE_ID_WEBHOOK );
2020-01-17 18:47:08 +01:00
$this -> logger -> info ( 'Starting Webhook daemon 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
{
//Delete queue on daemon close
2020-01-17 18:47:08 +01:00
$this -> logger -> info ( 'Closing queue : ' . QUEUE_ID_WEBHOOK );
2020-01-07 01:31:34 +01:00
msg_remove_queue ( $this -> webhook_queue );
2020-01-17 18:47:08 +01:00
$this -> logger -> info ( 'Stopping Webhook daemon 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
}
}