2

In Magento 2, what is the cleanest way to have the same CRON job run for every store, and pull relevant configuration settings from the appropriate store?

Would it be to add a loop inside Vendor/Module/Cron/Job.php, execute method, checking every store, and setting $this->storeManager->setCurrentStore at the top of the method?

Followed by,

$this->scopeConfig->getValue('...', \Magento\Store\Model\ScopeInterface::SCOPE_STORE)

2 Answers 2

3

Simply Inject the StoresConfig class in your Cron model class constructor, and read store config values with getStoresConfigByPath($path) for all stores. This will return the config values with the associated store ids:

Class File: /vendor/magento/module-store/Model/StoresConfig.php

public function getStoresConfigByPath($path)
{
    $stores = $this->_storeManager->getStores(true);
    $storeValues = [];
    /** @var $store \Magento\Store\Model\Store */
    foreach ($stores as $store) {
        try {
            $value = $this->_config->getValue($path, ScopeInterface::SCOPE_STORE, $store->getCode());
            $storeValues[$store->getId()] = $value;
        } catch (NoSuchEntityException $e) {
            // Store doesn't really exist, so move on.
            continue;
        }
    }
    return $storeValues;
}
0

Here is the following procedure to setup cron in Magento2 :

app/code/Vender/ModuleName/etc/cron_groups.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Cron:etc/cron_groups.xsd">
    <group id="cron_group_name">
        <schedule_generate_every>1</schedule_generate_every>
        <schedule_ahead_for>4</schedule_ahead_for>
        <schedule_lifetime>2</schedule_lifetime>
        <history_cleanup_every>10</history_cleanup_every>
        <history_success_lifetime>60</history_success_lifetime>
        <history_failure_lifetime>600</history_failure_lifetime>
    </group>
</config>

Create crontab.xml : app/code/Vendor/ModuleName/etc/crontab.xml

<?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="cron_group_name">
        <job name="cronname_api" instance="Vendor\ModuleName\Cron\Cron" method="execute">
            <schedule>* * * * *</schedule>
        </job>
        .......................
        ......................
    </group>
</config>

Create Cron.php app/code/Vendor/ModuleName/Cron/Cron.php

  <?php
    namespace Vendor\ModuleName\Cron;

    class Cron
    {
    public function execute()
    {
    // Paste your logic for multiple stores
   // Given example cron will run every minute execute() function 
    }
        }
2
  • Thank you, but how would I run particular code in execute() for every store, using system.xml values from each store?
    – djdy
    Commented Jan 23, 2018 at 14:18
  • As you mentioned above you can use ScopeConfigInterface , also define multiple jobs for each of your store at by using store code or Id its up to you, As I don't know what logic you actually want to deploy inside the cron's , But I can say you pick one of option and it will work for all conditions without blocker , either ScopeConfigInterface Or Multiple Cron Jobs . Commented Jan 23, 2018 at 14:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.