3

I see this in the Mage_Log config.xml

<crontab>
    <jobs>
        <log_clean>
            <run>
                <model>log/cron::logClean</model>
            </run>
        </log_clean>
    </jobs>
</crontab>

and noticed that the <schedule> tag is omitted. I know this cron job is schedule to run at what is set in the System > Configuration > Advanced > System > Log Cleaning configuration, but I don't see how this connection is made. How does Magento know when to run this cron based on the configuration in admin panel? I'm trying to do something similar for a custom module.

I'm using Magento EE 1.13.0.2.

3 Answers 3

4

The schedule tag would be omitted here because this cron task is disabled by default. In the admin (System -> Configuration -> System -> Log Cleaning) you'll find the flag to turn it on as well as an option to choose the run/schedule frequency.

When the task is disabled, the schedule config value is stored as NULL so that it never turns up in the cron_schedule table when it's not supposed to run.

As soon as you turn it on, you'll find a value in the core_config_data table for crontab/jobs/log_clean/schedule/cron_expr defining the frequency. This value will be honored just like the one in the xml file would normally be were it present.

Since you're wanting to do something similar here, I would suggest taking a look at the system.xml file from the Mage_Log module to see what scaffolding they have in place to setup the configuration options to store the schedule config value.

2
  • Ah, the <backend_model> in system.xml is what manages that. I was wondering what that did. Thanks! Commented Dec 27, 2013 at 17:59
  • i think this ansewer is missing the how it is done. Commented May 16, 2016 at 13:56
1

According to the Mage/Log/etc/config.xml file you can see it has invoked the logClean() function located in Mage_Log_Model_Cron class. Have a look at that function.

   /**
     * Clean logs
     *
     * @return Mage_Log_Model_Cron
     */
    public function logClean()
    {
        if (!Mage::getStoreConfigFlag(self::XML_PATH_LOG_CLEAN_ENABLED)) {
            return $this;
        }

        $this->_errors = array();

        try {
            Mage::getModel('log/log')->clean();
        }
        catch (Exception $e) {
            $this->_errors[] = $e->getMessage();
            $this->_errors[] = $e->getTrace();
        }

        $this->_sendLogCleanEmail();

        return $this;
    }

The following line in this function gets the configuration value for enabling the log.

Mage::getStoreConfigFlag(self::XML_PATH_LOG_CLEAN_ENABLED)

Here this XML_PATH_LOG_CLEAN_ENABLED = 'system/log/enabled' . So Magento knows whether the logging is enabled or not in the system configuration. Then it'll continue with the functions as per they have been invoked.

1
  • My question was how does Magento's Cron know when to schedule and run this cron job logClean()? You can set what time to run it at in System > Configuration, but I don't see where it grabs that data in code. Commented Dec 27, 2013 at 16:11
0

to add more precision here is where exactly the how log cron schedule is generated:

in th file: app/code/core/Mage/Log/Model/Resource/Log.php which has the function :

/**
 * Clean logs
 *
 * @param Mage_Log_Model_Log $object
 * @return Mage_Log_Model_Resource_Log
 */
public function clean(Mage_Log_Model_Log $object)
{
    $cleanTime = $object->getLogCleanTime(); // this is the line you want

    Mage::dispatchEvent('log_log_clean_before', array(
        'log'   => $object
    ));

    $this->_cleanVisitors($cleanTime);
    $this->_cleanCustomers($cleanTime);
    $this->_cleanUrls();

    Mage::dispatchEvent('log_log_clean_after', array(
        'log'   => $object
    ));

    return $this;
}

As you see above in the function Mage_Log_Model_Log::getLogCleanTime() all the magic happens :p

public function getLogCleanTime()
{
    return Mage::getStoreConfig(self::XML_LOG_CLEAN_DAYS) * 60 * 60 * 24; 
    //here is the formula :D
}

so it is now clear that the schedule is computed based on the configuration key :

const XML_LOG_CLEAN_DAYS = 'system/log/clean_after_day'

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.