Last active
July 11, 2017 04:31
-
-
Save steverobbins/46beb1831cef5fc94b5ddf3b43fa0359 to your computer and use it in GitHub Desktop.
A Magento 1 script that will only reindex and refresh caches that have been invalidated.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Smart Magento reindexing and cache clearing. Only indexes and caches that are | |
# invalidate are cleared. Place this file in your shell/ directory and make it | |
# executable: chmod +x magentoSmartIndexCacheRefresh.sh | |
# | |
# Before use it is recommended to consider your failures as a developer due to | |
# lack of ability to correctly update individual indexes and caches as records | |
# modified processed. | |
# | |
# Usage: | |
# $ cd /var/www/html/magento | |
# $ ./shell/magentoSmartIndexCacheRefresh.sh | |
# | |
# Or cron: | |
# */5 * * * * cd /var/www/html/magento && ./shell/magentoSmartIndexCacheRefresh.sh >> /var/www/html/magento/var/log/smart-index-cache.log | |
# | |
# Or lazy daemon: | |
# $ screen | |
# $ watch -n 5 "cd /var/www/html/magento && ./shell/magentoSmartIndexCacheRefresh.sh" | |
# CTRL+d | |
# | |
BIN_PHP=`which php` | |
BIN_GREP=`which grep` | |
BIN_PS=`which ps` | |
DIR_SHELL=`echo $0 | sed 's/magentoSmartIndexCacheRefresh\.sh//g'` | |
magentoPhp() { | |
$BIN_PHP -r "require '$DIR_SHELL../app/Mage.php'; Mage::app(); $1" | |
} | |
reindex() { | |
magentoPhp "foreach(Mage::getSingleton('index/indexer')->getProcessesCollection() as \$p) { if (\$p->getStatus() == Mage_Index_Model_Process::STATUS_RUNNING) { continue; } \$p->indexEvents(); if (\$p->getStatus() != Mage_Index_Model_Process::STATUS_REQUIRE_REINDEX) { continue; } \$p->reindexEverything(); Mage::dispatchEvent(\$p->getIndexerCode() . '_shell_reindex_after'); echo 'Reindexed ', \$p->getIndexerCode(), PHP_EOL; }" | |
if [ $? -eq 0 ]; then | |
echo "Required indexes reindexed" | |
else | |
echo "There was a problem while reindexing" | |
fi | |
} | |
clearCache() { | |
magentoPhp "foreach(Mage::app()->getCacheInstance()->getInvalidatedTypes() as \$k => \$c) { Mage::app()->getCacheInstance()->cleanType(\$k); echo 'Cleared ', \$k, PHP_EOL; }" | |
if [ $? -eq 0 ]; then | |
echo "Required caches cleared" | |
else | |
echo "There was a problem while clearing caches" | |
fi | |
} | |
if ! $BIN_PS aux | $BIN_GREP "${DIR_SHELL}magentoSmartIndexCacheRefresh.sh" | $BIN_GREP -v grep | $BIN_GREP -v "$$" | $BIN_GREP -v watch | $BIN_GREP -v "sh -c" 1>/dev/null 2>/dev/null ; then | |
reindex | |
clearCache | |
else | |
echo "Script is already running. Not executing." | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment