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;
|
|
|
|
|
2024-10-31 21:22:56 +01:00
|
|
|
use controllers\internals\Queue;
|
2021-11-05 23:26:09 +01:00
|
|
|
use GuzzleHttp\Promise\Utils;
|
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
|
|
|
|
{
|
2024-10-31 21:22:56 +01:00
|
|
|
private ?Queue $webhook_queue;
|
2020-09-02 23:32:17 +02:00
|
|
|
private $guzzle_client;
|
2020-01-07 01:31:34 +01:00
|
|
|
|
|
|
|
/**
|
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
|
|
|
|
|
2020-09-02 23:32:17 +02:00
|
|
|
$this->guzzle_client = new \GuzzleHttp\Client();
|
|
|
|
|
2020-01-08 18:20:17 +01:00
|
|
|
//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;
|
2020-09-02 23:32:17 +02:00
|
|
|
$promises = [];
|
2020-01-07 01:31:34 +01:00
|
|
|
while ($find_message)
|
|
|
|
{
|
2024-10-31 21:22:56 +01:00
|
|
|
$message = $this->webhook_queue->read(QUEUE_TYPE_WEBHOOK);
|
2020-01-17 18:19:25 +01:00
|
|
|
|
2024-10-31 21:22:56 +01:00
|
|
|
if ($message === null)
|
2020-01-07 01:31:34 +01:00
|
|
|
{
|
|
|
|
$find_message = false;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2024-10-31 21:22:56 +01:00
|
|
|
$this->logger->info('Trigger webhook : ' . $message);
|
2020-01-07 01:31:34 +01:00
|
|
|
|
2024-10-31 21:22:56 +01:00
|
|
|
$message = json_decode($message, true);
|
2021-01-17 03:16:57 +01:00
|
|
|
$promises[] = $this->guzzle_client->postAsync($message['url'], ['form_params' => $message['data']]);
|
2020-09-02 23:32:17 +02:00
|
|
|
}
|
|
|
|
|
2020-09-02 23:45:01 +02:00
|
|
|
try
|
|
|
|
{
|
2021-11-05 23:26:09 +01:00
|
|
|
$responses = Utils::unwrap($promises);
|
2020-09-02 23:45:01 +02:00
|
|
|
}
|
|
|
|
catch (\Exception $e)
|
|
|
|
{
|
2020-09-02 23:32:17 +02:00
|
|
|
$this->logger->info('Webhook : ' . json_encode($message) . 'failed with ' . $e->getMessage());
|
2020-01-07 01:31:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
usleep(0.5 * 1000000);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function on_start()
|
|
|
|
{
|
2024-10-31 21:22:56 +01:00
|
|
|
try{
|
|
|
|
$this->webhook_queue = new Queue(QUEUE_ID_WEBHOOK);
|
|
|
|
}
|
|
|
|
catch (\Exception $e)
|
|
|
|
{
|
|
|
|
$this->logger->info('Webhook : failed with ' . $e->getMessage());
|
|
|
|
}
|
2020-01-07 01:31:34 +01:00
|
|
|
|
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);
|
2024-10-31 21:22:56 +01:00
|
|
|
unset($this->webhook_queue);
|
2020-01-07 01:31:34 +01:00
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|