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 31, 2024
1 parent 300248d commit ffb8ac7
Show file tree
Hide file tree
Showing 9 changed files with 86 additions and 53 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 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
4 changes: 3 additions & 1 deletion classes/Commands/UpdateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,12 @@ 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');

$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
10 changes: 0 additions & 10 deletions classes/Parameters/UpgradeFileNames.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,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 @@ -166,7 +157,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
10 changes: 10 additions & 0 deletions classes/Task/AbstractTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
use PrestaShop\Module\AutoUpgrade\Analytics;
use PrestaShop\Module\AutoUpgrade\Log\Logger;
use PrestaShop\Module\AutoUpgrade\UpgradeContainer;
use PrestaShop\Module\AutoUpgrade\Upgrader;
use PrestaShop\Module\AutoUpgrade\UpgradeTools\Translator;

abstract class AbstractTask
Expand Down Expand Up @@ -184,6 +185,15 @@ public function setErrorFlag(): void
public function init(): void
{
$this->container->initPrestaShopCore();

$isUpdateTask = self::TASK_TYPE === TaskType::TASK_TYPE_UPDATE;

if ($this->container->getUpgrader()->getChannel() === Upgrader::CHANNEL_LOCAL && $isUpdateTask) {
$this->container->loadLocalXml();
}

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

abstract public function run(): int;
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
52 changes: 27 additions & 25 deletions classes/Task/Update/UpdateFiles.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
namespace PrestaShop\Module\AutoUpgrade\Task\Update;

use Exception;
use PrestaShop\Module\AutoUpgrade\Exceptions\UpgradeException;
use PrestaShop\Module\AutoUpgrade\Parameters\UpgradeFileNames;
use PrestaShop\Module\AutoUpgrade\Progress\Backlog;
use PrestaShop\Module\AutoUpgrade\Task\AbstractTask;
Expand Down Expand Up @@ -224,30 +225,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 +259,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
17 changes: 9 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 @@ -471,6 +463,15 @@ public function getFilesystemAdapter(): FilesystemAdapter
return $this->filesystemAdapter;
}

/**
* @throws Exception
*/
public function loadLocalXml(): void
{
$archiveXml = $this->getUpgradeConfiguration()->getLocalChannelXml();
$this->fileLoader->addXmlMd5File($this->getUpgrader()->getDestinationVersion(), $this->getProperty(self::DOWNLOAD_PATH) . DIRECTORY_SEPARATOR . $archiveXml);
}

/**
* @throws Exception
*/
Expand Down
28 changes: 28 additions & 0 deletions classes/UpgradeTools/CoreUpgrader/CoreUpgrader.php
Original file line number Diff line number Diff line change
Expand Up @@ -851,4 +851,32 @@ protected function runCoreCacheClean(): void
$this->logger->info($this->container->getTranslator()->trans('Running opcache_reset'));
$this->container->resetOpcache();
}

/**
* @throws Exception
*/
protected function shouldClearCoreCache(): bool
{
$destinationVersion = $this->container->getUpgrader()->getDestinationVersion();

return version_compare($destinationVersion, '9.0.0') >= 0 && php_sapi_name() === 'cli';
}

/**
* @throws Exception
*/
protected function clearCoreCache(): void
{
$rootPath = $this->container->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.');
}
}
4 changes: 4 additions & 0 deletions classes/UpgradeTools/CoreUpgrader/CoreUpgrader80.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ protected function initConstants(): void
$this->forceRemovingFiles();
parent::initConstants();

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

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

0 comments on commit ffb8ac7

Please sign in to comment.