From 80b94c1ca62901de9de1d0237bd203cb570613e0 Mon Sep 17 00:00:00 2001 From: Cid Lopes Date: Tue, 7 Jun 2022 14:11:06 +0300 Subject: [PATCH 1/5] Move currencies building process to cronjob --- Cron/CurrenciesCron.php | 109 ++++++++++++++++++++++++ Model/Meta/Account/Settings/Builder.php | 7 +- etc/crontab.xml | 3 + 3 files changed, 113 insertions(+), 6 deletions(-) create mode 100644 Cron/CurrenciesCron.php diff --git a/Cron/CurrenciesCron.php b/Cron/CurrenciesCron.php new file mode 100644 index 000000000..c2156c91c --- /dev/null +++ b/Cron/CurrenciesCron.php @@ -0,0 +1,109 @@ + + * @copyright 2020 Nosto Solutions Ltd + * @license http://opensource.org/licenses/BSD-3-Clause BSD 3-Clause + * + */ + +namespace Nosto\Tagging\Cron; + +use Exception; +use Nosto\Operation\UpdateSettings; +use Nosto\Tagging\Helper\Account as NostoHelperAccount; +use Nosto\Tagging\Helper\Scope as NostoHelperScope; +use Nosto\Tagging\Logger\Logger as NostoLogger; +use Nosto\Tagging\Model\Meta\Account\Settings\Builder as NostoSettingsBuilder; +use Nosto\Tagging\Model\Meta\Account\Settings\Currencies\Builder as NostoCurrenciesBuilder; + +/** + * Cronjob class that periodically updates currencies used in the store + */ +class CurrenciesCron +{ + protected NostoLogger $logger; + private NostoCurrenciesBuilder $nostoCurrenciesBuilder; + private NostoHelperScope $nostoHelperScope; + private NostoHelperAccount $nostoHelperAccount; + private NostoSettingsBuilder $nostoSettingsBuilder; + + /** + * Rates constructor. + * + * @param NostoLogger $logger + * @param NostoHelperScope $nostoHelperScope + * @param NostoCurrenciesBuilder $nostoCurrenciesBuilder + * @param NostoHelperAccount $nostoHelperAccount + * @param NostoSettingsBuilder $nostoSettingsBuilder + */ + public function __construct( + NostoLogger $logger, + NostoHelperScope $nostoHelperScope, + NostoCurrenciesBuilder $nostoCurrenciesBuilder, + NostoHelperAccount $nostoHelperAccount, + NostoSettingsBuilder $nostoSettingsBuilder + ) { + $this->logger = $logger; + $this->nostoHelperScope = $nostoHelperScope; + $this->nostoCurrenciesBuilder = $nostoCurrenciesBuilder; + $this->nostoHelperAccount = $nostoHelperAccount; + $this->nostoSettingsBuilder = $nostoSettingsBuilder; + } + + public function execute(): void + { + $this->logger->info('Updating currencies to Nosto for all store views'); + foreach ($this->nostoHelperScope->getStores(false) as $store) { + $this->logger->info('Updating currencies for ' . $store->getName()); + if (!$account = $this->nostoHelperAccount->findAccount($store)) { + $this->logger->info(sprintf( + 'Skipping update; an account doesn\'t exist for %s', + $store->getName() + )); + continue; + } + + $settings = $this->nostoSettingsBuilder->build($store); + try { + $settings->setCurrencies($this->nostoCurrenciesBuilder->build($store)); + $service = new UpdateSettings($account); + $service->update($settings); + } catch (Exception $e) { + $this->logger->warning(sprintf( + 'Unable to update the currencies for the store view %s. Message was: %s', + $store->getName(), + $e->getMessage() + )); + } + } + } +} diff --git a/Model/Meta/Account/Settings/Builder.php b/Model/Meta/Account/Settings/Builder.php index 01c8a2300..7e7bcee2c 100644 --- a/Model/Meta/Account/Settings/Builder.php +++ b/Model/Meta/Account/Settings/Builder.php @@ -47,13 +47,11 @@ use Nosto\Tagging\Helper\Data as NostoDataHelper; use Nosto\Tagging\Helper\Variation as NostoVariationHelper; use Nosto\Tagging\Logger\Logger as NostoLogger; -use Nosto\Tagging\Model\Meta\Account\Settings\Currencies\Builder as NostoCurrenciesBuilder; class Builder { private NostoLogger $logger; private ManagerInterface $eventManager; - private NostoCurrenciesBuilder $nostoCurrenciesBuilder; private NostoHelperCurrency $nostoHelperCurrency; private NostoDataHelper $nostoDataHelper; private NostoVariationHelper $nostoVariationHelper; @@ -63,7 +61,6 @@ class Builder * @param NostoLogger $logger * @param ManagerInterface $eventManager * @param NostoHelperCurrency $nostoHelperCurrency - * @param NostoCurrenciesBuilder $nostoCurrenciesBuilder * @param NostoDataHelper $nostoDataHelper * @param NostoVariationHelper $nostoVariationHelper */ @@ -71,13 +68,11 @@ public function __construct( NostoLogger $logger, ManagerInterface $eventManager, NostoHelperCurrency $nostoHelperCurrency, - NostoCurrenciesBuilder $nostoCurrenciesBuilder, NostoDataHelper $nostoDataHelper, NostoVariationHelper $nostoVariationHelper ) { $this->logger = $logger; $this->eventManager = $eventManager; - $this->nostoCurrenciesBuilder = $nostoCurrenciesBuilder; $this->nostoHelperCurrency = $nostoHelperCurrency; $this->nostoDataHelper = $nostoDataHelper; $this->nostoVariationHelper = $nostoVariationHelper; @@ -102,7 +97,7 @@ public function build(Store $store) } elseif ($this->nostoDataHelper->isPricingVariationEnabled($store)) { $settings->setDefaultVariantId($this->nostoVariationHelper->getDefaultVariationCode()); } - $settings->setCurrencies($this->nostoCurrenciesBuilder->build($store)); + // Currencies are build in CurrenciesCron to avoid increased loading times when logging into the admin } catch (Exception $e) { $this->logger->exception($e); } diff --git a/etc/crontab.xml b/etc/crontab.xml index 3e9f51951..16bf83597 100644 --- a/etc/crontab.xml +++ b/etc/crontab.xml @@ -41,5 +41,8 @@ 4 * * * * + + 0 1 * * * + From c1814d9472feb12e187736cb3f6ce3e5e5f8dd3f Mon Sep 17 00:00:00 2001 From: Cid Lopes Date: Tue, 7 Jun 2022 14:12:55 +0300 Subject: [PATCH 2/5] Move currencies building process to a cronjob --- Cron/CurrenciesCron.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Cron/CurrenciesCron.php b/Cron/CurrenciesCron.php index c2156c91c..e07c7c2b7 100644 --- a/Cron/CurrenciesCron.php +++ b/Cron/CurrenciesCron.php @@ -91,9 +91,8 @@ public function execute(): void )); continue; } - - $settings = $this->nostoSettingsBuilder->build($store); try { + $settings = $this->nostoSettingsBuilder->build($store); $settings->setCurrencies($this->nostoCurrenciesBuilder->build($store)); $service = new UpdateSettings($account); $service->update($settings); From 92edeb933e032d9e3a2f584dd339e5c7ffab772e Mon Sep 17 00:00:00 2001 From: Cid Lopes Date: Tue, 7 Jun 2022 14:24:59 +0300 Subject: [PATCH 3/5] Move currencies building process to a cronjob --- etc/crontab.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/etc/crontab.xml b/etc/crontab.xml index 16bf83597..b3cb11b91 100644 --- a/etc/crontab.xml +++ b/etc/crontab.xml @@ -41,7 +41,7 @@ 4 * * * * - + 0 1 * * * From f4ae68925af4fbd098ef52840b38e3707e1f5552 Mon Sep 17 00:00:00 2001 From: Cid Lopes Date: Tue, 7 Jun 2022 14:42:04 +0300 Subject: [PATCH 4/5] Move currencies building process to a cronjob --- Cron/CurrenciesCron.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cron/CurrenciesCron.php b/Cron/CurrenciesCron.php index e07c7c2b7..c6e6aaa64 100644 --- a/Cron/CurrenciesCron.php +++ b/Cron/CurrenciesCron.php @@ -57,7 +57,7 @@ class CurrenciesCron private NostoSettingsBuilder $nostoSettingsBuilder; /** - * Rates constructor. + * CurrenciesCron constructor. * * @param NostoLogger $logger * @param NostoHelperScope $nostoHelperScope From cb4acf1f34ee63d45a2d6b56b72969f22a30ac8d Mon Sep 17 00:00:00 2001 From: Cid Lopes Date: Tue, 7 Jun 2022 15:08:44 +0300 Subject: [PATCH 5/5] Move currencies building process to a cronjob --- Cron/CurrenciesCron.php | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/Cron/CurrenciesCron.php b/Cron/CurrenciesCron.php index c6e6aaa64..f0238081f 100644 --- a/Cron/CurrenciesCron.php +++ b/Cron/CurrenciesCron.php @@ -84,24 +84,24 @@ public function execute(): void $this->logger->info('Updating currencies to Nosto for all store views'); foreach ($this->nostoHelperScope->getStores(false) as $store) { $this->logger->info('Updating currencies for ' . $store->getName()); - if (!$account = $this->nostoHelperAccount->findAccount($store)) { + if ($account = $this->nostoHelperAccount->findAccount($store)) { + try { + $settings = $this->nostoSettingsBuilder->build($store); + $settings->setCurrencies($this->nostoCurrenciesBuilder->build($store)); + $service = new UpdateSettings($account); + $service->update($settings); + } catch (Exception $e) { + $this->logger->error(sprintf( + 'Unable to update the currencies for the store view %s. Message was: %s', + $store->getName(), + $e->getMessage() + )); + } + } else { $this->logger->info(sprintf( 'Skipping update; an account doesn\'t exist for %s', $store->getName() )); - continue; - } - try { - $settings = $this->nostoSettingsBuilder->build($store); - $settings->setCurrencies($this->nostoCurrenciesBuilder->build($store)); - $service = new UpdateSettings($account); - $service->update($settings); - } catch (Exception $e) { - $this->logger->warning(sprintf( - 'Unable to update the currencies for the store view %s. Message was: %s', - $store->getName(), - $e->getMessage() - )); } } }