Skip to content

Commit 4e3547d

Browse files
authored
MCLOUD-6135: Every step should throw only StepException (magento#745)
1 parent 68ffba1 commit 4e3547d

28 files changed

+552
-244
lines changed

src/Scenario/Processor.php

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
use Magento\MagentoCloud\OnFail\Action\ActionException;
1111
use Magento\MagentoCloud\OnFail\Action\ActionInterface;
1212
use Magento\MagentoCloud\Package\Manager;
13-
use Magento\MagentoCloud\Step\StepException;
1413
use Magento\MagentoCloud\Step\StepInterface;
1514
use Magento\MagentoCloud\Scenario\Exception\ProcessorException;
1615
use Psr\Log\LoggerInterface;
@@ -64,43 +63,55 @@ public function execute(array $scenarios): void
6463
implode(', ', $scenarios),
6564
$this->packageManager->getPrettyInfo()
6665
));
66+
$steps = [];
6767

6868
try {
6969
$this->mergedScenarios = $this->merger->merge($scenarios);
7070
$steps = $this->mergedScenarios['steps'];
71+
} catch (Throwable $exception) {
72+
$this->handleException(
73+
$exception,
74+
sprintf('Unhandled error: %s', $exception->getMessage())
75+
);
76+
}
7177

78+
try {
7279
array_walk($steps, function (StepInterface $step, string $name) {
7380
$this->logger->debug('Running step: ' . $name);
7481

7582
$step->execute();
7683

7784
$this->logger->debug(sprintf('Step "%s" finished', $name));
7885
});
79-
} catch (StepException $stepException) {
80-
try {
81-
$actions = $this->mergedScenarios['actions'];
82-
83-
array_walk($actions, function (ActionInterface $action, string $name) {
84-
$this->logger->debug('Running on fail action: ' . $name);
85-
86-
$action->execute();
87-
88-
$this->logger->debug(sprintf('On fail action "%s" finished', $name));
89-
});
90-
} catch (ActionException $actionException) {
91-
$this->logger->error($actionException->getMessage());
92-
}
86+
} catch (Throwable $stepException) {
87+
$this->runActions();
9388
$this->handleException($stepException);
94-
} catch (Throwable $exception) {
95-
$this->handleException(
96-
$exception,
97-
sprintf('Unhandled error: %s', $exception->getMessage())
98-
);
9989
}
10090

10191
$this->logger->info('Scenario(s) finished');
10292
}
10393

94+
/**
95+
* Run scenario actions.
96+
* @return void
97+
*/
98+
private function runActions(): void
99+
{
100+
try {
101+
$actions = $this->mergedScenarios['actions'];
102+
103+
array_walk($actions, function (ActionInterface $action, string $name) {
104+
$this->logger->debug('Running on fail action: ' . $name);
105+
106+
$action->execute();
107+
108+
$this->logger->debug(sprintf('On fail action "%s" finished', $name));
109+
});
110+
} catch (ActionException $actionException) {
111+
$this->logger->error($actionException->getMessage());
112+
}
113+
}
114+
104115
/**
105116
* Logs error message and throws ProcessorException
106117
*

src/StaticContent/ThreadCountOptimizer.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
use Magento\MagentoCloud\Config\StageConfigInterface;
1111
use Magento\MagentoCloud\Package\MagentoVersion;
12+
use Magento\MagentoCloud\Package\UndefinedPackageException;
1213
use Magento\MagentoCloud\Util\Cpu;
1314
use Psr\Log\LoggerInterface;
1415

@@ -65,6 +66,7 @@ public function __construct(LoggerInterface $logger, MagentoVersion $magentoVers
6566
* @param int $threads
6667
* @param string $strategy
6768
* @return int
69+
* @throws UndefinedPackageException
6870
*/
6971
public function optimize(int $threads, string $strategy): int
7072
{

src/Step/Build/DeployStaticContent.php

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Magento\MagentoCloud\Config\Validator\GlobalStage\ScdOnBuild;
1111
use Magento\MagentoCloud\Config\Validator\Result\Error;
1212
use Magento\MagentoCloud\Filesystem\Flag\Manager as FlagManager;
13+
use Magento\MagentoCloud\Step\StepException;
1314
use Magento\MagentoCloud\Step\StepInterface;
1415
use Psr\Log\LoggerInterface;
1516

@@ -61,23 +62,29 @@ public function __construct(
6162
*/
6263
public function execute()
6364
{
64-
$this->flagManager->delete(FlagManager::FLAG_STATIC_CONTENT_DEPLOY_IN_BUILD);
65+
try {
66+
$this->flagManager->delete(FlagManager::FLAG_STATIC_CONTENT_DEPLOY_IN_BUILD);
6567

66-
$result = $this->scdOnBuild->validate();
68+
$result = $this->scdOnBuild->validate();
6769

68-
if ($result instanceof Error) {
69-
$this->logger->notice('Skipping static content deploy: ' . $result->getError());
70+
if ($result instanceof Error) {
71+
$this->logger->notice('Skipping static content deploy: ' . $result->getError());
7072

71-
return;
72-
}
73+
return;
74+
}
7375

74-
$this->logger->notice('Generating fresh static content');
76+
$this->logger->notice('Generating fresh static content');
7577

76-
foreach ($this->steps as $step) {
77-
$step->execute();
78-
}
78+
foreach ($this->steps as $step) {
79+
$step->execute();
80+
}
7981

80-
$this->flagManager->set(FlagManager::FLAG_STATIC_CONTENT_DEPLOY_IN_BUILD);
81-
$this->logger->notice('End of generating fresh static content');
82+
$this->flagManager->set(FlagManager::FLAG_STATIC_CONTENT_DEPLOY_IN_BUILD);
83+
$this->logger->notice('End of generating fresh static content');
84+
} catch (StepException $e) {
85+
throw $e;
86+
} catch (\Exception $e) {
87+
throw new StepException($e->getMessage(), $e->getCode(), $e);
88+
}
8289
}
8390
}

src/Step/Build/DeployStaticContent/Generate.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
namespace Magento\MagentoCloud\Step\Build\DeployStaticContent;
99

1010
use Magento\MagentoCloud\App\Error;
11-
use Magento\MagentoCloud\Config\ConfigException;
11+
use Magento\MagentoCloud\App\GenericException;
1212
use Magento\MagentoCloud\Config\Stage\BuildInterface;
1313
use Magento\MagentoCloud\Step\StepException;
1414
use Magento\MagentoCloud\Step\StepInterface;
@@ -74,18 +74,18 @@ public function __construct(
7474
*/
7575
public function execute(): void
7676
{
77-
$locales = $this->buildOption->getLocales();
78-
$threadCount = $this->buildOption->getThreadCount();
77+
try {
78+
$locales = $this->buildOption->getLocales();
79+
$threadCount = $this->buildOption->getThreadCount();
7980

80-
$logMessage = 'Generating static content for locales: ' . implode(' ', $locales);
81+
$logMessage = 'Generating static content for locales: ' . implode(' ', $locales);
8182

82-
if ($threadCount) {
83-
$logMessage .= PHP_EOL . 'Using ' . $threadCount . ' Threads';
84-
}
83+
if ($threadCount) {
84+
$logMessage .= PHP_EOL . 'Using ' . $threadCount . ' Threads';
85+
}
8586

86-
$this->logger->info($logMessage);
87+
$this->logger->info($logMessage);
8788

88-
try {
8989
$commands = $this->commandFactory->matrix(
9090
$this->buildOption,
9191
$this->buildConfig->get(BuildInterface::VAR_SCD_MATRIX)
@@ -96,7 +96,7 @@ public function execute(): void
9696
}
9797
} catch (ShellException $e) {
9898
throw new StepException($e->getMessage(), Error::BUILD_SCD_FAILED, $e);
99-
} catch (ConfigException $e) {
99+
} catch (GenericException $e) {
100100
throw new StepException($e->getMessage(), $e->getCode(), $e);
101101
}
102102
}

src/Step/Build/PreBuild.php

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Magento\MagentoCloud\Filesystem\DirectoryList;
1111
use Magento\MagentoCloud\Filesystem\Driver\File;
1212
use Magento\MagentoCloud\Filesystem\Flag\Manager as FlagManager;
13+
use Magento\MagentoCloud\Step\StepException;
1314
use Magento\MagentoCloud\Step\StepInterface;
1415
use Magento\MagentoCloud\Config\Stage\BuildInterface;
1516
use Psr\Log\LoggerInterface;
@@ -70,29 +71,33 @@ public function __construct(
7071
*/
7172
public function execute()
7273
{
73-
$verbosityLevel = $this->stageConfig->get(BuildInterface::VAR_VERBOSE_COMMANDS);
74+
try {
75+
$verbosityLevel = $this->stageConfig->get(BuildInterface::VAR_VERBOSE_COMMANDS);
7476

75-
$generatedCode = $this->directoryList->getGeneratedCode();
76-
$generatedMetadata = $this->directoryList->getGeneratedMetadata();
77+
$generatedCode = $this->directoryList->getGeneratedCode();
78+
$generatedMetadata = $this->directoryList->getGeneratedMetadata();
7779

78-
$this->logger->info('Verbosity level is ' . ($verbosityLevel ?: 'not set'));
80+
$this->logger->info('Verbosity level is ' . ($verbosityLevel ?: 'not set'));
7981

80-
$this->flagManager->delete(FlagManager::FLAG_STATIC_CONTENT_DEPLOY_IN_BUILD);
82+
$this->flagManager->delete(FlagManager::FLAG_STATIC_CONTENT_DEPLOY_IN_BUILD);
8183

82-
if ($this->file->isExists($generatedCode)) {
83-
$this->logger->info(
84-
'Generated code exists from an old deployment - clearing it now.',
85-
['metadataPath' => $generatedCode]
86-
);
87-
$this->file->clearDirectory($generatedCode);
88-
}
84+
if ($this->file->isExists($generatedCode)) {
85+
$this->logger->info(
86+
'Generated code exists from an old deployment - clearing it now.',
87+
['metadataPath' => $generatedCode]
88+
);
89+
$this->file->clearDirectory($generatedCode);
90+
}
8991

90-
if ($this->file->isExists($generatedMetadata)) {
91-
$this->logger->info(
92-
'Generated metadata exists from an old deployment - clearing it now.',
93-
['metadataPath' => $generatedMetadata]
94-
);
95-
$this->file->clearDirectory($generatedMetadata);
92+
if ($this->file->isExists($generatedMetadata)) {
93+
$this->logger->info(
94+
'Generated metadata exists from an old deployment - clearing it now.',
95+
['metadataPath' => $generatedMetadata]
96+
);
97+
$this->file->clearDirectory($generatedMetadata);
98+
}
99+
} catch (\Exception $e) {
100+
throw new StepException($e->getMessage(), $e->getCode(), $e);
96101
}
97102
}
98103
}

src/Step/Deploy/InstallUpdate/ConfigUpdate/Amqp.php

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
namespace Magento\MagentoCloud\Step\Deploy\InstallUpdate\ConfigUpdate;
99

10+
use Magento\MagentoCloud\App\GenericException;
11+
use Magento\MagentoCloud\Step\StepException;
1012
use Magento\MagentoCloud\Step\StepInterface;
1113
use Magento\MagentoCloud\Config\Magento\Env\ReaderInterface as ConfigReader;
1214
use Magento\MagentoCloud\Config\Magento\Env\WriterInterface as ConfigWriter;
@@ -69,17 +71,21 @@ public function __construct(
6971
*/
7072
public function execute()
7173
{
72-
$config = $this->configReader->read();
73-
$amqpConfig = $this->amqpConfig->get();
74+
try {
75+
$config = $this->configReader->read();
76+
$amqpConfig = $this->amqpConfig->get();
7477

75-
if (count($amqpConfig)) {
76-
$this->logger->info('Updating env.php AMQP configuration.');
77-
$config['queue'] = $amqpConfig;
78-
$this->configWriter->create($config);
79-
} elseif (isset($config['queue'])) {
80-
$this->logger->info('Removing queue configuration from env.php.');
81-
unset($config['queue']);
82-
$this->configWriter->create($config);
78+
if (count($amqpConfig)) {
79+
$this->logger->info('Updating env.php AMQP configuration.');
80+
$config['queue'] = $amqpConfig;
81+
$this->configWriter->create($config);
82+
} elseif (isset($config['queue'])) {
83+
$this->logger->info('Removing queue configuration from env.php.');
84+
unset($config['queue']);
85+
$this->configWriter->create($config);
86+
}
87+
} catch (GenericException $e) {
88+
throw new StepException($e->getMessage(), $e->getCode(), $e);
8389
}
8490
}
8591
}

src/Step/Deploy/InstallUpdate/ConfigUpdate/CronConsumersRunner.php

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@
77

88
namespace Magento\MagentoCloud\Step\Deploy\InstallUpdate\ConfigUpdate;
99

10+
use Magento\MagentoCloud\App\GenericException;
1011
use Magento\MagentoCloud\Config\Environment;
1112
use Magento\MagentoCloud\Config\Magento\Env\ReaderInterface as ConfigReader;
1213
use Magento\MagentoCloud\Config\Magento\Env\WriterInterface as ConfigWriter;
1314
use Magento\MagentoCloud\Config\RepositoryFactory;
1415
use Magento\MagentoCloud\Config\Stage\DeployInterface;
1516
use Magento\MagentoCloud\Package\MagentoVersion;
17+
use Magento\MagentoCloud\Step\StepException;
1618
use Magento\MagentoCloud\Step\StepInterface;
1719
use Psr\Log\LoggerInterface;
1820

@@ -93,22 +95,26 @@ public function __construct(
9395
*/
9496
public function execute()
9597
{
96-
if (!$this->magentoVersion->isGreaterOrEqual('2.2')) {
97-
return;
98-
}
99-
100-
$this->logger->info('Updating env.php cron consumers runner configuration.');
101-
$config = $this->configReader->read();
102-
$runnerConfig = $this->repositoryFactory->create(
103-
$this->stageConfig->get(DeployInterface::VAR_CRON_CONSUMERS_RUNNER)
104-
);
98+
try {
99+
if (!$this->magentoVersion->isGreaterOrEqual('2.2')) {
100+
return;
101+
}
102+
103+
$this->logger->info('Updating env.php cron consumers runner configuration.');
104+
$config = $this->configReader->read();
105+
$runnerConfig = $this->repositoryFactory->create(
106+
$this->stageConfig->get(DeployInterface::VAR_CRON_CONSUMERS_RUNNER)
107+
);
105108

106-
$config['cron_consumers_runner'] = [
107-
'cron_run' => $runnerConfig->get('cron_run') === true,
108-
'max_messages' => $runnerConfig->get('max_messages', static::DEFAULT_MAX_MESSAGES),
109-
'consumers' => $runnerConfig->get('consumers', []),
110-
];
109+
$config['cron_consumers_runner'] = [
110+
'cron_run' => $runnerConfig->get('cron_run') === true,
111+
'max_messages' => $runnerConfig->get('max_messages', static::DEFAULT_MAX_MESSAGES),
112+
'consumers' => $runnerConfig->get('consumers', []),
113+
];
111114

112-
$this->configWriter->create($config);
115+
$this->configWriter->create($config);
116+
} catch (GenericException $e) {
117+
throw new StepException($e->getMessage(), $e->getCode(), $e);
118+
}
113119
}
114120
}

0 commit comments

Comments
 (0)