Skip to content

Commit

Permalink
Update AMPHP
Browse files Browse the repository at this point in the history
  • Loading branch information
bencroker committed Feb 17, 2024
1 parent ef86cc1 commit 3b7f88e
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 54 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ This plugin requires a commercial license purchasable through the [Craft Plugin

## Requirements

This plugin requires [Craft CMS](https://craftcms.com/) 4.0.0 or later.
This plugin requires [Craft CMS](https://craftcms.com/) 4.0.0 or later, or 5.0.0 or later.

## Installation

Expand Down
14 changes: 7 additions & 7 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
],
"require": {
"php": "^8.2",
"amphp/http-client": "^4.0",
"amphp/parallel": "^1.0",
"amphp/http-client": "^5.0",
"amphp/parallel": "^2.0",
"craftcms/cms": "^5.0.0-beta.1"
},
"require-dev": {
Expand All @@ -33,16 +33,16 @@
"url": "https://github.com/bencroker/craft-pest-core"
}
],
"scripts": {
"phpstan": "phpstan --ansi --memory-limit=1G",
"check-cs": "ecs check --ansi",
"fix-cs": "ecs check --fix --ansi"
},
"autoload": {
"psr-4": {
"putyourlightson\\cacheigniter\\": "src/"
}
},
"scripts": {
"phpstan": "phpstan --ansi --memory-limit=1G",
"check-cs": "ecs check --ansi",
"fix-cs": "ecs check --fix --ansi"
},
"config": {
"allow-plugins": {
"craftcms/plugin-installer": true,
Expand Down
55 changes: 9 additions & 46 deletions src/drivers/warmers/HttpWarmer.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,22 @@
use Amp\Http\Client\HttpClientBuilder;
use Amp\Http\Client\HttpException;
use Amp\Http\Client\Request;
use Amp\MultiReasonException;
use Amp\Sync\LocalSemaphore;
use Amp\Pipeline\Pipeline;
use Craft;
use craft\helpers\UrlHelper;
use putyourlightson\blitz\drivers\generators\HttpGenerator;
use putyourlightson\cacheigniter\CacheIgniter;
use putyourlightson\cacheigniter\events\WarmUrlsEvent;
use Throwable;
use yii\log\Logger;

use function Amp\Iterator\fromIterable;
use function Amp\Promise\wait;

/**
* Based on Blitz’s HttpGenerator class.
*
* @see HttpGenerator
*
* The Amp PHP framework is used for making HTTP requests and a concurrent
* The AMPHP framework is used for making HTTP requests and a concurrent
* iterator is used to send the requests concurrently.
* See https://amphp.org/http-client/concurrent
* and https://amphp.org/sync/concurrent-iterator
* See https://amphp.org/http-client/ and https://amphp.org/pipeline
*
* @property-read null|string $settingsHtml
*/
Expand Down Expand Up @@ -72,21 +66,16 @@ public function warmUrlsWithProgress(array $urls, ?callable $setProgressHandler

$client = HttpClientBuilder::buildDefault();

// Approach 4: Concurrent Iterator
// https://amphp.org/sync/concurrent-iterator#approach-4-concurrent-iterator
$promise = \Amp\Sync\ConcurrentIterator\each(
fromIterable($urls),
new LocalSemaphore($this->concurrency),
function(string $url) use ($setProgressHandler, &$count, $total, $client) {
$count++;
$concurrentIterator = Pipeline::fromIterable($urls)
->concurrent($this->concurrency);

if (UrlHelper::isAbsoluteUrl($url) === false) {
return;
}
foreach ($concurrentIterator as $url) {
$count++;

if (UrlHelper::isAbsoluteUrl($url) !== false) {
try {
$request = $this->_createRequest($url);
yield $client->request($request);
$client->request($request);

if (is_callable($setProgressHandler)) {
$this->updateProgress($setProgressHandler, $count, $total);
Expand All @@ -95,14 +84,6 @@ function(string $url) use ($setProgressHandler, &$count, $total, $client) {
CacheIgniter::$plugin->log($exception->getMessage() . ' [' . $url . ']', [], Logger::LEVEL_ERROR);
}
}
);

// Exceptions are thrown only when the promise is resolved.
try {
wait($promise);
} // Catch all possible exceptions to avoid interrupting progress.
catch (Throwable $exception) {
CacheIgniter::$plugin->log($this->_getAllExceptionMessages($exception));
}
}

Expand All @@ -127,24 +108,6 @@ protected function defineRules(): array
];
}

/**
* Returns all messages for an exception, for easier debugging.
*/
private function _getAllExceptionMessages(Throwable $exception): string
{
$messages = [
$exception->getMessage(),
];

while ($exception = $exception->getPrevious()) {
if (!($exception instanceof MultiReasonException)) {
$messages[] = $exception->getMessage();
}
}

return implode('. ', $messages);
}

private function _createRequest(string $url): Request
{
$request = new Request($url);
Expand Down

0 comments on commit 3b7f88e

Please sign in to comment.