Skip to content

Commit

Permalink
Provided support for PHP 8.2+
Browse files Browse the repository at this point in the history
  • Loading branch information
Juan Lago committed Mar 21, 2024
1 parent 06d7add commit 881fb9c
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 57 deletions.
18 changes: 11 additions & 7 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,24 @@
}
],
"config": {
"vendor-dir": "src/Vendor"
"vendor-dir": "src/Vendor",
"allow-plugins": {
"php-http/discovery": true
}
},
"scripts": {
"post-create-project-cmd": [
"@composer create-project mamuph/system src/System \"2.2\""
]
},
"require": {
"league/climate": "3.2.1",
"elasticsearch/elasticsearch": "v6.0.1",
"nesbot/carbon": "1.22.1",
"amphp/amp": "^2.1",
"amphp/artax": "^3.0",
"league/climate": "^3.8.1",
"elasticsearch/elasticsearch": "^v7.17",
"nesbot/carbon": "^3.1",
"amphp/amp": "^v3.0.0",
"amphp/http-client": "^5.0",
"ext-pcntl": "*",
"ext-json": "*"
"ext-json": "*",
"revolt/event-loop": "^1.0"
}
}
23 changes: 10 additions & 13 deletions src/App/Controller/Main.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

use Amp\Loop;
use Revolt\EventLoop;


/**
Expand Down Expand Up @@ -161,20 +161,18 @@ public function actionMain()

// Initialize event loop
// ---------------------
Loop::run(function ()
EventLoop::repeat(Params::get('read_freq') / 1000, function ()
{
Loop::onSignal(SIGINT, [$this, 'actionTerminateBySignal']);
Loop::onSignal(SIGHUP, [$this, 'actionTerminateBySignal']);

// "repeat" is used instead of "onReadable", because unfortunately not all
// PHP installations include the libevent extension.
Loop::repeat(Params::get('read_freq'), Closure::fromCallable([$this, 'readLog']));
$this->readLog();
});

EventLoop::onSignal(SIGINT, fn() => $this->actionTerminateBySignal());
EventLoop::onSignal(SIGHUP, fn() => $this->actionTerminateBySignal());

// Finish app
Apprunner::terminate(Apprunner::EXIT_SUCCESS);
EventLoop::run();

// Finish app
Apprunner::terminate();
}


Expand Down Expand Up @@ -253,9 +251,8 @@ public function actionTerminateBySignal()
{
if (!ignore_user_abort())
{
echo "Exiting...";

Loop::stop();
echo "Exiting...\n";
Apprunner::terminate(Apprunner::EXIT_HUP);
}
}

Expand Down
5 changes: 2 additions & 3 deletions src/App/Model/Clients/Datadog.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

use Amp\Artax\DefaultClient;
use Amp\Http\Client\HttpClientBuilder;

/**
* Class Model_Clients_Datadog.
Expand Down Expand Up @@ -30,8 +30,7 @@ public function __construct(array $hosts, int $retries = 2, bool $check_cert = t

// Set limit to the batch size
$this->settings['batch_size'] = $this->settings['batch_size'] > static::MAX_BATCH_SIZE ? static::MAX_BATCH_SIZE : $this->settings['batch_size'];

$this->client = new DefaultClient();
$this->client = HttpClientBuilder::buildDefault();
}

}
67 changes: 40 additions & 27 deletions src/App/Model/Senders/Datadog.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
<?php


use Amp\Artax\Client;
use Amp\Artax\Request;
use Amp\Http\Client\HttpClient;
use Amp\Http\Client\Request;


/**
Expand All @@ -16,7 +15,7 @@ class Model_Senders_Datadog implements Model_Contracts_Sender
/**
* Datadog client.
*
* @var \Amp\Artax\DefaultClient
* @var HttpClient
*/
protected $client;

Expand Down Expand Up @@ -62,37 +61,51 @@ class Model_Senders_Datadog implements Model_Contracts_Sender
public function __construct($client, bool $async = false)
{
$this->client = $client->getClient();
$this->client->setOption(Client::OP_DISCARD_BODY, $async);

$this->client_settings = $client->getClientSettings();

$this->async = $async;
$this->async = $async;
}


/**
/**
* Send log entry.
*
* @param string $index
* @param array $body
* @param string $type
* @throws \Amp\Http\Client\HttpException
*/
public function send(string $index, array $body)
{
$this->batch[$index][] = $body;

if (count($this->batch[$index]) >= $this->client_settings['batch_size'])
$this->sendNow($index);
}

/**
* Perform the request.
*
* @param Request $request
* @return void
* @throws \Amp\Http\Client\HttpException
*/
protected function performRequest(Request $request)
{
$response = $this->client->request($request);

if (Params::get('verbose') && !$response->isSuccessful()) {
echo 'Error on send log ' . $response->getStatus();
}
}


/**
* Send logs immediately.
*
* @param string $index
* @return Generator
*/
/**
* Send logs immediately.
*
* @param string $index
* @return Generator
* @throws \Amp\Http\Client\HttpException
*/
protected function sendNow(string $index)
{
$query_params = http_build_query([
Expand All @@ -101,18 +114,18 @@ protected function sendNow(string $index)
'hostname' => $this->batch[$index][0]['hostname']
]);

$request = (new Request($this->client_settings['hosts'][0] . '?' . $query_params, 'POST'))
->withHeader('Content-Type', 'application/json')
->withBody(json_encode($this->batch[$index]));

$this->client->request($request)->onResolve(function ($error, $response) {
if ($error && Params::get('verbose'))
{
echo 'Error on send log' . PHP_EOL;
var_dump($error);
}
});

$request = new Request(
$this->client_settings['hosts'][0] . '?' . $query_params,
'POST',
json_encode($this->batch[$index])
);
$request->setHeader('Content-Type', 'application/json');

if ($this->async) {
Amp\async(fn() => $this->performRequest($request));
} else {
$this->performRequest($request);
}

$this->batch[$index] = [];
}
Expand Down
7 changes: 3 additions & 4 deletions src/App/Model/Senders/ElasticSearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,24 @@ class Model_Senders_ElasticSearch implements Model_Contracts_Sender
/**
* Elastic Search client.
*
* @var \Elasticsearch\Client
*/
protected $client;
protected Elasticsearch\Client $client;


/**
* Last entry.
*
* @var array
*/
protected $last_entry = [];
protected array $last_entry = [];


/**
* Use future mode.
*
* @var bool $future_mode
*/
protected $future_mode = false;
protected bool $future_mode = false;


/**
Expand Down
6 changes: 3 additions & 3 deletions src/Config/Version.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
return array (
'major' => 2,
'minor' => 7,
'build' => 53,
'major' => 4,
'minor' => 0,
'build' => 55,
'codename' => '',
);

0 comments on commit 881fb9c

Please sign in to comment.