Esempio n. 1
0
 public function start($count = 0)
 {
     if (!$count) {
         $this->limit = Mage::getStoreConfig('kirchbergerknorr/factfindersync/queue');
     } else {
         $this->limit = $count;
     }
     if ($this->indexProcess->isLocked()) {
         $this->log("Another %s process is running! Aborted", self::PROCESS_ID);
         return false;
     }
     // Set an exclusive lock.
     $this->indexProcess->lockAndBlock();
     $this->log("========================================");
     $timeStart = microtime(true);
     $this->log("Started FactFinderSync");
     $this->insertNewProducts();
     $this->updateImportedProducts();
     $timeEnd = microtime(true);
     $time = $timeEnd - $timeStart;
     $this->log("Finished FactFinderSync limit %s in %s seconds", $this->limit, $time);
     // Remove the lock.
     $this->indexProcess->unlock();
     return true;
 }
Esempio n. 2
0
 public function testLockAndBlock()
 {
     $this->_prepareMocksForTestLock(false);
     $result = $this->_indexProcess->lockAndBlock();
     $this->assertEquals($this->_indexProcess, $result);
 }
Esempio n. 3
0
 /**
  * Run the Task
  * 
  * IF ANY Job we run fails, due to another processes being run we should
  * gracefully exit and wait our next go!
  * 
  * Also change auto sync to just create a job, and run a single job Queue!
  */
 public function run()
 {
     if ($this->_config()->isLogEnabled()) {
         $this->_config()->dbLog("Cron [Triggered]");
     }
     /**
      * This doesn't exist in 1.3.2!
      */
     $indexProcess = new Mage_Index_Model_Process();
     $indexProcess->setId(self::LOCK_INDEX_ID);
     if ($indexProcess->isLocked()) {
         // Check how old the lock is - unlock after 1hr
         if ($this->_lockIsOld(self::LOCK_INDEX_ID)) {
             $indexProcess->unlock();
         } else {
             $this->_config()->log('MAILUP: cron already running or locked');
             return false;
         }
     }
     $indexProcess->lockAndBlock();
     try {
         require_once dirname(__FILE__) . '/../Helper/Data.php';
         $db_read = Mage::getSingleton('core/resource')->getConnection('core_read');
         $db_write = Mage::getSingleton('core/resource')->getConnection('core_write');
         $syncTableName = Mage::getSingleton('core/resource')->getTableName('mailup/sync');
         $jobsTableName = Mage::getSingleton('core/resource')->getTableName('mailup/job');
         $lastsync = gmdate("Y-m-d H:i:s");
         // reading customers (jobid == 0, their updates)
         $customer_entity_table_name = Mage::getSingleton('core/resource')->getTableName('customer_entity');
         /**
          * Now Handle Jobs we need to Sync, and all customers attached to each job
          */
         foreach (Mage::getModel('mailup/job')->fetchQueuedOrStartedJobsCollection() as $jobModel) {
             /* @var $jobModel MailUp_MailUpSync_Model_Job */
             $job = $jobModel->getData();
             $storeId = isset($job['store_id']) ? $job['store_id'] : NULL;
             // If job is auto-sync and cron is not enabled for the job's site, skip the job
             if ($jobModel->isAutoSync() && !$this->_config()->isCronExportEnabled($storeId)) {
                 $this->_config()->dbLog("Auto-Task skipped as auto-sync disabled for site", $job["id"], $storeId);
                 continue;
             }
             $stmt = $db_write->query("UPDATE {$jobsTableName}\n                    SET status='started', start_datetime='" . gmdate("Y-m-d H:i:s") . "'\n                    WHERE id={$job["id"]}");
             $customers = array();
             $job['mailupNewGroup'] = 0;
             $job['mailupIdList'] = Mage::getStoreConfig('mailup_newsletter/mailup/list', $storeId);
             $job["mailupGroupId"] = $job["mailupgroupid"];
             $job["send_optin_email_to_new_subscribers"] = $job["send_optin"];
             // If group is 0 and there is a default group, set group to this group
             $defaultGroupId = Mage::getStoreConfig('mailup_newsletter/mailup/default_group');
             if ($job["mailupGroupId"] == 0 && $defaultGroupId !== null) {
                 $job["mailupGroupId"] = $defaultGroupId;
             }
             $tmp = Mage::getSingleton('mailup/source_lists');
             $tmp = $tmp->toOptionArray($storeId);
             // pass store id!
             foreach ($tmp as $t) {
                 if ($t["value"] == $job['mailupIdList']) {
                     $job['mailupListGUID'] = $t["guid"];
                     $job["groups"] = $t["groups"];
                     break;
                 }
             }
             unset($tmp);
             unset($t);
             $stmt = $db_read->query("\n                    SELECT ms.*, ce.email\n                    FROM {$syncTableName} ms\n                    JOIN {$customer_entity_table_name} ce\n                        ON (ms.customer_id = ce.entity_id)\n                    WHERE ms.needs_sync=1\n                    AND ms.entity='customer'\n                    AND job_id={$job["id"]}");
             while ($row = $stmt->fetch()) {
                 $customers[] = $row["customer_id"];
             }
             /**
              * Send the Data!
              */
             $returnCode = MailUp_MailUpSync_Helper_Data::generateAndSendCustomers($customers, $job, $storeId);
             /**
              * Check return OK
              */
             if ($returnCode === 0) {
                 $customerCount = count($customers);
                 $db_write->query("\n                        UPDATE {$syncTableName} SET needs_sync=0, last_sync='{$lastsync}'\n                        WHERE job_id = {$job["id"]}\n                        AND entity='customer'");
                 $this->_config()->dbLog("Job Task [update] [Synced] [customer count:{$customerCount}]", $job["id"], $storeId);
                 // finishing the job also
                 $db_write->query("\n                        UPDATE {$jobsTableName} SET status='finished', finish_datetime='" . gmdate("Y-m-d H:i:s") . "'\n                        WHERE id={$job["id"]}");
                 $this->_config()->dbLog("Jobs [Update] [Complete] [{$job["id"]}]", $job["id"], $storeId);
             } else {
                 $stmt = $db_write->query("UPDATE {$jobsTableName} SET status='queued' WHERE id={$job["id"]}");
                 if ($this->_config()->isLogEnabled()) {
                     $this->_config()->dbLog(sprintf("generateAndSendCustomers [ReturnCode] [ERROR] [%d]", $returnCode), $job["id"], $storeId);
                 }
             }
         }
     } catch (Exception $e) {
         // In case of otherwise uncaught error, unlock and re-throw
         $indexProcess->unlock();
         throw $e;
     }
     $indexProcess->unlock();
     if ($this->_config()->isLogEnabled()) {
         $this->_config()->dbLog("Cron [Completed]");
     }
 }
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://opensource.org/licenses/osl-3.0.php
 *
 * @category   Mehulchaudhari
 * @package    Mehulchaudhari_FeedsGenerator
 * @author     Mehul Chaudhari
 * @copyright  Copyright (c) 2014 ; ;
 * @license    http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
 */
/** @var $installer Mage_Eav_Model_Entity_Setup */
const PROCESS_ID = 'google_taxonomy';
$indexProcess = new Mage_Index_Model_Process();
$indexProcess->setId(PROCESS_ID);
if (!$indexProcess->isLocked()) {
    $indexProcess->lockAndBlock();
    $installer = $this;
    $installer->startSetup();
    // Create (or re-create) the table containing all of the Google taxonomy information. This is a list
    // of available taxonomy values.
    $installer->run("\n        DROP TABLE IF EXISTS {$this->getTable('feedsgenerator_taxonomy')};\n        CREATE TABLE {$this->getTable('feedsgenerator_taxonomy')} (\n          `taxonomy_id` int(10) unsigned NOT NULL AUTO_INCREMENT,\n\t\t  `store_lang` varchar(255) NOT NULL,\n          `taxonomy_name` varchar(255) NOT NULL,\n          PRIMARY KEY (`taxonomy_id`)\n        ) ENGINE=InnoDB DEFAULT CHARSET=utf8;\n    ");
    // Fill the taxonomy table with values provided by Google. Values are inserted in batches of
    // 1000 to increase processing speed.
    $connection = $installer->getConnection();
    $Datail = Mage::getModel('feedsgenerator/googleproducts_taxonomy')->getDataDetail();
    foreach ($Datail as $lang => $taxonomy) {
        $data = array();
        foreach ($taxonomy as $i => $t) {
            $data[] = array($lang, $t);
        }
        $connection->insertArray($this->getTable('feedsgenerator_taxonomy'), array('store_lang', 'taxonomy_name'), $data);
 /**
  * Lock and block process.
  * If new instance of the process will try validate locking state
  * script will wait until process will be unlocked
  *
  * @return Mage_Index_Model_Process
  */
 public function lockAndBlock()
 {
     if (false === $this->getLockInstance()) {
         return parent::lockAndBlock();
     }
     $this->getLockInstance()->lockAndBlock();
     return $this;
 }