Skip to content

Commit

Permalink
Improve state management
Browse files Browse the repository at this point in the history
  • Loading branch information
M0rgan01 committed Nov 22, 2024
1 parent a16d46d commit 726baf7
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 37 deletions.
1 change: 1 addition & 0 deletions classes/Commands/CreateBackupCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ protected function execute(InputInterface $input, OutputInterface $output): ?int
try {
$this->setupEnvironment($input, $output);

$this->upgradeContainer->getFileConfigurationStorage()->cleanAllBackupFiles();
$controller = new AllBackupTasks($this->upgradeContainer);
$controller->init();
$exitCode = $controller->run();
Expand Down
2 changes: 1 addition & 1 deletion classes/Commands/RestoreCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ protected function execute(InputInterface $input, OutputInterface $output): ?int
return ExitCode::SUCCESS;
}
}

$this->upgradeContainer->getFileConfigurationStorage()->cleanAllRestoreFiles();
$controller = new AllRestoreTasks($this->upgradeContainer);
$controller->setOptions([
'backup' => $backup,
Expand Down
28 changes: 14 additions & 14 deletions classes/Commands/UpdateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
use PrestaShop\Module\AutoUpgrade\Parameters\UpgradeConfiguration;
use PrestaShop\Module\AutoUpgrade\Task\ExitCode;
use PrestaShop\Module\AutoUpgrade\Task\Runner\AllUpdateTasks;
use PrestaShop\Module\AutoUpgrade\Task\TaskName;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
Expand All @@ -58,8 +59,7 @@ protected function configure(): void
->addOption('no-chain', null, InputOption::VALUE_NONE, 'Prevents chaining of update commands. The command will execute a task and then stop, logging the next command that needs to be run. You will need to manually restart the process to continue with the next step.')
->addOption('channel', null, InputOption::VALUE_REQUIRED, "Selects what update to run ('local' / 'online')")
->addOption('config-file-path', null, InputOption::VALUE_REQUIRED, 'Configuration file location for update.')
->addOption('action', null, InputOption::VALUE_REQUIRED, 'Advanced users only. Sets the step you want to start from (Default: UpgradeNow, see ' . DeveloperDocumentation::DEV_DOC_UPGRADE_CLI_URL . ' for other values available)')
->addOption('data', null, InputOption::VALUE_REQUIRED, 'Advanced users only. Contains the state of the update process encoded in base64');
->addOption('action', null, InputOption::VALUE_REQUIRED, 'Advanced users only. Sets the step you want to start from (Default: UpgradeNow, see ' . DeveloperDocumentation::DEV_DOC_UPGRADE_CLI_URL . ' for other values available)');
}

/**
Expand All @@ -77,9 +77,15 @@ protected function execute(InputInterface $input, OutputInterface $output): ?int
try {
$this->setupEnvironment($input, $output);

// in the case of commands containing the update status, it is not necessary to update the configuration
// also we do not want to repeat the update of the config in the recursive commands
if ($input->getOption('data') === null) {
$action = $input->getOption('action');

$isFirstUpdateProcess = $action === null || $action === TaskName::TASK_UPDATE_INITIALIZATION;
if ($isFirstUpdateProcess) {
$this->upgradeContainer->getFileConfigurationStorage()->cleanAllUpdateFiles();
}

// if we are not in chain mode or we are in the 1st step of the update, we update the configuration
if ($isFirstUpdateProcess || $noChainMode || (isset($chainMode) && !$chainMode)) {
$configPath = $input->getOption('config-file-path');
$exitCode = $this->loadConfiguration($configPath, $this->upgradeContainer);
if ($exitCode !== ExitCode::SUCCESS) {
Expand All @@ -91,8 +97,7 @@ protected function execute(InputInterface $input, OutputInterface $output): ?int
$this->logger->debug('Starting the update process.');
$controller = new AllUpdateTasks($this->upgradeContainer);
$controller->setOptions([
'data' => $input->getOption('data'),
'action' => $input->getOption('action'),
'action' => $action,
UpgradeConfiguration::CHANNEL => $input->getOption('channel'),
]);
$controller->init();
Expand Down Expand Up @@ -125,15 +130,10 @@ private function chainCommand(OutputInterface $output): int
if (preg_match('/--action=(\S+)/', $lastInfo, $actionMatches)) {
$action = $actionMatches[1];
$this->logger->debug('Action parameter found: ' . $action);
}

if (preg_match('/--data=(\S+)/', $lastInfo, $dataMatches)) {
$data = $dataMatches[1];
$this->logger->debug('Data parameter found: ' . $data);
}
if (empty($action) || empty($data)) {
} else {
throw new InvalidArgumentException('The command does not contain the necessary information to continue the update process.');
}

$new_string = str_replace('INFO - $ ', '', $lastInfo);
$decorationParam = $output->isDecorated() ? ' --ansi' : '';
system('php ' . $new_string . $decorationParam, $exitCode);
Expand Down
8 changes: 0 additions & 8 deletions classes/Task/AbstractTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,14 +105,6 @@ public function __construct(UpgradeContainer $container)
}
}

/**
* @return string base64 encoded data from AjaxResponse
*/
public function getEncodedResponse(): string
{
return base64_encode($this->getJsonResponse());
}

/**
* @return string Json encoded data from AjaxResponse
*/
Expand Down
12 changes: 2 additions & 10 deletions classes/Task/Runner/AllUpdateTasks.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,6 @@ public function setOptions(array $options): void

$this->container->getUpgradeConfiguration()->merge($config);
}

if (!empty($options['data'])) {
$this->container->getState()->importFromEncodedData($options['data']);
}
}

/**
Expand All @@ -101,15 +97,11 @@ protected function checkIfRestartRequested(AjaxResponse $response): bool
$this->logger->info('Restart requested. Please run the following command to continue your upgrade:');
$args = $_SERVER['argv'];
foreach ($args as $key => $arg) {
if (
strpos($arg, '--data') === 0
|| strpos($arg, '--action') === 0
|| strpos($arg, '--config-file-path') === 0
) {
if (strpos($arg, '--action') === 0 || strpos($arg, '--config-file-path') === 0) {
unset($args[$key]);
}
}
$this->logger->info('$ ' . implode(' ', $args) . ' --action=' . $response->getNext() . ' --data=' . $this->getEncodedResponse());
$this->logger->info('$ ' . implode(' ', $args) . ' --action=' . $response->getNext());

return true;
}
Expand Down
4 changes: 0 additions & 4 deletions classes/Task/Runner/SingleTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,6 @@ public function setOptions(array $options): void
if (!empty($options['action'])) {
$this->step = $options['action'];
}

if (!empty($options['data'])) {
$this->container->getState()->importFromEncodedData($options['data']);
}
}

protected function canContinue(): bool
Expand Down
2 changes: 2 additions & 0 deletions controllers/admin/AdminSelfUpgradeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,8 @@ public function init()
if (!$this->ajax) {
// removing temporary files before init state to make sure state is already available
$this->upgradeContainer->getFileConfigurationStorage()->cleanAllUpdateFiles();
$this->upgradeContainer->getFileConfigurationStorage()->cleanAllBackupFiles();
$this->upgradeContainer->getFileConfigurationStorage()->cleanAllRestoreFiles();
}

if (!$this->upgradeContainer->getState()->isInitialized()) {
Expand Down

0 comments on commit 726baf7

Please sign in to comment.