Example #1
0
 /**
  * Generate cron schedule.
  * Rewrites the original method to remove duplicates afterwards (that exists because of a bug)
  *
  * @return Mage_Cron_Model_Observer
  */
 public function generate()
 {
     /**
      * check if schedule generation is needed
      */
     $lastRun = Mage::app()->loadCache(self::CACHE_KEY_LAST_SCHEDULE_GENERATE_AT);
     if ($lastRun > time() - Mage::getStoreConfig(self::XML_PATH_SCHEDULE_GENERATE_EVERY) * 60) {
         return $this;
     }
     $startTime = microtime(true);
     $result = parent::generate();
     $this->deleteDuplicates();
     if ($logFile = Mage::getStoreConfig('system/cron/logFile')) {
         $history = Mage::getModel('cron/schedule')->getCollection()->setPageSize(1)->setOrder('scheduled_at', 'desc')->load();
         $newestSchedule = $history->getFirstItem();
         /* @var $newestSchedule Aoe_Scheduler_Model_Schedule */
         $duration = microtime(true) - $startTime;
         Mage::log('Generated schedule. Newest task is scheduled at "' . $newestSchedule->getScheduledAt() . '". (Duration: ' . round($duration, 2) . ' sec)', null, $logFile);
     }
     return $result;
 }
 /**
  * Generate cron schedule.
  * Rewrites the original method to remove duplicates afterwards (that exists because of a bug)
  *
  * @return Mage_Cron_Model_Observer
  */
 public function generate()
 {
     $result = parent::generate();
     $cron_schedule = Mage::getSingleton('core/resource')->getTableName('cron_schedule');
     $conn = Mage::getSingleton('core/resource')->getConnection('core_read');
     $results = $conn->fetchAll("\r\n\t\t\tSELECT\r\n\t\t\t\tGROUP_CONCAT(schedule_id) AS ids,\r\n\t\t\t\tCONCAT(job_code, scheduled_at) AS jobkey,\r\n\t\t\t\tcount(*) AS qty\r\n\t\t\tFROM {$cron_schedule}\r\n\t\t\tWHERE status = 'pending'\r\n\t\t\tGROUP BY jobkey\r\n\t\t\tHAVING qty > 1;\r\n\t\t");
     foreach ($results as $row) {
         $ids = explode(',', $row['ids']);
         $removeIds = array_slice($ids, 1);
         foreach ($removeIds as $id) {
             Mage::getModel('cron/schedule')->load($id)->delete();
         }
     }
     return $result;
 }