Skip to content
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
43 changes: 13 additions & 30 deletions Console/Command/WarmNodeCommand.php
Original file line number Diff line number Diff line change
@@ -1,35 +1,21 @@
<?php

namespace MageOps\NodeWarmer\Console\Command;
declare(strict_types=1);

namespace MageOps\NodeWarmer\Console\Command;

class WarmNodeCommand extends \Symfony\Component\Console\Command\Command
{
/**
* @var \Magento\Framework\App\State
*/
private $state;

/**
* @var \MageOps\NodeWarmer\Service\NodeWarmer
*/
private $nodeWarmer;

public function __construct(
\Magento\Framework\App\State $state,
\MageOps\NodeWarmer\Service\NodeWarmer $nodeWarmer
)
{
parent::__construct();

$this->state = $state;
$this->nodeWarmer = $nodeWarmer;
protected \Magento\Framework\App\State $state,
protected \MageOps\NodeWarmer\Service\NodeWarmer $nodeWarmer,
protected \Magento\Framework\Filesystem\DriverInterface $filesystemDriver,
?string $name = null
) {
parent::__construct($name);
}

/**
* {@inheritdoc}
*/
protected function configure()
protected function configure(): void
{
$this
->setName('cs:warm-node')
Expand All @@ -38,32 +24,29 @@ protected function configure()
->addOption('local-url', 'u', \Symfony\Component\Console\Input\InputOption::VALUE_REQUIRED, 'Url of the local app instance', 'http://localhost:80');
}

private function setAreaCode()
private function setAreaCode(): void
{
$this->state->setAreaCode(\Magento\Framework\App\Area::AREA_FRONTEND);
}

/**
* {@inheritdoc}
*/
protected function execute(
\Symfony\Component\Console\Input\InputInterface $input,
\Symfony\Component\Console\Output\OutputInterface $output
)
): int
{
$this->setAreaCode();

$force = $input->getOption('force');
$localUrl = trim($input->getOption('local-url'), '"');

try {
@$this->nodeWarmer->warmNodeUp($localUrl, $force);
@$this->nodeWarmer->warmNodeUp($localUrl, $force); // phpcs:ignore
$output->writeln(sprintf('Done, output saved to "%s"', $this->nodeWarmer->getWarmupLogFilePath()));
return \Magento\Framework\Console\Cli::RETURN_SUCCESS;
} catch (\Exception $exception) {
$message = sprintf('Warmup did not complete, generated WARMUP file anyway: %s', (string)$exception);
$output->writeln($message);
file_put_contents($this->nodeWarmer->getWarmupLogFilePath(), $message);
$this->filesystemDriver->filePutContents($this->nodeWarmer->getWarmupLogFilePath(), $message);
return \Magento\Framework\Console\Cli::RETURN_FAILURE;
}
}
Expand Down
33 changes: 9 additions & 24 deletions Log/CapturingLoggerDecorator.php
Original file line number Diff line number Diff line change
@@ -1,46 +1,31 @@
<?php

declare(strict_types=1);

namespace MageOps\NodeWarmer\Log;

class CapturingLoggerDecorator extends \Psr\Log\AbstractLogger implements \Psr\Log\LoggerInterface
{
/**
* @var \Psr\Log\LoggerInterface
*/
private $upstreamLogger;

/**
* @var array
*/
private $buffer = [];

/**
* @param \Psr\Log\LoggerInterface $upstreamLogger
*/
public function __construct(\Psr\Log\LoggerInterface $upstreamLogger)
{
$this->upstreamLogger = $upstreamLogger;
protected array $buffer = [];
public function __construct(
protected \Psr\Log\LoggerInterface $upstreamLogger
) {
}

/**
* @return array
*/
public function flush()
public function flush(): array
{
$buffer = $this->buffer;
$this->buffer = [];

return $buffer;
}

/**
* @param mixed $level
* @param string|\Stringable $message
* @param array $context
*/
public function log($level, string|\Stringable $message, array $context = []): void
public function log($level, $message, array $context = []): void //phpcs:ignore
{
$this->upstreamLogger->log($level, $message, $context);
$this->buffer[] = [time(), $level, $message];
}
}
}
8 changes: 5 additions & 3 deletions Log/LogFormatter.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
<?php

declare(strict_types=1);

namespace MageOps\NodeWarmer\Log;

class LogFormatter implements \Monolog\Formatter\FormatterInterface
{
public function format(array $record)
public function format(array $record): string
{
return sprintf('[%s] [%s] %s',
date('Y-m-d H:i:s', $record[0]),
Expand All @@ -13,8 +15,8 @@ public function format(array $record)
);
}

public function formatBatch(array $records)
public function formatBatch(array $records): string
{
return implode("\n", array_map([$this, 'format'], $records));
}
}
}
45 changes: 13 additions & 32 deletions Model/Config.php
Original file line number Diff line number Diff line change
@@ -1,76 +1,57 @@
<?php

declare(strict_types=1);

namespace MageOps\NodeWarmer\Model;

class Config
{
const CACHE_CODE_VERSION_PATH = 'node_warmer/cache_code_version';
const DEPLOYED_STATIC_CONTENT_VERSION_PATH = 'node_warmer/deployed_static_content_version';

/**
* @var \Magento\Config\Model\ResourceModel\Config\Data\CollectionFactory
*/
protected $configCollectionFactory;

/**
* @var \Magento\Framework\App\Config\Storage\WriterInterface
*/
protected $configWriter;
public const CACHE_CODE_VERSION_PATH = 'node_warmer/cache_code_version';
public const DEPLOYED_STATIC_CONTENT_VERSION_PATH = 'node_warmer/deployed_static_content_version';

public function __construct(
\Magento\Config\Model\ResourceModel\Config\Data\CollectionFactory $configCollectionFactory,
\Magento\Framework\App\Config\Storage\WriterInterface $configWriter
)
{
$this->configWriter = $configWriter;
$this->configCollectionFactory = $configCollectionFactory;
protected \Magento\Config\Model\ResourceModel\Config\Data\CollectionFactory $configCollectionFactory,
protected \Magento\Framework\App\Config\Storage\WriterInterface $configWriter
) {
}

/**
* @return bool
*/
public function getCacheCodeVersion()
public function getCacheCodeVersion(): bool
{
return $this->getUncachedConfigValue(self::CACHE_CODE_VERSION_PATH);
}

/**
* @param string $newVersion
*/
public function updateCacheCodeVersion($newVersion)
public function updateCacheCodeVersion(string $newVersion): void
{
$this->configWriter->save(self::CACHE_CODE_VERSION_PATH, $newVersion);
}

/**
* @return bool
*/
public function getDeployedStaticContentVersion()
public function getDeployedStaticContentVersion(): ?string
{
return $this->getUncachedConfigValue(self::DEPLOYED_STATIC_CONTENT_VERSION_PATH);
}

/**
* @param string $newVersion
*/
public function updateDeployedStaticContentVersion($newVersion)
public function updateDeployedStaticContentVersion(string $newVersion): void
{
$this->configWriter->save(self::DEPLOYED_STATIC_CONTENT_VERSION_PATH, $newVersion);
}

/**
* Standard ScopeConfig can return value cached in redis
* For this module we always need value directly from database
* @param $path
* @return string|null
*/
protected function getUncachedConfigValue($path): ?string {
protected function getUncachedConfigValue(string $path): ?string {
$configCollection = $this->configCollectionFactory->create();
$configCollection->addFieldToFilter('path', ['eq' => $path]);

$config = $configCollection->getFirstItem();

if($config === null) {
if ($config === null) {
return null;
}

Expand Down
Loading