mirror of
https://github.com/shlinkio/shlink.git
synced 2025-03-29 04:52:54 +03:00
Updated NotifyVisitToWebHooks so that it waits for all requests to finish
This commit is contained in:
parent
562b0a0868
commit
21a3d4b66b
4 changed files with 40 additions and 15 deletions
|
@ -40,6 +40,7 @@ return [
|
||||||
'em',
|
'em',
|
||||||
'Logger_Shlink',
|
'Logger_Shlink',
|
||||||
'config.url_shortener.visits_webhooks',
|
'config.url_shortener.visits_webhooks',
|
||||||
|
'config.url_shortener.domain',
|
||||||
],
|
],
|
||||||
],
|
],
|
||||||
|
|
||||||
|
|
|
@ -61,6 +61,11 @@ class Visit extends AbstractEntity implements JsonSerializable
|
||||||
return ! empty($this->remoteAddr);
|
return ! empty($this->remoteAddr);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function getShortUrl(): ShortUrl
|
||||||
|
{
|
||||||
|
return $this->shortUrl;
|
||||||
|
}
|
||||||
|
|
||||||
public function getVisitLocation(): VisitLocationInterface
|
public function getVisitLocation(): VisitLocationInterface
|
||||||
{
|
{
|
||||||
return $this->visitLocation ?? new UnknownVisitLocation();
|
return $this->visitLocation ?? new UnknownVisitLocation();
|
||||||
|
|
|
@ -10,8 +10,12 @@ use GuzzleHttp\ClientInterface;
|
||||||
use GuzzleHttp\RequestOptions;
|
use GuzzleHttp\RequestOptions;
|
||||||
use Psr\Log\LoggerInterface;
|
use Psr\Log\LoggerInterface;
|
||||||
use Shlinkio\Shlink\Core\Entity\Visit;
|
use Shlinkio\Shlink\Core\Entity\Visit;
|
||||||
|
use Shlinkio\Shlink\Core\Transformer\ShortUrlDataTransformer;
|
||||||
|
use Throwable;
|
||||||
|
|
||||||
use function Functional\map;
|
use function Functional\map;
|
||||||
|
use function Functional\partial_left;
|
||||||
|
use function GuzzleHttp\Promise\settle;
|
||||||
|
|
||||||
class NotifyVisitToWebHooks
|
class NotifyVisitToWebHooks
|
||||||
{
|
{
|
||||||
|
@ -23,17 +27,21 @@ class NotifyVisitToWebHooks
|
||||||
private $logger;
|
private $logger;
|
||||||
/** @var array */
|
/** @var array */
|
||||||
private $webhooks;
|
private $webhooks;
|
||||||
|
/** @var ShortUrlDataTransformer */
|
||||||
|
private $transformer;
|
||||||
|
|
||||||
public function __construct(
|
public function __construct(
|
||||||
ClientInterface $httpClient,
|
ClientInterface $httpClient,
|
||||||
EntityManagerInterface $em,
|
EntityManagerInterface $em,
|
||||||
LoggerInterface $logger,
|
LoggerInterface $logger,
|
||||||
array $webhooks
|
array $webhooks,
|
||||||
|
array $domainConfig
|
||||||
) {
|
) {
|
||||||
$this->httpClient = $httpClient;
|
$this->httpClient = $httpClient;
|
||||||
$this->em = $em;
|
$this->em = $em;
|
||||||
$this->logger = $logger;
|
$this->logger = $logger;
|
||||||
$this->webhooks = $webhooks;
|
$this->webhooks = $webhooks;
|
||||||
|
$this->transformer = new ShortUrlDataTransformer($domainConfig);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function __invoke(ShortUrlLocated $shortUrlLocated): void
|
public function __invoke(ShortUrlLocated $shortUrlLocated): void
|
||||||
|
@ -51,17 +59,25 @@ class NotifyVisitToWebHooks
|
||||||
|
|
||||||
$requestOptions = [
|
$requestOptions = [
|
||||||
RequestOptions::TIMEOUT => 10,
|
RequestOptions::TIMEOUT => 10,
|
||||||
RequestOptions::JSON => $visit->jsonSerialize(),
|
RequestOptions::JSON => [
|
||||||
|
'shortUrl' => $this->transformer->transform($visit->getShortUrl(), false),
|
||||||
|
'visit' => $visit->jsonSerialize(),
|
||||||
|
],
|
||||||
];
|
];
|
||||||
$requestPromises = map($this->webhooks, function (string $webhook) use ($requestOptions, $visitId) {
|
$logWebhookWarning = function (string $webhook, Throwable $e) use ($visitId): void {
|
||||||
|
$this->logger->warning('Failed to notify visit with id "{visitId}" to webhook "{webhook}". {e}', [
|
||||||
|
'visitId' => $visitId,
|
||||||
|
'webhook' => $webhook,
|
||||||
|
'e' => $e,
|
||||||
|
]);
|
||||||
|
};
|
||||||
|
|
||||||
|
$requestPromises = map($this->webhooks, function (string $webhook) use ($requestOptions, $logWebhookWarning) {
|
||||||
$promise = $this->httpClient->requestAsync(RequestMethodInterface::METHOD_POST, $webhook, $requestOptions);
|
$promise = $this->httpClient->requestAsync(RequestMethodInterface::METHOD_POST, $webhook, $requestOptions);
|
||||||
return $promise->otherwise(function () use ($webhook, $visitId) {
|
return $promise->otherwise(partial_left($logWebhookWarning, $webhook));
|
||||||
// Log failures
|
|
||||||
$this->logger->warning('Failed to notify visit with id "{visitId}" to "{webhook}" webhook', [
|
|
||||||
'visitId' => $visitId,
|
|
||||||
'webhook' => $webhook,
|
|
||||||
]);
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Wait for all the promises to finish, ignoring rejections, as in those cases we only want to log the error.
|
||||||
|
settle($requestPromises)->wait();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -23,11 +23,11 @@ class ShortUrlDataTransformer implements DataTransformerInterface
|
||||||
/**
|
/**
|
||||||
* @param ShortUrl $shortUrl
|
* @param ShortUrl $shortUrl
|
||||||
*/
|
*/
|
||||||
public function transform($shortUrl): array
|
public function transform($shortUrl, bool $includeDeprecated = true): array
|
||||||
{
|
{
|
||||||
$longUrl = $shortUrl->getLongUrl();
|
$longUrl = $shortUrl->getLongUrl();
|
||||||
|
|
||||||
return [
|
$rawData = [
|
||||||
'shortCode' => $shortUrl->getShortCode(),
|
'shortCode' => $shortUrl->getShortCode(),
|
||||||
'shortUrl' => $shortUrl->toString($this->domainConfig),
|
'shortUrl' => $shortUrl->toString($this->domainConfig),
|
||||||
'longUrl' => $longUrl,
|
'longUrl' => $longUrl,
|
||||||
|
@ -35,10 +35,13 @@ class ShortUrlDataTransformer implements DataTransformerInterface
|
||||||
'visitsCount' => $shortUrl->getVisitsCount(),
|
'visitsCount' => $shortUrl->getVisitsCount(),
|
||||||
'tags' => invoke($shortUrl->getTags(), '__toString'),
|
'tags' => invoke($shortUrl->getTags(), '__toString'),
|
||||||
'meta' => $this->buildMeta($shortUrl),
|
'meta' => $this->buildMeta($shortUrl),
|
||||||
|
|
||||||
// Deprecated
|
|
||||||
'originalUrl' => $longUrl,
|
|
||||||
];
|
];
|
||||||
|
|
||||||
|
if ($includeDeprecated) {
|
||||||
|
$rawData['originalUrl'] = $longUrl;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $rawData;
|
||||||
}
|
}
|
||||||
|
|
||||||
private function buildMeta(ShortUrl $shortUrl): array
|
private function buildMeta(ShortUrl $shortUrl): array
|
||||||
|
|
Loading…
Add table
Reference in a new issue