Skip to content

Commit 34f3c24

Browse files
oshmyheliukshiftedreality
authored andcommitted
MAGECLOUD-2870: Static assets compression shouldn't break a deploy (#425)
1 parent d13162b commit 34f3c24

17 files changed

+140
-55
lines changed

dist/.magento.env.yaml

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,14 +114,25 @@
114114
# SCD_COMPRESSION_LEVEL - specifies which gzip compression level (0 to 9) to use when compressing static content; #
115115
# 0 disables compression. #
116116
# Magento Version: 2.1.4 and later #
117-
# Default value: 6 (build stage) or 4 (deploy stage) #
117+
# Default value: 6 (build stage) or 4 (deploy stage) #
118118
# Possible values: from 0 to 9 #
119119
# Stages: global, build and deploy #
120120
# Example: #
121121
# stage: #
122122
# deploy: #
123123
# SCD_COMPRESSION_LEVEL: 5 #
124124
#######################################################################################################################
125+
# SCD_COMPRESSION_TIMEOUT - determine in seconds maximum time for running static compression command #
126+
# by default this value is equal to 600 seconds. #
127+
# Magento Version: 2.1.4 and later #
128+
# Default value: 600 #
129+
# Possible values: > 0 #
130+
# Stages: global, build and deploy #
131+
# Example: #
132+
# stage: #
133+
# deploy: #
134+
# SCD_COMPRESSION_TIMEOUT: 800 #
135+
#######################################################################################################################
125136
# CLEAN_STATIC_FILES - cleans generated static files. By specifying the value of this configuration to 'false', #
126137
# you can leave the static files which were generated during the previous deployment. #
127138
# Magento Version: 2.1.4 and later #

src/App/Container.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Magento\MagentoCloud\Command\PostDeploy;
1515
use Magento\MagentoCloud\Config\Database\ConfigInterface;
1616
use Magento\MagentoCloud\Config\Database\MergedConfig;
17+
use Magento\MagentoCloud\Config\Schema;
1718
use Magento\MagentoCloud\Config\Validator as ConfigValidator;
1819
use Magento\MagentoCloud\Config\ValidatorInterface;
1920
use Magento\MagentoCloud\Filesystem\DirectoryCopier;
@@ -64,6 +65,7 @@ public function __construct(string $toolsBasePath, string $magentoBasePath)
6465
/**
6566
* Binding.
6667
*/
68+
$this->container->singleton(Schema::class);
6769
$this->container->singleton(DirectoryList::class);
6870
$this->container->singleton(FileList::class);
6971
$this->container->singleton(DeployProcess\InstallUpdate\ConfigUpdate\SearchEngine::class);

src/Config/Database/MergedConfig.php

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,13 @@ class MergedConfig implements ConfigInterface
5858
*/
5959
private $mergedConfig;
6060

61+
/**
62+
* Factory for creation database configurations
63+
*
64+
* @var RelationshipConnectionFactory
65+
*/
66+
private $connectionFactory;
67+
6168
/**
6269
* @param RelationshipConnectionFactory $connectionFactory
6370
* @param ConfigReader $configReader
@@ -72,7 +79,7 @@ public function __construct(
7279
DeployInterface $stageConfig,
7380
ConfigMerger $configMerger
7481
) {
75-
$this->connectionData = $connectionFactory->create(RelationshipConnectionFactory::CONNECTION_MAIN);
82+
$this->connectionFactory = $connectionFactory;
7683
$this->configReader = $configReader;
7784
$this->slaveConfig = $slaveConfig;
7885
$this->stageConfig = $stageConfig;
@@ -96,7 +103,7 @@ public function get(): array
96103
return $this->mergedConfig = $this->configMerger->clear($envDbConfig);
97104
}
98105

99-
if (!empty($this->connectionData->getHost())) {
106+
if (!empty($this->getConnectionData()->getHost())) {
100107
$dbConfig = $this->generateDbConfig();
101108
} else {
102109
$dbConfig = $this->getDbConfigFromEnvFile();
@@ -114,10 +121,10 @@ public function get(): array
114121
private function generateDbConfig(): array
115122
{
116123
$connectionData = [
117-
'username' => $this->connectionData->getUser(),
118-
'host' => $this->connectionData->getHost(),
119-
'dbname' => $this->connectionData->getDbName(),
120-
'password' => $this->connectionData->getPassword(),
124+
'username' => $this->getConnectionData()->getUser(),
125+
'host' => $this->getConnectionData()->getHost(),
126+
'dbname' => $this->getConnectionData()->getDbName(),
127+
'password' => $this->getConnectionData()->getPassword(),
121128
];
122129

123130
$dbConfig = [
@@ -155,9 +162,9 @@ public function isDbConfigurationCompatibleWithSlaveConnection(): bool
155162
$envDbConfig = $this->stageConfig->get(DeployInterface::VAR_DATABASE_CONFIGURATION);
156163

157164
if ((isset($envDbConfig['connection']['default']['host'])
158-
&& $envDbConfig['connection']['default']['host'] !== $this->connectionData->getHost())
165+
&& $envDbConfig['connection']['default']['host'] !== $this->getConnectionData()->getHost())
159166
|| (isset($envDbConfig['connection']['default']['dbname'])
160-
&& $envDbConfig['connection']['default']['dbname'] !== $this->connectionData->getDbName())
167+
&& $envDbConfig['connection']['default']['dbname'] !== $this->getConnectionData()->getDbName())
161168
) {
162169
return false;
163170
}
@@ -176,4 +183,18 @@ private function getDbConfigFromEnvFile(): array
176183
{
177184
return $this->configReader->read()['db'] ?? [];
178185
}
186+
187+
/**
188+
* Returns connection data from relationship array
189+
*
190+
* @return ConnectionInterface
191+
*/
192+
private function getConnectionData(): ConnectionInterface
193+
{
194+
if (!$this->connectionData instanceof ConnectionInterface) {
195+
$this->connectionData = $this->connectionFactory->create(RelationshipConnectionFactory::CONNECTION_MAIN);
196+
}
197+
198+
return $this->connectionData;
199+
}
179200
}

src/Config/Schema.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,18 @@ public function getSchema()
9696
StageConfigInterface::STAGE_DEPLOY => 4,
9797
],
9898
],
99+
StageConfigInterface::VAR_SCD_COMPRESSION_TIMEOUT => [
100+
self::SCHEMA_TYPE => ['integer'],
101+
self::SCHEMA_STAGE => [
102+
StageConfigInterface::STAGE_GLOBAL,
103+
StageConfigInterface::STAGE_BUILD,
104+
StageConfigInterface::STAGE_DEPLOY
105+
],
106+
self::SCHEMA_DEFAULT_VALUE => [
107+
StageConfigInterface::STAGE_BUILD => 600,
108+
StageConfigInterface::STAGE_DEPLOY => 600,
109+
],
110+
],
99111
StageConfigInterface::VAR_SCD_STRATEGY => [
100112
self::SCHEMA_TYPE => ['string'],
101113
self::SCHEMA_VALUE_VALIDATION => ['compact', 'quick', 'standard'],

src/Config/StageConfigInterface.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ interface StageConfigInterface
2727
* Deployment variables.
2828
*/
2929
const VAR_SCD_COMPRESSION_LEVEL = 'SCD_COMPRESSION_LEVEL';
30+
const VAR_SCD_COMPRESSION_TIMEOUT = 'SCD_COMPRESSION_TIMEOUT';
3031
const VAR_SCD_STRATEGY = 'SCD_STRATEGY';
3132
const VAR_SCD_THREADS = 'SCD_THREADS';
3233
const VAR_SKIP_SCD = 'SKIP_SCD';

src/DB/Connection.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
namespace Magento\MagentoCloud\DB;
77

88
use Magento\MagentoCloud\DB\Data\ConnectionFactory;
9-
use Magento\MagentoCloud\DB\Data\ConnectionInterface as DatabaseConnectionInterface;
109
use Psr\Log\LoggerInterface;
1110

1211
/**
@@ -32,9 +31,9 @@ class Connection implements ConnectionInterface
3231
private $fetchMode = \PDO::FETCH_ASSOC;
3332

3433
/**
35-
* @var DatabaseConnectionInterface
34+
* @var ConnectionFactory
3635
*/
37-
private $connectionData;
36+
private $connectionFactory;
3837

3938
/**
4039
* @param LoggerInterface $logger
@@ -43,7 +42,7 @@ class Connection implements ConnectionInterface
4342
public function __construct(LoggerInterface $logger, ConnectionFactory $connectionFactory)
4443
{
4544
$this->logger = $logger;
46-
$this->connectionData = $connectionFactory->create(ConnectionFactory::CONNECTION_MAIN);
45+
$this->connectionFactory = $connectionFactory;
4746
}
4847

4948
/**
@@ -184,14 +183,15 @@ private function connect()
184183
return;
185184
}
186185

186+
$connectionData = $this->connectionFactory->create(ConnectionFactory::CONNECTION_MAIN);
187187
$this->pdo = new \PDO(
188188
sprintf(
189189
'mysql:dbname=%s;host=%s',
190-
$this->connectionData->getDbName(),
191-
$this->connectionData->getHost()
190+
$connectionData->getDbName(),
191+
$connectionData->getHost()
192192
),
193-
$this->connectionData->getUser(),
194-
$this->connectionData->getPassword(),
193+
$connectionData->getUser(),
194+
$connectionData->getPassword(),
195195
[
196196
\PDO::ATTR_PERSISTENT => true,
197197
]

src/DB/Dump.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,26 @@
66
namespace Magento\MagentoCloud\DB;
77

88
use Magento\MagentoCloud\DB\Data\ConnectionFactory;
9-
use Magento\MagentoCloud\DB\Data\ConnectionInterface as DatabaseConnectionInterface;
109

1110
/**
1211
* Class Dump generate mysqldump command with read only connection
1312
*/
1413
class Dump implements DumpInterface
1514
{
1615
/**
17-
* Database connection data for read operations
16+
* Factory for creation database data connection classes
1817
*
19-
* @var DatabaseConnectionInterface
18+
* @var ConnectionFactory
2019
*/
21-
private $connectionData;
20+
private $connectionFactory;
2221

2322
/**
2423
* @param ConnectionFactory $connectionFactory
2524
*/
2625
public function __construct(
2726
ConnectionFactory $connectionFactory
2827
) {
29-
$this->connectionData = $connectionFactory->create(ConnectionFactory::CONNECTION_SLAVE);
28+
$this->connectionFactory = $connectionFactory;
3029
}
3130

3231
/**
@@ -36,19 +35,20 @@ public function __construct(
3635
*/
3736
public function getCommand(): string
3837
{
39-
$command = 'mysqldump -h ' . escapeshellarg($this->connectionData->getHost())
40-
. ' -u ' . escapeshellarg($this->connectionData->getUser());
38+
$connectionData = $this->connectionFactory->create(ConnectionFactory::CONNECTION_SLAVE);
39+
$command = 'mysqldump -h ' . escapeshellarg($connectionData->getHost())
40+
. ' -u ' . escapeshellarg($connectionData->getUser());
4141

42-
$port = $this->connectionData->getPort();
42+
$port = $connectionData->getPort();
4343
if (!empty($port)) {
4444
$command .= ' -P ' . escapeshellarg($port);
4545
}
4646

47-
$password = $this->connectionData->getPassword();
47+
$password = $connectionData->getPassword();
4848
if ($password) {
4949
$command .= ' -p' . escapeshellarg($password);
5050
}
51-
$command .= ' ' . escapeshellarg($this->connectionData->getDbName())
51+
$command .= ' ' . escapeshellarg($connectionData->getDbName())
5252
. ' --single-transaction --no-autocommit --quick';
5353

5454
return $command;

src/Process/Build/CompressStaticContent.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ public function execute()
7777
if ($this->flagManager->exists(FlagManager::FLAG_STATIC_CONTENT_DEPLOY_IN_BUILD)) {
7878
$this->compressor->process(
7979
$this->stageConfig->get(BuildInterface::VAR_SCD_COMPRESSION_LEVEL),
80+
$this->stageConfig->get(BuildInterface::VAR_SCD_COMPRESSION_TIMEOUT),
8081
$this->stageConfig->get(BuildInterface::VAR_VERBOSE_COMMANDS)
8182
);
8283
} else {

src/Process/Deploy/CompressStaticContent.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ public function execute()
8181
) {
8282
$this->staticContentCompressor->process(
8383
$this->stageConfig->get(DeployInterface::VAR_SCD_COMPRESSION_LEVEL),
84+
$this->stageConfig->get(DeployInterface::VAR_SCD_COMPRESSION_TIMEOUT),
8485
$this->stageConfig->get(DeployInterface::VAR_VERBOSE_COMMANDS)
8586
);
8687
} else {

src/Process/Deploy/InstallUpdate/ConfigUpdate/DbConnection.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
use Magento\MagentoCloud\Config\Deploy\Reader as ConfigReader;
1212
use Magento\MagentoCloud\Config\Deploy\Writer as ConfigWriter;
1313
use Magento\MagentoCloud\Config\Stage\DeployInterface;
14-
use Magento\MagentoCloud\DB\Data\ConnectionInterface;
1514
use Magento\MagentoCloud\DB\Data\RelationshipConnectionFactory;
1615
use Magento\MagentoCloud\Process\ProcessInterface;
1716
use Psr\Log\LoggerInterface;
@@ -57,9 +56,9 @@ class DbConnection implements ProcessInterface
5756
private $configMerger;
5857

5958
/**
60-
* @var ConnectionInterface
59+
* @var RelationshipConnectionFactory
6160
*/
62-
private $connectionData;
61+
private $connectionFactory;
6362

6463
/**
6564
* @param DeployInterface $stageConfig
@@ -88,7 +87,7 @@ public function __construct(
8887
$this->configReader = $configReader;
8988
$this->configMerger = $configMerger;
9089
$this->logger = $logger;
91-
$this->connectionData = $connectionFactory->create(RelationshipConnectionFactory::CONNECTION_MAIN);
90+
$this->connectionFactory = $connectionFactory;
9291
}
9392

9493
/**
@@ -112,8 +111,9 @@ public function execute()
112111
private function addLoggingAboutSlaveConnection()
113112
{
114113
$envDbConfig = $this->stageConfig->get(DeployInterface::VAR_DATABASE_CONFIGURATION);
114+
$connectionData = $this->connectionFactory->create(RelationshipConnectionFactory::CONNECTION_MAIN);
115115

116-
if (!$this->connectionData->getHost()
116+
if (!$connectionData->getHost()
117117
|| !$this->stageConfig->get(DeployInterface::VAR_MYSQL_USE_SLAVE_CONNECTION)
118118
|| (!$this->configMerger->isEmpty($envDbConfig) && !$this->configMerger->isMergeRequired($envDbConfig))
119119
) {

0 commit comments

Comments
 (0)