Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BEG-137: Move recaching to queue consumer changes #6

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/php.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: Run PHP Static Analysis

on: pull_request

jobs:
code-standards:
runs-on: ubuntu-latest
Expand All @@ -11,7 +11,7 @@ jobs:
with:
fetch-depth: 0
- name: Code Standards Test
uses: docker://aligent/code-standards-pipe-php:7.4
uses: docker://aligent/code-standards-pipe-php:8.1
env:
STANDARDS: "Magento2"
SKIP_DEPENDENCIES: "true"
61 changes: 61 additions & 0 deletions Api/Data/PrerenderRecachingManagementRequestInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php
/**
* Aligent Consulting
* Copyright (c) Aligent Consulting (https://www.aligent.com.au)
*/

declare(strict_types=1);

namespace Aligent\Prerender\Api\Data;

interface PrerenderRecachingManagementRequestInterface
{
public const BATCH_URLS = "batch_urls";
public const STORE_ID = "store_id";
public const INDEXER_ID = "indexer_id";

/**
* Get Batch Urls
*
* @return string
*/
public function getBatchUrls(): string;

/**
* Set Batch Urls
*
* @param string $batchUrls
* @return void
*/
public function setBatchUrls(string $batchUrls): void;

/**
* Get Store Id
*
* @return int
*/
public function getStoreId(): int;

/**
* Set Store Id
*
* @param int $storeId
* @return void
*/
public function setStoreId(int $storeId): void;

/**
* Get Indexer Id
*
* @return string
*/
public function getIndexerId(): string;

/**
* Set Indexer Id
*
* @param string $indexerId
* @return void
*/
public function setIndexerId(string $indexerId): void;
}
24 changes: 24 additions & 0 deletions Api/PrerenderRecachingManagementInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php
/**
* Aligent Consulting
* Copyright (c) Aligent Consulting (https://www.aligent.com.au)
*/

declare(strict_types=1);

namespace Aligent\Prerender\Api;

use Aligent\Prerender\Api\Data\PrerenderRecachingManagementRequestInterface;
use DOMException;

interface PrerenderRecachingManagementInterface
{

/**
* Recache Urls
*
* @param PrerenderRecachingManagementRequestInterface $prerenderRecachingManagementRequest
* @return string
*/
public function recacheUrls(PrerenderRecachingManagementRequestInterface $prerenderRecachingManagementRequest): string;
}
9 changes: 3 additions & 6 deletions Helper/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,13 @@ class Config
private const XML_PATH_PRERENDER_TOKEN = 'system/prerender/token';
private const XML_PATH_RECACHE_SERVICE_URL = 'system/prerender/service_url';

/** @var ScopeConfigInterface */
private ScopeConfigInterface $scopeConfig;

/**
*
* @param ScopeConfigInterface $scopeConfig
*/
public function __construct(ScopeConfigInterface $scopeConfig)
{
$this->scopeConfig = $scopeConfig;
public function __construct(
private readonly ScopeConfigInterface $scopeConfig
) {
}

/**
Expand Down
63 changes: 63 additions & 0 deletions Model/Api/Data/PrerenderRecachingManagementRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php
/**
* Aligent Consulting
* Copyright (c) Aligent Consulting (https://www.aligent.com.au)
*/

declare(strict_types=1);

namespace Aligent\Prerender\Model\Api\Data;

use Aligent\Prerender\Api\Data\PrerenderRecachingManagementRequestInterface;
use Magento\Framework\DataObject;

class PrerenderRecachingManagementRequest extends DataObject implements PrerenderRecachingManagementRequestInterface
{
/**
* @inheritDoc
*/
public function getBatchUrls(): string
{
return $this->getData(self::BATCH_URLS);
}

/**
* @inheritDoc
*/
public function setBatchUrls(string $batchUrls): void
{
$this->setData(self::BATCH_URLS, $batchUrls);
}

/**
* @inheritDoc
*/
public function getStoreId(): int
{
return $this->getData(self::STORE_ID);
}

/**
* @inheritDoc
*/
public function setStoreId(int $storeId): void
{
$this->setData(self::STORE_ID, $storeId);
}

/**
* @inheritDoc
*/
public function getIndexerId(): string
{
return $this->getData(self::INDEXER_ID);
}

/**
* @inheritDoc
*/
public function setIndexerId(string $indexerId): void
{
$this->setData(self::INDEXER_ID, $indexerId);
}
}
21 changes: 4 additions & 17 deletions Model/Api/PrerenderClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,6 @@ class PrerenderClient implements PrerenderClientInterface

private const MAX_URLS = 1000;

/** @var Config */
private Config $prerenderConfigHelper;
/** @var ClientInterface */
private ClientInterface $client;
/** @var SerializerInterface */
private SerializerInterface $jsonSerializer;
/** @var LoggerInterface */
private LoggerInterface $logger;

/**
*
* @param Config $prerenderConfigHelper
Expand All @@ -35,15 +26,11 @@ class PrerenderClient implements PrerenderClientInterface
* @param LoggerInterface $logger
*/
public function __construct(
Config $prerenderConfigHelper,
ClientInterface $client,
SerializerInterface $jsonSerializer,
LoggerInterface $logger
private readonly Config $prerenderConfigHelper,
private readonly ClientInterface $client,
private readonly SerializerInterface $jsonSerializer,
private readonly LoggerInterface $logger
) {
$this->prerenderConfigHelper = $prerenderConfigHelper;
$this->jsonSerializer = $jsonSerializer;
$this->logger = $logger;
$this->client = $client;
$this->client->addHeader('content-type', 'application/json');
}

Expand Down
50 changes: 50 additions & 0 deletions Model/Api/PrerenderRecachingManagement.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php
/**
* Aligent Consulting
* Copyright (c) Aligent Consulting (https://www.aligent.com.au)
*/

declare(strict_types=1);

namespace Aligent\Prerender\Model\Api;

use Magento\Framework\Serialize\Serializer\Json;
use Aligent\Prerender\Api\PrerenderRecachingManagementInterface;
use Aligent\Prerender\Api\Data\PrerenderRecachingManagementRequestInterface;
use Aligent\Prerender\Api\PrerenderClientInterface;
use Exception;

class PrerenderRecachingManagement implements PrerenderRecachingManagementInterface
{

/**
* @param PrerenderClientInterface $prerenderClient
* @param Json $json
*/
public function __construct(
private readonly PrerenderClientInterface $prerenderClient,
private readonly Json $json,
) {
}

/**
* @inheritDoc
*/
public function recacheUrls(
PrerenderRecachingManagementRequestInterface $prerenderRecachingManagementRequest
): string {
$message = "";
try {
$this->prerenderClient->recacheUrls(
$this->json->unserialize($prerenderRecachingManagementRequest->getBatchUrls()),
$prerenderRecachingManagementRequest->getStoreId()
);
$message = 'INFO: Recaching Urls successfully synced for ' .
$prerenderRecachingManagementRequest->getIndexerId();
} catch (Exception $exception) {
$message = 'ERROR: There was an error syncing the Recaching Urls for ' .
$prerenderRecachingManagementRequest->getIndexerId() . $exception->getMessage();
}
return $message;
}
}
45 changes: 22 additions & 23 deletions Model/Indexer/Category/CategoryIndexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
use Aligent\Prerender\Api\PrerenderClientInterface;
use Aligent\Prerender\Helper\Config;
use Aligent\Prerender\Model\Url\GetUrlsForCategories;
use Aligent\Prerender\Api\Data\PrerenderRecachingManagementRequestInterfaceFactory
as PrerenderRecachingManagementRequest;
use Magento\AsynchronousOperations\Model\MassSchedule;
use Magento\Framework\Serialize\Serializer\Json;
use Magento\Framework\App\DeploymentConfig;
use Magento\Framework\Exception\FileSystemException;
use Magento\Framework\Exception\LocalizedException;
Expand All @@ -25,42 +29,31 @@ class CategoryIndexer implements IndexerActionInterface, MviewActionInterface, D
private const INDEXER_ID = 'prerender_category';
private const DEPLOYMENT_CONFIG_INDEXER_BATCHES = 'indexer/batch_size/';

/** @var DimensionProviderInterface */
private DimensionProviderInterface $dimensionProvider;
/** @var GetUrlsForCategories */
private GetUrlsForCategories $getUrlsForCategories;
/** @var PrerenderClientInterface */
private PrerenderClientInterface $prerenderClient;
/** @var DeploymentConfig */
private DeploymentConfig $eploymentConfig;
/** @var Config */
private Config $prerenderConfigHelper;
/** @var int|null */
private ?int $batchSize;

/**
*
* @param DimensionProviderInterface $dimensionProvider
* @param GetUrlsForCategories $getUrlsForCategories
* @param PrerenderClientInterface $prerenderClient
* @param DeploymentConfig $deploymentConfig
* @param Config $prerenderConfigHelper
* @param PrerenderRecachingManagementRequest $prerenderRecachingManagementRequest
* @param MassSchedule $massSchedule
* @param int|null $batchSize
*/
public function __construct(
DimensionProviderInterface $dimensionProvider,
GetUrlsForCategories $getUrlsForCategories,
PrerenderClientInterface $prerenderClient,
DeploymentConfig $deploymentConfig,
Config $prerenderConfigHelper,
private readonly DimensionProviderInterface $dimensionProvider,
private readonly GetUrlsForCategories $getUrlsForCategories,
private readonly PrerenderClientInterface $prerenderClient,
private readonly DeploymentConfig $deploymentConfig,
private readonly Config $prerenderConfigHelper,
private readonly PrerenderRecachingManagementRequest $prerenderRecachingManagementRequest,
private readonly MassSchedule $massSchedule,
private readonly Json $json,
?int $batchSize = 1000
) {
$this->dimensionProvider = $dimensionProvider;
$this->getUrlsForCategories = $getUrlsForCategories;
$this->prerenderClient = $prerenderClient;
$this->deploymentConfig = $deploymentConfig;
$this->batchSize = $batchSize;
$this->prerenderConfigHelper = $prerenderConfigHelper;
}

/**
Expand Down Expand Up @@ -129,7 +122,9 @@ public function execute($ids): void
public function executeByDimensions(array $dimensions, \Traversable $entityIds): void
{
if (count($dimensions) > 1 || !isset($dimensions[StoreDimensionProvider::DIMENSION_NAME])) {
throw new \InvalidArgumentException('Indexer "' . self::INDEXER_ID . '" supports only Store dimension');
throw new \InvalidArgumentException(
'Indexer "' . self::INDEXER_ID . '" supports only Store dimension'
);
}
$storeId = (int)$dimensions[StoreDimensionProvider::DIMENSION_NAME]->getValue();

Expand All @@ -147,7 +142,11 @@ public function executeByDimensions(array $dimensions, \Traversable $entityIds):

$urlBatches = array_chunk($urls, $this->batchSize);
foreach ($urlBatches as $batchUrls) {
$this->prerenderClient->recacheUrls($batchUrls, $storeId);
$request = $this->prerenderRecachingManagementRequest->create();
$request->setBatchUrls($this->json->serialize($batchUrls));
$request->setStoreId((int) $storeId);
$request->setIndexerId(self::INDEXER_ID);
$this->massSchedule->publishMass('asynchronous.prerender.recaching', [[$request]]);
}
}
}
Loading
Loading