Skip to content

Commit

Permalink
Merge pull request #107 from magento-extensibility/MAGETWO-43787-cli-…
Browse files Browse the repository at this point in the history
…varnish-merchant-beta

[Extensibility] [merchant beta] MAGETWO-44438 clear varnish from CLI
  • Loading branch information
He, Joan(johe) committed Nov 3, 2015
2 parents 6ea9993 + 6969625 commit 3238f30
Show file tree
Hide file tree
Showing 15 changed files with 466 additions and 328 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,26 @@

namespace Magento\Backend\Console\Command;

use Magento\Framework\App\Cache\Manager;
use Magento\Framework\Event\ManagerInterface;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

abstract class AbstractCacheTypeManageCommand extends AbstractCacheManageCommand
{
/** @var ManagerInterface */
protected $eventManager;

/**
* @param Manager $cacheManager
* @param ManagerInterface $eventManager
*/
public function __construct(Manager $cacheManager, ManagerInterface $eventManager)
{
$this->eventManager = $eventManager;
parent::__construct($cacheManager);
}

/**
* Perform a cache management action on cache types
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ protected function configure()
protected function performAction(array $cacheTypes)
{
$this->cacheManager->clean($cacheTypes);
$this->eventManager->dispatch('adminhtml_cache_flush_system');
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ protected function configure()
protected function performAction(array $cacheTypes)
{
$this->cacheManager->flush($cacheTypes);
$this->eventManager->dispatch('adminhtml_cache_flush_all');
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,25 @@

class CacheCleanCommandTest extends \PHPUnit_Framework_TestCase
{
/**
* @var \Magento\Framework\App\Cache\Manager|\PHPUnit_Framework_MockObject_MockObject
*/
/** @var \Magento\Framework\App\Cache\Manager|\PHPUnit_Framework_MockObject_MockObject */
private $cacheManager;

/**
* @var CacheCleanCommand
*/
/** @var CacheCleanCommand */
private $command;

/** @var \Magento\Framework\Event\ManagerInterface | \PHPUnit_Framework_MockObject_MockObject */
private $eventManagerMock;

public function setUp()
{
$this->eventManagerMock = $this->getMock('\Magento\Framework\Event\ManagerInterface', [], [], '', false);
$this->cacheManager = $this->getMock('Magento\Framework\App\Cache\Manager', [], [], '', false);
$this->command = new CacheCleanCommand($this->cacheManager);
$this->command = new CacheCleanCommand($this->cacheManager, $this->eventManagerMock);
}

public function testExecute()
{
$this->eventManagerMock->expects($this->once())->method('dispatch')->with('adminhtml_cache_flush_system');
$this->cacheManager->expects($this->once())->method('getAvailableTypes')->willReturn(['A', 'B', 'C']);
$this->cacheManager->expects($this->once())->method('clean')->with(['A', 'B']);
$param = ['types' => ['A', 'B']];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,25 @@

class CacheFlushCommandTest extends \PHPUnit_Framework_TestCase
{
/**
* @var \Magento\Framework\App\Cache\Manager|\PHPUnit_Framework_MockObject_MockObject
*/
/** @var \Magento\Framework\App\Cache\Manager|\PHPUnit_Framework_MockObject_MockObject */
private $cacheManager;

/**
* @var CacheFlushCommand
*/
/** @var CacheFlushCommand */
private $command;

/** @var \Magento\Framework\Event\ManagerInterface | \PHPUnit_Framework_MockObject_MockObject */
private $eventManagerMock;

public function setUp()
{
$this->eventManagerMock = $this->getMock('\Magento\Framework\Event\ManagerInterface', [], [], '', false);
$this->cacheManager = $this->getMock('Magento\Framework\App\Cache\Manager', [], [], '', false);
$this->command = new CacheFlushCommand($this->cacheManager);
$this->command = new CacheFlushCommand($this->cacheManager, $this->eventManagerMock);
}

public function testExecute()
{
$this->eventManagerMock->expects($this->once())->method('dispatch')->with('adminhtml_cache_flush_all');
$this->cacheManager->expects($this->once())->method('getAvailableTypes')->willReturn(['A', 'B', 'C']);
$this->cacheManager->expects($this->once())->method('flush')->with(['A', 'B']);
$param = ['types' => ['A', 'B']];
Expand Down
91 changes: 8 additions & 83 deletions app/code/Magento/CacheInvalidate/Model/Observer.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,60 +22,22 @@ class Observer
*/
const DEFAULT_PORT = 80;

/**
* Application config object
*
* @var \Magento\Framework\App\Config\ScopeConfigInterface
*/
/** @var \Magento\Framework\App\Config\ScopeConfigInterface */
protected $config;

/**
* @var InvalidateLogger
*/
private $logger;

/**
* @var UriFactory
*/
protected $uriFactory;

/**
* @var SocketFactory
*/
protected $socketAdapterFactory;

/**
* @var DeploymentConfig
*/
private $deploymentConfig;

/**
* @var RequestInterface
*/
private $request;
/** @var PurgeCache */
private $purgeCache;

/**
* @param \Magento\PageCache\Model\Config $config
* @param UriFactory $uriFactory
* @param SocketFactory $socketAdapterFactory
* @param InvalidateLogger $logger
* @param DeploymentConfig $deploymentConfig
* @param RequestInterface $request
* @param PurgeCache $purgeCache
*/
public function __construct(
\Magento\PageCache\Model\Config $config,
UriFactory $uriFactory,
SocketFactory $socketAdapterFactory,
InvalidateLogger $logger,
DeploymentConfig $deploymentConfig,
RequestInterface $request
\Magento\CacheInvalidate\Model\PurgeCache $purgeCache
) {
$this->config = $config;
$this->uriFactory = $uriFactory;
$this->socketAdapterFactory = $socketAdapterFactory;
$this->logger = $logger;
$this->deploymentConfig = $deploymentConfig;
$this->request = $request;
$this->purgeCache = $purgeCache;
}

/**
Expand All @@ -96,7 +58,7 @@ public function invalidateVarnish(\Magento\Framework\Event\Observer $observer)
$tags[] = sprintf($pattern, preg_replace("~_\\d+$~", '', $tag));
$tags[] = sprintf($pattern, $tag);
}
$this->sendPurgeRequest(implode('|', array_unique($tags)));
$this->purgeCache->sendPurgeRequest(implode('|', array_unique($tags)));
}
}
}
Expand All @@ -111,44 +73,7 @@ public function invalidateVarnish(\Magento\Framework\Event\Observer $observer)
public function flushAllCache(\Magento\Framework\Event\Observer $observer)
{
if ($this->config->getType() == \Magento\PageCache\Model\Config::VARNISH && $this->config->isEnabled()) {
$this->sendPurgeRequest('.*');
$this->purgeCache->sendPurgeRequest('.*');
}
}

/**
* Send curl purge request
* to invalidate cache by tags pattern
*
* @param string $tagsPattern
* @return void
*/
protected function sendPurgeRequest($tagsPattern)
{
$uri = $this->uriFactory->create();
$socketAdapter = $this->socketAdapterFactory->create();
$servers = $this->deploymentConfig->get(ConfigOptionsListConstants::CONFIG_PATH_CACHE_HOSTS)
?: [['host' => $this->request->getHttpHost()]];
$headers = ['X-Magento-Tags-Pattern' => $tagsPattern];
$socketAdapter->setOptions(['timeout' => 10]);
foreach ($servers as $server) {
$port = isset($server['port']) ? $server['port'] : self::DEFAULT_PORT;
$uri->setScheme('http')
->setHost($server['host'])
->setPort($port);
try {
$socketAdapter->connect($server['host'], $port);
$socketAdapter->write(
'PURGE',
$uri,
'1.1',
$headers
);
$socketAdapter->close();
} catch (Exception $e) {
$this->logger->critical($e->getMessage(), compact('server', 'tagsPattern'));
}
}

$this->logger->execute(compact('servers', 'tagsPattern'));
}
}
79 changes: 79 additions & 0 deletions app/code/Magento/CacheInvalidate/Model/PurgeCache.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php
/**
* Copyright © 2015 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\CacheInvalidate\Model;

use Magento\Framework\Cache\InvalidateLogger;
use Magento\Framework\App\DeploymentConfig;

class PurgeCache
{
const HEADER_X_MAGENTO_TAGS_PATTERN = 'X-Magento-Tags-Pattern';

/**
* @var \Magento\PageCache\Model\Cache\Server
*/
protected $cacheServer;

/**
* @var \Magento\CacheInvalidate\Model\SocketFactory
*/
protected $socketAdapterFactory;

/**
* @var InvalidateLogger
*/
private $logger;

/**
* Constructor
*
* @param \Magento\PageCache\Model\Cache\Server $cacheServer
* @param \Magento\CacheInvalidate\Model\SocketFactory $socketAdapterFactory
* @param InvalidateLogger $logger
*/
public function __construct(
\Magento\PageCache\Model\Cache\Server $cacheServer,
\Magento\CacheInvalidate\Model\SocketFactory $socketAdapterFactory,
InvalidateLogger $logger
) {
$this->cacheServer = $cacheServer;
$this->socketAdapterFactory = $socketAdapterFactory;
$this->logger = $logger;
}

/**
* Send curl purge request
* to invalidate cache by tags pattern
*
* @param string $tagsPattern
* @return bool Return true if successful; otherwise return false
*/
public function sendPurgeRequest($tagsPattern)
{
$socketAdapter = $this->socketAdapterFactory->create();
$servers = $this->cacheServer->getUris();
$headers = [self::HEADER_X_MAGENTO_TAGS_PATTERN => $tagsPattern];
$socketAdapter->setOptions(['timeout' => 10]);
foreach ($servers as $server) {
try {
$socketAdapter->connect($server->getHost(), $server->getPort());
$socketAdapter->write(
'PURGE',
$server,
'1.1',
$headers
);
$socketAdapter->close();
} catch (\Exception $e) {
$this->logger->critical($e->getMessage(), compact('server', 'tagsPattern'));
return false;
}
}

$this->logger->execute(compact('servers', 'tagsPattern'));
return true;
}
}
18 changes: 0 additions & 18 deletions app/code/Magento/CacheInvalidate/Model/UriFactory.php

This file was deleted.

Loading

0 comments on commit 3238f30

Please sign in to comment.