/**
  * Run the jobs that are in the queue
  *
  * @param mixed  $limit Limit of jobs to run
  * @return  int  Number of jobs run
  */
 public function run($limit = NULL)
 {
     // Cleanup crashed jobs
     $pids = $this->_db->fetchCol("SELECT pid FROM {$this->_table} WHERE pid IS NOT NULL GROUP BY pid");
     foreach ($pids as $pid) {
         // Old pid is no longer running, release it's reserved tasks
         if (is_numeric($pid) && !file_exists("/proc/{$pid}/status")) {
             Mage::log("A crashed job queue process was detected for pid {$pid}", Zend_Log::NOTICE, self::ERROR_LOG);
             $this->_db->update($this->_table, array('pid' => new Zend_Db_Expr('NULL')), array('pid = ?' => $pid));
         }
     }
     // Reserve all new jobs since last run
     $pid = getmypid();
     $limit = $limit ? "LIMIT {$limit}" : '';
     $batchSize = $this->_db->query("UPDATE {$this->_db->quoteIdentifier($this->_table, true)} SET pid = {$pid} WHERE pid IS NULL ORDER BY job_id {$limit}")->rowCount();
     // Run all reserved jobs
     $result = $this->_db->query($this->_db->select()->from($this->_table, '*')->where('pid = ?', $pid)->order(array('job_id')));
     while ($row = $result->fetch()) {
         $where = $this->_db->quoteInto('job_id = ?', $row['job_id']);
         $data = substr($row['data'], 0, 1) == '{' ? json_decode($row['data'], TRUE) : ($data = unserialize($row['data']));
         // Check retries
         if ($row['retries'] >= $row['max_retries']) {
             $this->_db->delete($this->_table, $where);
             Mage::log("{$row['pid']}: Mage::getSingleton({$row['class']})->{$row['method']}(" . json_encode($data) . ")\n" . $row['error_log'], Zend_Log::ERR, self::ERROR_LOG);
             continue;
         }
         // Run job!
         try {
             $model = Mage::getSingleton($row['class']);
             $method = $row['method'];
             $model->{$method}(new Varien_Object($data));
             $this->_db->delete($this->_table, $where);
             Mage::log("{$row['pid']}: Mage::getSingleton({$row['class']})->{$row['method']}(" . json_encode($data) . ")", Zend_Log::INFO, self::SUCCESS_LOG);
         } catch (Exception $e) {
             // Increment retries and log error information
             $error = date('c') . " ERROR: " . get_class($e) . ": '{$e->getMessage()}' in {$e->getFile()}:{$e->getLine()}\n" . "Stack trace:\n" . $e->getTraceAsString();
             $bind = array('pid' => new Zend_Db_Expr('NULL'), 'retries' => new Zend_Db_Expr('retries + 1'), 'error_log' => new Zend_Db_Expr('SUBSTR(CONCAT(error_log,' . $this->_db->quote($error) . ',"\\n\\n"),1,20000)'));
             $this->_db->update($this->_table, $bind, $where);
         }
     }
     return $batchSize;
 }
Exemplo n.º 2
0
 /**
  * @param Varien_Db_Adapter_Pdo_Mysql $connection
  * @param $tableName
  * @param array $fields
  * @param bool $onDuplicate
  * @return string
  */
 public function insert($connection, $tableName, $fields = array(), $onDuplicate = true)
 {
     $sql = "INSERT INTO `{$tableName}` ";
     $sql .= "(`" . implode('`,`', array_keys($fields)) . "`) ";
     $sql .= "VALUES (" . implode(',', $fields) . ") ";
     if ($onDuplicate && $fields) {
         $sql .= " ON DUPLICATE KEY UPDATE";
         $updateFields = array();
         foreach ($fields as $key => $field) {
             $key = $connection->quoteIdentifier($key);
             $updateFields[] = "{$key}=VALUES({$key})";
         }
         $sql .= " " . implode(', ', $updateFields);
     }
     return $sql;
 }
Exemplo n.º 3
0
 /**
  * Retrieve table header comment
  *
  * @param unknown_type $tableName
  * @return string
  */
 public function getTableHeader($tableName)
 {
     $quotedTableName = $this->_write->quoteIdentifier($tableName);
     return "\n--\n" . "-- Table structure for table {$quotedTableName}\n" . "--\n\n";
 }
Exemplo n.º 4
0
 /**
  * Retrieve after insert data SQL fragment
  *
  * @param string $tableName
  * @return string
  */
 public function getTableDataAfterSql($tableName)
 {
     $quotedTableName = $this->_read->quoteIdentifier($tableName);
     return "/*!40000 ALTER TABLE {$quotedTableName} ENABLE KEYS */;\n" . "UNLOCK TABLES;\n";
 }