Skip to content

Commit

Permalink
Rework UpdateFiles warmup
Browse files Browse the repository at this point in the history
  • Loading branch information
Morgan Pichat committed Oct 30, 2024
1 parent d48a8b4 commit 1e69510
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 57 deletions.
10 changes: 3 additions & 7 deletions classes/Commands/AbstractCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@
namespace PrestaShop\Module\AutoUpgrade\Commands;

use Exception;
use http\Exception\InvalidArgumentException;
use PrestaShop\Module\AutoUpgrade\ErrorHandler;
use PrestaShop\Module\AutoUpgrade\Log\CliLogger;
use PrestaShop\Module\AutoUpgrade\Log\Logger;
use PrestaShop\Module\AutoUpgrade\Task\ExitCode;
use PrestaShop\Module\AutoUpgrade\Task\Miscellaneous\UpdateConfig;
use PrestaShop\Module\AutoUpgrade\UpgradeContainer;
use Symfony\Component\Console\Command\Command;
Expand Down Expand Up @@ -94,17 +94,13 @@ protected function loadConfiguration(?string $configPath, UpgradeContainer $upgr
$this->logger->debug('Loading configuration from ' . $configPath);
$configFile = file_get_contents($configPath);
if (!$configFile) {
$this->logger->error('Configuration file not found a location ' . $configPath);

return ExitCode::FAIL;
throw new InvalidArgumentException('Configuration file not found a location ' . $configPath);
}

$inputData = json_decode($configFile, true);

if (!$inputData) {
$this->logger->error('An error occurred during the json decode process, please check the content and syntax of the file content');

return ExitCode::FAIL;
throw new InvalidArgumentException('An error occurred during the json decode process, please check the content and syntax of the file content');
}

$this->logger->debug('Configuration file content: ' . json_encode($inputData));
Expand Down
39 changes: 38 additions & 1 deletion classes/Commands/UpdateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,11 @@

use Exception;
use PrestaShop\Module\AutoUpgrade\DeveloperDocumentation;
use PrestaShop\Module\AutoUpgrade\Exceptions\UpgradeException;
use PrestaShop\Module\AutoUpgrade\Task\ExitCode;
use PrestaShop\Module\AutoUpgrade\Task\Runner\AllUpdateTasks;
use PrestaShop\Module\AutoUpgrade\Task\TaskName;
use PrestaShop\Module\AutoUpgrade\UpgradeContainer;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
Expand Down Expand Up @@ -89,10 +92,16 @@ protected function execute(InputInterface $input, OutputInterface $output): ?int

$this->logger->debug('Configuration loaded successfully.');
$this->logger->debug('Starting the update process.');
$action = $input->getOption('action');

if ($action && $this->shouldClearCoreCache($action)) {
$this->clearCoreCache();
}

$controller = new AllUpdateTasks($this->upgradeContainer);
$controller->setOptions([
'data' => $input->getOption('data'),
'action' => $input->getOption('action'),
'action' => $action,
'channel' => $input->getOption('channel'),
]);
$controller->init();
Expand Down Expand Up @@ -146,4 +155,32 @@ private function chainCommand(OutputInterface $output): int

return ExitCode::SUCCESS;
}

/**
* @throws Exception
*/
private function shouldClearCoreCache(string $action): bool
{
$destinationVersion = $this->upgradeContainer->getState()->getInstallVersion();

return $action === TaskName::TASK_UPDATE_DATABASE && version_compare($destinationVersion, '9.0.0') >= 0;
}

/**
* @throws Exception
*/
private function clearCoreCache(): void
{
$rootPath = $this->upgradeContainer->getProperty(UpgradeContainer::PS_ROOT_PATH);
$command = 'php ' . $rootPath . '/bin/console cache:warmup --env=prod';
$output = [];
$resultCode = 0;

exec($command, $output, $resultCode);

if ($resultCode !== 0) {
throw new UpgradeException("An error was raised when clearing the core cache: \n" . implode("\n", $output));
}
$this->logger->debug('Core cache has been cleared to avoid dependency conflicts.');
}
}
10 changes: 0 additions & 10 deletions classes/Parameters/UpgradeFileNames.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,6 @@ class UpgradeFileNames
*/
const MODULES_TO_UPGRADE_LIST = 'modulesToUpgrade.list';

/**
* during upgradeFiles process,
* this files contains the list of files left to upgrade in a serialized array.
* (this file is deleted in init() method if you reload the page).
*
* @var string
*/
const FILES_DIFF_LIST = 'filesDiff.list';

/**
* during backupFiles process,
* this files contains the list of files left to save in a serialized array.
Expand Down Expand Up @@ -157,7 +148,6 @@ class UpgradeFileNames
public static $tmp_files = [
'QUERIES_TO_UPGRADE_LIST', // used ?
'FILES_TO_UPGRADE_LIST',
'FILES_DIFF_LIST',
'FILES_TO_BACKUP_LIST',
'DB_TABLES_TO_BACKUP_LIST',
'QUERIES_TO_RESTORE_LIST',
Expand Down
4 changes: 2 additions & 2 deletions classes/Task/Miscellaneous/CompareReleases.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,15 @@
namespace PrestaShop\Module\AutoUpgrade\Task\Miscellaneous;

use Exception;
use PrestaShop\Module\AutoUpgrade\Parameters\UpgradeFileNames;
use PrestaShop\Module\AutoUpgrade\Task\AbstractTask;
use PrestaShop\Module\AutoUpgrade\Task\ExitCode;
use PrestaShop\Module\AutoUpgrade\Task\TaskName;

/**
* This class gets the list of all modified and deleted files between current version
* and target version (according to channel configuration).
*
* TODO Task to remove after removing the old UI
*/
class CompareReleases extends AbstractTask
{
Expand All @@ -57,7 +58,6 @@ public function run(): int
$this->nextParams['status'] = 'error';
$this->nextParams['msg'] = sprintf('Unable to generate diff file list between %1$s and %2$s.', _PS_VERSION_, $version);
} else {
$this->container->getFileConfigurationStorage()->save($diffFileList, UpgradeFileNames::FILES_DIFF_LIST);
$this->nextParams['msg'] = $this->translator->trans(
'%modifiedfiles% files will be modified, %deletedfiles% files will be deleted (if they are found).',
[
Expand Down
3 changes: 0 additions & 3 deletions classes/Task/Miscellaneous/UpdateConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,6 @@ public function run(): int
return ExitCode::FAIL;
}

$this->container->getState()->setInstallVersion($this->container->getUpgrader()->getDestinationVersion());
$this->container->getState()->setOriginVersion($this->container->getProperty(UpgradeContainer::PS_VERSION));

return ExitCode::SUCCESS;
}

Expand Down
51 changes: 26 additions & 25 deletions classes/Task/Update/UpdateFiles.php
Original file line number Diff line number Diff line change
Expand Up @@ -224,30 +224,31 @@ protected function warmUp(): int
rename($newReleasePath . DIRECTORY_SEPARATOR . 'install-dev', $newReleasePath . DIRECTORY_SEPARATOR . 'install');
}

// Now, we will get the list of changed and removed files between the versions. This was generated previously by
// CompareReleases task.
$filepath_list_diff = $this->container->getProperty(UpgradeContainer::WORKSPACE_PATH) . DIRECTORY_SEPARATOR . UpgradeFileNames::FILES_DIFF_LIST;
$list_files_diff = [];

// We check if that file exists first and load it
if (file_exists($filepath_list_diff)) {
$list_files_diff = $this->container->getFileConfigurationStorage()->load(UpgradeFileNames::FILES_DIFF_LIST);
// $list_files_diff now contains an array with a list of changed and deleted files.
// We only keep list of files to delete. The modified files will be listed in list_files_to_upgrade below.
$list_files_diff = $list_files_diff['deleted'];

// Admin folder name in this deleted files list is standard /admin/.
// We will need to change it to our own admin folder name.
$admin_dir = trim(str_replace($this->container->getProperty(UpgradeContainer::PS_ROOT_PATH), '', $this->container->getProperty(UpgradeContainer::PS_ADMIN_PATH)), DIRECTORY_SEPARATOR);
foreach ($list_files_diff as $k => $path) {
if (preg_match('#autoupgrade#', $path)) {
unset($list_files_diff[$k]);
} elseif (substr($path, 0, 6) === '/admin') {
// Please make sure that the condition to check if the string starts with /admin stays here, because it was replacing
// admin even in the middle of a path, not deleting some files as a result.
// Also, do not use DIRECTORY_SEPARATOR, keep forward slash, because the path come from the XML standardized.
$list_files_diff[$k] = '/' . $admin_dir . substr($path, 6);
}
$destinationVersion = $this->container->getState()->getInstallVersion();
$originVersion = $this->container->getState()->getOriginVersion();
$this->logger->debug(sprintf('Generate diff file list between %s and %s.', $originVersion, $destinationVersion));
$diffFileList = $this->container->getChecksumCompare()->getFilesDiffBetweenVersions($originVersion, $destinationVersion);
if (!is_array($diffFileList)) {
$this->logger->error($this->translator->trans('Unable to generate diff file list between %s and %s.', [$originVersion, $destinationVersion]));
$this->next = TaskName::TASK_ERROR;

return ExitCode::FAIL;
}
// $diffFileList now contains an array with a list of changed and deleted files.
// We only keep list of files to delete. The modified files will be listed in list_files_to_upgrade below.
$diffFileList = $diffFileList['deleted'];

// Admin folder name in this deleted files list is standard /admin/.
// We will need to change it to our own admin folder name.
$admin_dir = trim(str_replace($this->container->getProperty(UpgradeContainer::PS_ROOT_PATH), '', $this->container->getProperty(UpgradeContainer::PS_ADMIN_PATH)), DIRECTORY_SEPARATOR);
foreach ($diffFileList as $k => $path) {
if (preg_match('#autoupgrade#', $path)) {
unset($diffFileList[$k]);
} elseif (substr($path, 0, 6) === '/admin') {
// Please make sure that the condition to check if the string starts with /admin stays here, because it was replacing
// admin even in the middle of a path, not deleting some files as a result.
// Also, do not use DIRECTORY_SEPARATOR, keep forward slash, because the path come from the XML standardized.
$diffFileList[$k] = '/' . $admin_dir . substr($path, 6);
}
}

Expand All @@ -257,7 +258,7 @@ protected function warmUp(): int
);

// Add our previously created list of deleted files
$list_files_to_upgrade = array_reverse(array_merge($list_files_diff, $list_files_to_upgrade));
$list_files_to_upgrade = array_reverse(array_merge($diffFileList, $list_files_to_upgrade));

$total_files_to_upgrade = count($list_files_to_upgrade);
$this->container->getFileConfigurationStorage()->save(
Expand Down
22 changes: 14 additions & 8 deletions classes/UpgradeContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -435,14 +435,6 @@ public function getUpgrader(): Upgrader
$currentPrestashopVersion
);

$this->getState()->setInstallVersion($upgrader->getDestinationVersion());
$this->getState()->setOriginVersion($this->getProperty(self::PS_VERSION));

if ($upgrader->getChannel() === Upgrader::CHANNEL_LOCAL) {
$archiveXml = $this->getUpgradeConfiguration()->getLocalChannelXml();
$this->fileLoader->addXmlMd5File($upgrader->getDestinationVersion(), $this->getProperty(self::DOWNLOAD_PATH) . DIRECTORY_SEPARATOR . $archiveXml);
}

$this->upgrader = $upgrader;

return $this->upgrader;
Expand Down Expand Up @@ -481,6 +473,10 @@ public function getFileLoader(): FileLoader
}

$this->fileLoader = new FileLoader();
if ($this->getUpgrader()->getChannel() === Upgrader::CHANNEL_LOCAL) {
$archiveXml = $this->getUpgradeConfiguration()->getLocalChannelXml();
$this->fileLoader->addXmlMd5File($this->getUpgrader()->getDestinationVersion(), $this->getProperty(self::DOWNLOAD_PATH) . DIRECTORY_SEPARATOR . $archiveXml);
}

return $this->fileLoader;
}
Expand Down Expand Up @@ -557,6 +553,8 @@ public function getCompletionCalculator(): CompletionCalculator

/**
* @return State
*
* @throws Exception
*/
public function getState(): State
{
Expand All @@ -566,6 +564,14 @@ public function getState(): State

$this->state = new State();

if (!$this->state->getOriginVersion()) {
$this->state->setOriginVersion($this->getProperty(self::PS_VERSION));
}

if (!$this->state->getInstallVersion()) {
$this->state->setInstallVersion($this->getUpgrader()->getDestinationVersion());
}

return $this->state;
}

Expand Down
1 change: 0 additions & 1 deletion classes/UpgradeTools/CoreUpgrader/CoreUpgrader80.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ protected function initConstants(): void
{
$this->forceRemovingFiles();
parent::initConstants();

// Container may be needed to run upgrade scripts
$this->container->getSymfonyAdapter()->initKernel();
}
Expand Down

0 comments on commit 1e69510

Please sign in to comment.