Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move currencies building process to a cronjob #773

Merged
merged 5 commits into from
Jun 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 108 additions & 0 deletions Cron/CurrenciesCron.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
<?php

/**
* Copyright (c) 2020, Nosto Solutions Ltd
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors
* may be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* @author Nosto Solutions Ltd <[email protected]>
* @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;

/**
* CurrenciesCron 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)) {
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()
));
}
}
}
}
7 changes: 1 addition & 6 deletions Model/Meta/Account/Settings/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -63,21 +61,18 @@ class Builder
* @param NostoLogger $logger
* @param ManagerInterface $eventManager
* @param NostoHelperCurrency $nostoHelperCurrency
* @param NostoCurrenciesBuilder $nostoCurrenciesBuilder
* @param NostoDataHelper $nostoDataHelper
* @param NostoVariationHelper $nostoVariationHelper
*/
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;
Expand All @@ -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);
}
Expand Down
3 changes: 3 additions & 0 deletions etc/crontab.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,8 @@
<job name="nosto_exchange_rates" instance="Nosto\Tagging\Cron\RatesCron" method="execute">
<schedule>4 * * * *</schedule>
</job>
<job name="nosto_currencies" instance="Nosto\Tagging\Cron\CurrenciesCron" method="execute">
<schedule>0 1 * * *</schedule> <!-- Everyday at 1 am -->
</job>
</group>
</config>