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 b1cd37e
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 45 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 @@ -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
84 changes: 59 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 @@ -69,6 +70,10 @@ public function run(): int
// @TODO : does not upgrade files in modules, translations if they have not a correct md5 (or crc32, or whatever) from previous version
for ($i = 0; $i < $this->container->getUpgradeConfiguration()->getNumberOfFilesPerCall(); ++$i) {
if (!$filesToUpgrade->getRemainingTotal()) {
if ($this->shouldClearCoreCache()) {
$this->clearCoreCache();
}

$this->next = TaskName::TASK_UPDATE_DATABASE;
$this->logger->info($this->translator->trans('All files upgraded. Now upgrading database...'));
$this->stepDone = true;
Expand Down Expand Up @@ -224,30 +229,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->getUpgrader()->getDestinationVersion();
$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 +263,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 All @@ -282,4 +288,32 @@ public function init(): void
{
// Do nothing. Overrides parent init for avoiding core to be loaded here.
}

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

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

/**
* @throws Exception
*/
private 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.');
}
}

0 comments on commit b1cd37e

Please sign in to comment.