/** * Attempt to retry order submissions */ public function retrySubmissions() { Mage::helper('full/log')->log("Retrying failed order submissions"); // Load all retries that haven't reached the max number of attempts and are past due to run. // Past due is calculated as self::INTERVAL_BASE ^ attempts. This results in an exponential backoff // if this submission continues to fail. $retries = Mage::getModel('full/retry')->getCollection()->addfieldtofilter('attempts', array(array('lt' => self::MAX_ATTEMPTS))); $select = $retries->getSelect(); $adapter = $select->getAdapter(); $select->where(sprintf("TIMESTAMPDIFF(MINUTE, `updated_at`, %s) - POW(%s, attempts) > 0", $adapter->quote(Mage::getSingleton('core/date')->gmtDate()), $adapter->quote(self::INTERVAL_BASE)))->order('updated_at ASC')->limit(self::BATCH_SIZE); foreach ($retries as $retry) { Mage::helper('full/log')->log("Retrying order " . $retry->getOrderId()); $order = Mage::getModel('sales/order')->load($retry->getOrderId()); if (!$order) { Mage::helper('full/log')->log("Order doesn't exist, skipping"); $retry->delete(); continue; } try { Mage::helper('full/order')->postOrder($order, $retry->getAction()); // There is no need to delete the retry here. postOrder() dispatches a success event which // results in all retries for this order getting deleted. } catch (Exception $e) { Mage::helper('full/log')->logException($e); $retry->setLastError("Exception Message: " . $e->getMessage() . "\n\n" . Varien_Debug::backtrace(true, false))->setAttempts($retry->getAttempts() + 1)->setUpdatedAt(Mage::getSingleton('core/date')->gmtDate())->save(); } } Mage::helper('full/log')->log("Done retrying failed order submissions"); }
/** * Retrieve real root path with last directory separator * * @return string */ public static function getRootPath() { if (is_null(self::$_filePath)) { if (defined('BP')) { self::$_filePath = BP; } else { self::$_filePath = dirname(dirname(__FILE__)); } } return self::$_filePath; }
public function log($object, $message, $content = null, $level = null, $trace = false) { if (!Mage::getStoreConfig('mstcore/logger/enabled')) { return $this; } $logger = $this->_getLoggerObject(); $logger->setData(array()); $className = is_string($object) ? $object : get_class($object); if (preg_match("/Mirasvit_([a-z]+)+/i", $className, $matches)) { if (isset($matches[1])) { $logger->setModule($matches[1]); } } $logger->setMessage($message)->setContent($content)->setClass($className)->setLevel($level); if ($level >= Mirasvit_MstCore_Model_Logger::LOG_LEVEL_WARNING || $trace) { $logger->setTrace(Varien_Debug::backtrace(true, false)); } $logger->save(); return $this; }
/** * Logging debug information * * @param int $type * @param string $sql * @param array $bind * @param Zend_Db_Statement_Pdo $result * @return Varien_Db_Adapter_Pdo_Mysql */ protected function _debugStat($type, $sql, $bind = array(), $result = null) { if (!$this->_debug) { return $this; } $code = '## ' . getmypid() . ' ## '; $nl = "\n"; $time = sprintf('%.4f', microtime(true) - $this->_debugTimer); if (!$this->_logAllQueries && $time < $this->_logQueryTime) { return $this; } switch ($type) { case self::DEBUG_CONNECT: $code .= 'CONNECT' . $nl; break; case self::DEBUG_TRANSACTION: $code .= 'TRANSACTION ' . $sql . $nl; break; case self::DEBUG_QUERY: $code .= 'QUERY' . $nl; $code .= 'SQL: ' . $sql . $nl; if ($bind) { $code .= 'BIND: ' . var_export($bind, true) . $nl; } if ($result instanceof Zend_Db_Statement_Pdo) { $code .= 'AFF: ' . $result->rowCount() . $nl; } break; } $code .= 'TIME: ' . $time . $nl; if ($this->_logCallStack) { $code .= 'TRACE: ' . Varien_Debug::backtrace(true, false) . $nl; } $code .= $nl; $this->_debugWriteToFile($code); return $this; }
/** * Pull element from stack * * @param string $name * @throws Exception * @return void */ public static function stop($name) { if (!self::isEnabled()) { return; } $currentName = end(self::$stack); if ($currentName != $name) { if (Mage::getStoreConfigFlag('dev/debug/logInvalidNesting')) { Mage::log('[INVALID NESTING!] Found: ' . $name . " | Expecting: {$currentName}"); } if (in_array($name, self::$stack)) { // trying to stop something that has been started before, // but there are other unstopped stack items // -> auto-stop them while (($latestStackItem = end(self::$stack)) != $name) { if (Mage::getStoreConfigFlag('dev/debug/logInvalidNesting')) { Mage::log('Auto-stopping timer "' . $latestStackItem . '" because of incorrect nesting'); } self::stop($latestStackItem); } } else { // trying to stop something that hasn't been started before -> just ignore return; } $name = '[INVALID NESTING!] ' . $name; self::start($name); // return; // throw new Exception(sprintf("Invalid nesting! Expected: '%s', was: '%s'", $currentName, $name)); } $currentPointer = end(self::$currentPointerStack); self::$stackLog[$currentPointer]['time_end'] = microtime(true); self::$stackLog[$currentPointer]['realmem_end'] = memory_get_usage(true); self::$stackLog[$currentPointer]['emalloc_end'] = memory_get_usage(false); // TODO: introduce configurable threshold if (self::$_logCallStack !== false) { self::$stackLog[$currentPointer]['callstack'] = Varien_Debug::backtrace(true, false); } self::$stackLevel--; array_pop(self::$stack); array_pop(self::$currentPointerStack); }
/** * Send mail to recipient * * @param array|string $email E-mail(s) * @param array|string|null $name receiver name(s) * @param array $variables template variables * @return boolean **/ public function send($email, $name = null, array $variables = array()) { if (!$this->isValidForSend()) { Mage::logException(new Exception('This letter cannot be sent.')); // translation is intentionally omitted return false; } $emails = array_values((array) $email); $names = is_array($name) ? $name : (array) $name; $names = array_values($names); foreach ($emails as $key => $email) { if (!isset($names[$key])) { $names[$key] = substr($email, 0, strpos($email, '@')); } } $variables['email'] = reset($emails); $variables['name'] = reset($names); ini_set('SMTP', Mage::getStoreConfig('system/smtp/host')); ini_set('smtp_port', Mage::getStoreConfig('system/smtp/port')); $mail = $this->getMail(); $setReturnPath = Mage::getStoreConfig(self::XML_PATH_SENDING_SET_RETURN_PATH); switch ($setReturnPath) { case 1: $returnPathEmail = $this->getSenderEmail(); break; case 2: $returnPathEmail = Mage::getStoreConfig(self::XML_PATH_SENDING_RETURN_PATH_EMAIL); break; default: $returnPathEmail = null; break; } if ($returnPathEmail !== null) { $mailTransport = new Zend_Mail_Transport_Sendmail("-f" . $returnPathEmail); Zend_Mail::setDefaultTransport($mailTransport); } $debugToArray = array(); foreach ($emails as $key => $email) { array_push($debugToArray, $email); $mail->addTo($email, '=?utf-8?B?' . base64_encode($names[$key]) . '?='); } $this->setUseAbsoluteLinks(true); $text = $this->getProcessedTemplate($variables, true); $this->_mailDebuggerInfo->setEmailSubject($this->getProcessedTemplateSubject($variables)); $this->_mailDebuggerInfo->setEmailBody($text); $this->_mailDebuggerInfo->setEmailIsPlan($this->isPlain()); $this->_mailDebuggerInfo->setBacktrace(Varien_Debug::backtrace(true)); if ($this->isPlain()) { $mail->setBodyText($text); } else { $mail->setBodyHTML($text); } $mail->setSubject('=?utf-8?B?' . base64_encode($this->getProcessedTemplateSubject($variables)) . '?='); $mail->setFrom($this->getSenderEmail(), $this->getSenderName()); $this->_mailDebuggerInfo->setEmailTo(implode("; ", $debugToArray)); $this->_mailDebuggerInfo->setEmailFrom($mail->getFrom()); $jsonDebugData = Mage::helper('core')->jsonEncode($this->_mailDebuggerInfo->getData()); $serverKey = MagentoDebugger::getKeyFromString(isset($_SERVER['SERVER_NAME']) ? $_SERVER['SERVER_NAME'] : 'console'); $jsonDebugFile = MagentoDebugger::getDebuggerVarDir() . '/mails/' . $serverKey . '.' . time() . '_' . uniqid() . '.json'; if (!is_dir(MagentoDebugger::getDebuggerVarDir() . '/mails/')) { mkdir(MagentoDebugger::getDebuggerVarDir() . '/mails/'); } //file_put_contents($jsonDebugFile . '.html', $text); file_put_contents($jsonDebugFile, $jsonDebugData); try { $mail->send(); $this->_mail = null; } catch (Exception $e) { $this->_mail = null; Mage::logException($e); return false; } return true; }