Skip to content

Instantly share code, notes, and snippets.

@natrod
Last active August 29, 2015 14:22
Show Gist options
  • Save natrod/e00691bc30f4d7c55b4b to your computer and use it in GitHub Desktop.
Save natrod/e00691bc30f4d7c55b4b to your computer and use it in GitHub Desktop.
Catalog Rule script
<?php
require_once 'app/Mage.php';
ini_set('display_errors', 1);
ini_set("memory_limit","11364M");
Mage::setIsDeveloperMode(true);
$time_start = microtime(true); //get start time
$premem=memory_get_usage(true); //get initial memory usage
Mage::app();
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
try {
//Applying catalog Rules
Mage::getModel('catalogrule/rule')->applyAll();
Mage::app()->removeCache('catalog_rules_dirty');
//Message Body for status mail
$Report="Catalog Rules have been applied via cron job at ".date("d-m-y, H:i:s")."<br />".PHP_EOL;
} catch (Exception $e) {
$Report="Catalog Rules failure when applied via cron job at ".date("d-m-y, H:i:s")."<br />".PHP_EOL;
$date=date("Y-m-d-H-i-s");
$logname="catalog-rule-cron".$date.".log";
Mage::log(print_r($e->getMessage(),true),null,$logname);
}
$postmem=memory_get_usage(true); //memory after rules have been applied
$time_end = microtime(true); //time at completion
$tt=$time_end-$time_start; //total time taken
$tmem=echo_readable_memory_usage($postmem-$premem); // Difference between memory atscript end and script start
$Report.="TOTAL EXECUTION TIME ".$tt."<br />".PHP_EOL;
$Report.= "PRE MEM ".$premem."<br />".PHP_EOL;
$Report.= "POST MEM ".$postmem."<br />".PHP_EOL;
$Report.= "Total MEM Consumption ".$tmem."<br />".PHP_EOL;
$reciever="sample-email@test.com"; // To
$cc=array('sample-email2@test.com'); //If you want to cc the status to a few others
//Sending a status mail
$mail = new Zend_Mail();
$mail->setFrom("cronjobs@myshop.com","Catalog Rules Cron" );
$mail->addTo($reciever);
foreach($cc as $c )
{
$mail->addCc($c);
}
$mail->setSubject("Catalog Rule Cron Status".date("d-m-y"));
$mail->setBodyHtml($Report);
$mail->send();
function echo_readable_memory_usage($mem_usage) {
if ($mem_usage < 1024)
return $mem_usage." bytes";
elseif ($mem_usage < 1048576)
return round($mem_usage/1024,2)." KB";
else
return round($mem_usage/1048576,2)."MB";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment