Skip to content

Instantly share code, notes, and snippets.

@drewgillson
Last active March 29, 2017 22:36
Show Gist options
  • Save drewgillson/5394831 to your computer and use it in GitHub Desktop.
Save drewgillson/5394831 to your computer and use it in GitHub Desktop.
<?php
/*
==New BSD License==
Copyright (c) 2013, Drew Gillson
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* The name of Drew Gillson may not be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/**
* Gearman task dispatcher for Boost My Shop ERP (v2.9.1) extension for Magento (CE 1.7)
*
* @copyright Copyright (c) 2013 Drew Gillson (http://drewgillson.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
require_once 'abstract.php';
/**
* Magento Gearman Dispatcher Script
*
* @category Mage
* @package Mage_Shell
* @author Drew Gillson <[email protected]>
*/
class Mage_Shell_Gearman extends Mage_Shell_Abstract
{
private function init()
{
$this->indexers = Mage::getSingleton('index/indexer')->getProcessesCollection();
$this->db = Mage::getSingleton('core/resource')->getConnection('core_read');
$this->write = Mage::getSingleton('core/resource')->getConnection('core_write');
$this->gearmanQueue = Mage::getModel('gearman/queue');
$this->visibilityAttr = Mage::getModel('eav/entity_attribute')->loadByCode('catalog_product', 'visibility');
$this->chunkSize = 100; // number of products that get passed to the worker at a time
}
private function setManualIndexMode()
{
foreach ($this->indexers as $indexer) {
$indexer->setMode(Mage_Index_Model_Process::MODE_MANUAL)->save();
}
}
private function setRealtimeIndexMode()
{
foreach ($this->indexers as $indexer) {
$indexer->setMode(Mage_Index_Model_Process::MODE_REAL_TIME)->save();
}
}
public function run()
{
$this->init();
if (isset($this->_args['refresh-stock-status'])) {
echo "Refreshing simple product stock status\n";
/**
* If you get errors like SQLSTATE[HY000]: General error: 1030 Got error -1 from storage engine,
* try dropping a foreign key from index_process_event. After doing extensive research I found this:
* http://www.lexitconsulting.com/2012/10/magento-reindexing-transactions-and-locks/
*
* ALTER TABLE index_process_event DROP FOREIGN KEY FK_INDEX_PROCESS_EVENT_EVENT_ID_INDEX_EVENT_EVENT_ID;
**/
/**
* It's important that we sort configurables last, summary validity depends on simple products being updated first
**/
$sql = "SELECT entity_id FROM catalog_product_entity ORDER BY type_id DESC";
$rows = $this->db->fetchAll($sql);
$count = count($rows);
foreach ($rows as $row) {
$entity_ids[] = $row['entity_id'];
}
$this->setManualIndexMode();
$i = 0; // thread counter
$threads = array();
$chunks = array_chunk($entity_ids, $this->chunkSize, true);
foreach ($chunks as $chunk) {
/**
* This $event object is passed to the Gearman worker below
**/
$event = array();
$event['queue'] = 'refreshStockStatus';
$event['task'] = array(
'entity_ids' => $chunk,
'count' => ($count - $i)
);
/**
* If you don't need access to the state of the dispatched event you can
* call Mage::dispatchEvent('gearman_do_async_task', $event) instead of
* using the Gearman queue model
**/
$threads[] = $this->gearmanQueue->dispatchTask($event);
if ($i % 1000 == 0)
echo "Dispatched $i product refresh events to gearman\n";
$i = $i + $this->chunkSize;
}
$this->waitForThreads($threads);
/**
* The program won't proceed past this point until all threads are finished up
**/
$this->setRealtimeIndexMode();
}
else {
echo $this->usageHelp();
}
}
public function waitForThreads($threads)
{
$threadCount = count($threads);
do {
$done = 0;
foreach ($threads as $id) {
if ($this->gearmanQueue->checkTaskComplete($id))
$done++;
}
echo "Done: " . $done . " of " . $threadCount . "\n";
sleep(1);
}
while ($done < $threadCount);
}
public function usageHelp()
{
return <<<USAGE
Usage: php gearman.php -- [options]
refresh-stock-status Refresh stock status asynchronously
USAGE;
}
}
require_once dirname(__FILE__) . '/../app/Mage.php';
Mage::app();
$gearman = new Mage_Shell_Gearman();
$gearman->run();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment