Skip to content

Commit

Permalink
FFWEB:913 Add cron execution frequency configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
a-laurowski authored and a.laurowski committed Jan 9, 2019
1 parent 01d75dc commit 15e493a
Show file tree
Hide file tree
Showing 9 changed files with 126 additions and 30 deletions.
1 change: 1 addition & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#ADD
- Added possibility to export additional attributes in separate columns
- Adds possibility to configure frequency of feed file generation by Cron

# v0.9-beta.8
## FIX
Expand Down
17 changes: 8 additions & 9 deletions Omikron/Factfinder/Cron/Feed.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,30 @@ class Feed
/** @var \Omikron\Factfinder\Model\Export\Product */
protected $productExport;

/** @var \Magento\Framework\App\Config\ScopeConfigInterface */
protected $scopeConfig;
/** @var \Omikron\Factfinder\Helper\Data */
protected $configHelper;

/**
* Feed constructor.
*
* @param \Omikron\Factfinder\Model\Export\Product $productExport
* @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
* @param \Omikron\Factfinder\Helper\Data $configHelper
*/
public function __construct(
\Omikron\Factfinder\Model\Export\Product $productExport,
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
)
{
\Omikron\Factfinder\Helper\Data $configHelper
) {
$this->productExport = $productExport;
$this->scopeConfig = $scopeConfig;
$this->configHelper = $configHelper;
}

/**
* Executed on Cron Run
*/
public function execute()
{
if ($this->scopeConfig->getValue('factfinder/data_transfer/ff_cron_enabled')) {
if ($this->configHelper->isCronEnabled()) {
$this->productExport->exportProducts(true);
}
}
}
}
9 changes: 9 additions & 0 deletions Omikron/Factfinder/Helper/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class Data extends AbstractHelper
const PATH_AUTH_POSTFIX = 'factfinder/general/authentication_postfix';
const PATH_ADVANCED_VERSION = 'factfinder/advanced/version';
const PATH_DATATRANSFER_IMPORT = 'factfinder/data_transfer/ff_cron_import';
const PATH_CONFIGURABLE_CRON_IS_ENABLED = 'factfinder/configurable_cron/ff_cron_enabled';

// Data Transfer
const PATH_FF_UPLOAD_URL_USER = 'factfinder/basic_auth_data_transfer/ff_upload_url_user';
Expand Down Expand Up @@ -399,6 +400,14 @@ public function getFFPushedproductscampaign()
return $this->scopeConfig->getValue('factfinder/components/ff_pushedproductscampaign', 'store');
}

/**
* @return bool
*/
public function isCronEnabled()
{
return (bool) $this->scopeConfig->getValue(self::PATH_CONFIGURABLE_CRON_IS_ENABLED);
}

/**
* Get configuration options telling if additional attributes should be merged and exported as single column or each attribute
* should be exported in separate column
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php
namespace Omikron\Factfinder\Model\Adminhtml\System\Config\Backend\Feed;

/**
* Class Frequency
*/
class Frequency extends \Magento\Framework\App\Config\Value
{
const PATH_CRON_TIME = 'ff_cron_time';
const PATH_CRON_IS_ENABLED = 'factfinder/configurable_cron/ff_cron_enabled';
const PATH_CRON_FREQUENCY = 'factfinder/configurable_cron/ff_cron_frequency';
const CRON_STRING_PATH = 'crontab/default/jobs/factfinder_feed_export/schedule/cron_expr';

/**
* @var \Magento\Framework\App\Config\Storage\WriterInterface
*/
protected $configWriter;

/**
* Frequency constructor.
*
* @param \Magento\Framework\Model\Context $context
* @param \Magento\Framework\Registry $registry
* @param \Magento\Framework\App\Config\ScopeConfigInterface $config
* @param \Magento\Framework\App\Cache\TypeListInterface $cacheTypeList
* @param \Magento\Framework\App\Config\Storage\WriterInterface $configWriter
* @param \Magento\Framework\Model\ResourceModel\AbstractResource|null $resource
* @param \Magento\Framework\Data\Collection\AbstractDb|null $resourceCollection
* @param array $data
*/
public function __construct(
\Magento\Framework\Model\Context $context,
\Magento\Framework\Registry $registry,
\Magento\Framework\App\Config\ScopeConfigInterface $config,
\Magento\Framework\App\Cache\TypeListInterface $cacheTypeList,
\Magento\Framework\App\Config\Storage\WriterInterface $configWriter,
\Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
\Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
array $data = []
) {
parent::__construct($context, $registry, $config, $cacheTypeList, $resource, $resourceCollection, $data);
$this->configWriter = $configWriter;
}

/**
* @return \Magento\Framework\App\Config\Value|void
*/
public function afterSave()
{
$time = $this->getFieldsetDataValue(self::PATH_CRON_TIME);
$frequency = $this->getValue();

$frequencyWeekly = \Magento\Cron\Model\Config\Source\Frequency::CRON_WEEKLY;
$frequencyMonthly = \Magento\Cron\Model\Config\Source\Frequency::CRON_MONTHLY;

$cronExprArray = array(
intval($time[1]),
intval($time[0]),
($frequency == $frequencyMonthly) ? '1' : '*',
'*',
($frequency == $frequencyWeekly) ? '1' : '*',
);
$cronExprString = join(' ', $cronExprArray);

try {
$this->configWriter->save(self::CRON_STRING_PATH, $cronExprString);
} catch (Exception $e) {
throw new Exception('Unable to save the cron expression.');
}

return $this;
}
}
26 changes: 21 additions & 5 deletions Omikron/Factfinder/etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -258,11 +258,6 @@
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
<comment>Runs an automatic import of the product data to the FACT-Finder servers, after the FTP upload is finished.</comment>
</field>
<field id="ff_cron_enabled" translate="label" type="select" sortOrder="120" showInDefault="1" showInWebsite="0" showInStore="0">
<label>Generate Export Files(s) automatically</label>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
<comment>If enabled, new export files will be created once daily at 1 a.m. system time.</comment>
</field>
</group>
<group id="basic_auth_data_transfer" translate="label" type="text" sortOrder="40" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Export via Basic Auth</label>
Expand All @@ -274,6 +269,27 @@
<backend_model>Magento\Config\Model\Config\Backend\Encrypted</backend_model>
</field>
</group>
<group id="configurable_cron" translate="label" type="text" sortOrder="130" showInDefault="1" showInWebsite="0" showInStore="0">
<label>Cron Schedule</label>
<field id="ff_cron_enabled" translate="label" type="select" sortOrder="120" showInDefault="1" showInWebsite="0" showInStore="0">
<label>Generate Export Files(s) automatically</label>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
</field>
<field id="ff_cron_time" translate="label comment" type="time" sortOrder="140" showInDefault="1" showInWebsite="0" showInStore="0">
<label>Start Time</label>
<depends>
<field id="ff_cron_enabled">1</field>
</depends>
</field>
<field id="ff_cron_frequency" translate="label comment" type="select" sortOrder="150" showInDefault="1" showInWebsite="0" showInStore="0">
<label>Cron execution frequency</label>
<source_model>Magento\Cron\Model\Config\Source\Frequency</source_model>
<backend_model>Omikron\Factfinder\Model\Adminhtml\System\Config\Backend\Feed\Frequency</backend_model>
<depends>
<field id="ff_cron_enabled">1</field>
</depends>
</field>
</group>
</section>
</system>
</config>
2 changes: 1 addition & 1 deletion Omikron/Factfinder/etc/crontab.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Cron:etc/crontab.xsd">
<group id="default">
<job name="factfinder_generate_feed" instance="Omikron\Factfinder\Cron\Feed" method="execute">
<job name="factfinder_feed_export" instance="Omikron\Factfinder\Cron\Feed" method="execute">
<schedule>0 1 * * *</schedule>
</job>
</group>
Expand Down
10 changes: 5 additions & 5 deletions Omikron/Factfinder/i18n/de_DE.csv
Original file line number Diff line number Diff line change
Expand Up @@ -85,16 +85,16 @@
"Success! Connection successfully tested!","Verbindung erfolgreich getestet!"
"Error! Connection could not be established. Please check your setup.","Verbindung konnte nicht aufgebaut werden. Bitte kontrollieren sie die Einstellungen."
"FACT-Finder error message:","FACT-Finder Fehlermeldung:"

"Generate Export File","Erstellung des Produktdaten Exports"
"Generate Export Files(s) automatically","Automatische Erstellung des Produktdaten Exports"
"If enabled, new export files will be created once daily at 1 a.m. system time.","Wenn dieses Feld aktiviert ist, wird der Produktdaten Export täglich um 1 Uhr Server-Zeit automatisch ausgeführt."
"Runs an automatic import of the product data to the FACT-Finder servers, after the FTP upload is finished.","Startet nach dem FTP Upload der Produktdaten den automatischen Import zum FACT-Finder Server."
"Automatic Import of productdata","Automatischer Import der Produktdaten"

"Yes","Ja"
"No","Nein

"No","Nein"
"Recommendations","Empfehlungen"
"Select Product Visibility","Wählen Sie Produktsichtbarkeit"
"Visibility attribute values used in filtering product collection.","Werte für die Sichtbarkeitsattribute, die bei der Filterung der Produktauswahl verwendet werden."
"Visibility attribute values used in filtering product collection.","Werte für die Sichtbarkeitsattribute, die bei der Filterung der Produktauswahl verwendet werden."
"Cron schedule", "Cron zeitplan"
"Start Time", "Startzeit"
"Cron execution frequency", "Cron Ausführungshäufigkeit"
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
18 changes: 8 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,19 +129,17 @@ For the option *Manufacturer*, choose the product attribute, which signifies the

The *Select additional Attributes* option offers a multiple-choice list of attributes. Select all of those you want added to the CSV file.

Before starting the export by clicking *Generate Export File(s) now*, you need to commit all changes by clicking “Save Config”.

You can also set the program to generate the product data export automatically. Activate the option *Generate Export Files(s) automatically* and the export is generated every day at 01:00 server time.

This file allows you to set other server times for the export:
```
Omikron/Factfinder/etc/crontab.xml
```
Before starting the export by clicking *Generate Export File(s) now*, you need to commit all changes by clicking “Save Config”.

![Product Data Export](Omikron/Factfinder/view/frontend/web/images/documentation/export-settings_en.jpg " Product Data Export")

In the file, change the entry `<schedule>0 1 * * *</schedule>` per your preferences. The time is defined with a cron expression. For more information about that topic, visit the [Wikipedia Page](https://en.wikipedia.org/wiki/Cron).
## Cron configuration

![Product Data Export](Omikron/Factfinder/view/frontend/web/images/documentation/export-settings_en.jpg " Product Data Export")
You can set the program to generate the product data export automatically. Activate the option *Generate Export Files(s) automatically* and the export is generated every day at 01:00 server time.

`<schedule>0 1 * * *</schedule>` is a default value however You can define your own cron expression in the module configuration at `Cron Schedule` section.

![Cron Configuration](Omikron/Factfinder/view/frontend/web/images/documentation/cron-configuration_en.jpg "Cron Configuration")

---

Expand Down

0 comments on commit 15e493a

Please sign in to comment.