Skip to content

Instantly share code, notes, and snippets.

@drewgillson
Created September 14, 2012 08:51
Show Gist options
  • Save drewgillson/3720849 to your computer and use it in GitHub Desktop.
Save drewgillson/3720849 to your computer and use it in GitHub Desktop.
Refresh All Without Background Tasks
<?php
/**
* This new CLI script refreshes stock status, just do 'php erp.php refreshstockstatus' in /shell
* Optimized for minimal memory usage with one fix to the core BoostShop ERP code, which is the addition
* of $this->_product->clearInstance() to the end of MDN_SalesOrderPlanning_Model_ProductAvailabilityStatus::Refresh
**/
#/shell/erp.php
require_once 'abstract.php';
class Mage_Shell_Erp extends Mage_Shell_Abstract
{
public function run()
{
if (isset($this->_args['refreshstockstatus'])) {
echo "Loading product collection...\n";
$helper = Mage::helper('SalesOrderPlanning/ProductAvailabilityStatus');
$helper->RefreshAllWithoutBackgroundTasks();
} else {
echo $this->usageHelp();
}
}
public function usageHelp()
{
return <<<USAGE
Usage: php -f erp.php -- [options]
refreshstockstatus Refresh Stock Status
help This help
USAGE;
}
}
require_once '../app/Mage.php';
$shell = new Mage_Shell_Erp();
$shell->run();
?>
<?php
#/app/code/community/MDN/SalesOrderPlanning/Helper/ProductAvailabilityStatus.php
class MDN_SalesOrderPlanning_Helper_ProductAvailabilityStatus extends Mage_Core_Helper_Abstract {
# only method modified was ->RefreshAllWithoutBackgroundTasks()
/**
* Refresh all product availability statuses without using background tasks
* Not using Magento collections to save memory, because there is no sense constructing
* a Catalog_Model_Product object here too. Not using foreach because it clones objects.
**/
public function RefreshAllWithoutBackgroundTasks() {
$db = Mage::getSingleton('core/resource')->getConnection('core_read');
$select = $db->select()->from('catalog_product_entity');
$collection = $db->fetchAll($select);
$count = count($collection);
$j = 0;
echo "Refreshing";
for ($i=0, $n=count($collection); $i<$n; ++$i) {
$j++;
$this->RefreshForOneProduct($collection[$i]['entity_id']);
if ($j % 100 == 0)
echo ' completed ' . $j . '/' . $count . " products (" . number_format(memory_get_usage() / 1024 / 1024 / 1024, 2) . "G)\nRefreshing";
else if ($j % 5 == 0)
echo '.';
unset($collection[$i]);
}
}
}
?>
<?php
#/app/code/community/MDN/SalesOrderPlanning/Model/ProductAvailabilityStatus.php
class MDN_SalesOrderPlanning_Model_ProductAvailabilityStatus extends Mage_Core_Model_Abstract {
# only method modified was ->Refresh()
function Refresh() {
# ...all code unchanged, just need to append this last line to force Magento to do garbage collection:
$this->_product->clearInstance();
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment