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

MAGECLOUD-1162: [GitHub] Deleting var/log while still writing to var/log/cloud.log #113

Merged
merged 3 commits into from
Dec 11, 2017
Merged
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
34 changes: 33 additions & 1 deletion src/Process/Build/BackupData.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/
namespace Magento\MagentoCloud\Process\Build;

use Magento\MagentoCloud\App\Logger\Pool as LoggerPool;
use Magento\MagentoCloud\Config\Environment;
use Magento\MagentoCloud\Filesystem\DirectoryList;
use Magento\MagentoCloud\Filesystem\Driver\File;
Expand Down Expand Up @@ -40,22 +41,30 @@ class BackupData implements ProcessInterface
*/
private $directoryList;

/**
* @var LoggerPool
*/
private $loggerPool;

/**
* @param File $file
* @param LoggerInterface $logger
* @param Environment $environment
* @param DirectoryList $directoryList
* @param LoggerPool $loggerPool
*/
public function __construct(
File $file,
LoggerInterface $logger,
Environment $environment,
DirectoryList $directoryList
DirectoryList $directoryList,
LoggerPool $loggerPool
) {
$this->file = $file;
$this->logger = $logger;
$this->environment = $environment;
$this->directoryList = $directoryList;
$this->loggerPool = $loggerPool;
}

/**
Expand Down Expand Up @@ -96,6 +105,8 @@ public function execute()

$this->logger->info('Copying writable directories to temp directory.');

$this->stopLogging();

foreach ($this->environment->getWritableDirectories() as $dir) {
$originalDir = $magentoRoot . $dir;
$initDir = $rootInitDir . $dir;
Expand All @@ -109,5 +120,26 @@ public function execute()
$this->file->createDirectory($originalDir);
}
}

$this->restoreLogging();
}

/**
* Removes all log handlers for closing all connections to files that are opened for logging.
*
* It's done for avoiding file system exceptions while file opened for writing is not physically exists
* and some process trying to write into that file.
*/
private function stopLogging()
{
$this->logger->setHandlers([]);
}

/**
* Restore all log handlers.
*/
private function restoreLogging()
{
$this->logger->setHandlers($this->loggerPool->getHandlers());
}
}
21 changes: 20 additions & 1 deletion src/Test/Unit/Process/Build/BackupDataTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/
namespace Magento\MagentoCloud\Test\Unit\Process\Build;

use Magento\MagentoCloud\App\Logger\Pool as LoggerPool;
use Magento\MagentoCloud\Config\Environment;
use Magento\MagentoCloud\Filesystem\DirectoryList;
use Magento\MagentoCloud\Filesystem\Driver\File;
Expand Down Expand Up @@ -42,6 +43,11 @@ class BackupDataTest extends TestCase
*/
private $directoryListMock;

/**
* @var LoggerPool|\PHPUnit_Framework_MockObject_MockObject
*/
private $loggerPoolMock;

/**
* @inheritdoc
*/
Expand All @@ -51,13 +57,15 @@ protected function setUp()
->disableOriginalConstructor()
->getMock();
$this->loggerMock = $this->getMockBuilder(LoggerInterface::class)
->setMethods(['setHandlers', 'info'])
->getMockForAbstractClass();
$this->environmentMock = $this->getMockBuilder(Environment::class)
->disableOriginalConstructor()
->getMock();
$this->directoryListMock = $this->getMockBuilder(DirectoryList::class)
->disableOriginalConstructor()
->getMock();
$this->loggerPoolMock = $this->createMock(LoggerPool::class);

$this->directoryListMock->expects($this->once())
->method('getMagentoRoot')
Expand All @@ -66,11 +74,22 @@ protected function setUp()
->method('getInit')
->willReturn('magento_root/init');

$this->loggerPoolMock->expects($this->once())
->method('getHandlers')
->willReturn(['handler1', 'handler2']);
$this->loggerMock->expects($this->exactly(2))
->method('setHandlers')
->withConsecutive(
[[]],
[['handler1', 'handler2']]
);

$this->process = new BackupData(
$this->fileMock,
$this->loggerMock,
$this->environmentMock,
$this->directoryListMock
$this->directoryListMock,
$this->loggerPoolMock
);
}

Expand Down