/** * Send message to all pending recipients * * @param Mzax_Emarketing_Model_Campaign $campaign * @param Varien_Object $options * @throws Exception * @return number */ public function sendRecipients(Mzax_Emarketing_Model_Campaign $campaign, Varien_Object $options) { $recipients = $campaign->getPendingRecipients(); $recipients->setPageSize($options->getMaximum()); $start = time(); $prepared = 0; $timeout = (int) $options->getTimeout(); /* @var $recipient Mzax_Emarketing_Model_Recipient */ foreach ($recipients as $recipient) { try { $this->sendRecipient($recipient); $prepared++; } catch (Exception $e) { $recipient->logException($e); if ($options->getBreakOnError()) { throw $e; } Mage::logException($e); } $recipient->isPrepared(true); $recipient->save(); if ($timeout && time() - $start > $timeout) { break; } } return $prepared; }
public function download(Varien_Object $connectionInfo, $target) { $file = $connectionInfo->getFile(); $this->_log($this->_getLog()->__("Connecting to FTP server %s", $connectionInfo->getHost())); $ftp = new Varien_Io_Ftp(); $ftp->open(array('host' => $connectionInfo->getHost(), 'user' => $connectionInfo->getUsername(), 'password' => $connectionInfo->getPassword(), 'timeout' => $connectionInfo->hasTimeout() ? $connectionInfo->getTimeout() : 10, 'passive' => $connectionInfo->hasPassive() ? $connectionInfo->getPassive() : true, 'ssl' => $connectionInfo->hasSsl() ? $connectionInfo->getSsl() : null, 'file_mode' => $connectionInfo->hasFileMode() ? $connectionInfo->getFileMode() : null)); if (!is_writable(Mage::getBaseDir() . DS . $target)) { Mage::throwException($this->_getLog()->__("Can not write file %s to %s, folder not writable (doesn't exist?)", $connectionInfo->getFile(), $target)); } $this->_log($this->_getLog()->__("Downloading file %s from %s, to %s", $connectionInfo->getFile(), $connectionInfo->getHost(), $target)); $targetPath = $this->_getTargetPath($target, basename($file)); $ftp->read($file, $targetPath); $ftp->close(); }
/** * Send recipients * * @param array $options * @throws Exception * @return number */ public function sendRecipients(array $options) { $lock = Mage::helper('mzax_emarketing')->lock('send_recipients'); if (!$lock) { return false; } /* @var $campaigns Mzax_Emarketing_Model_Resource_Campaign_Collection */ $campaigns = Mage::getResourceModel('mzax_emarketing/campaign_collection'); $campaigns->addRunningFilter(); //$campaigns->setOrder('last_check', 'DESC'); $campaigns->setOrder('RAND()', 'ASC'); $options = new Varien_Object($options); $options->getDataSetDefault('timeout', 100); $options->getDataSetDefault('maximum', 500); $options->getDataSetDefault('break_on_error', Mage::getIsDeveloperMode()); if ($options->getVerbose()) { echo "\n\n{$campaigns->getSelect()}\n\n"; } $start = time(); $timeout = (int) $options->getTimeout(); $maximum = (int) $options->getMaximum(); $count = 0; /* @var $campaign Mzax_Emarketing_Model_Campaign */ foreach ($campaigns as $campaign) { try { if ($options->getVerbose()) { echo sprintf("Send Recipients for '%s' (#%s)...", $campaign->getName(), $campaign->getId()); } $count += $sent = $campaign->sendRecipients(array('timeout' => $timeout - (time() - $start), 'maximum' => $maximum - $count, 'break_on_error' => $options->getBreakOnError())); if ($options->getVerbose()) { echo sprintf(" \tsent %s\n", $sent); } } catch (Exception $e) { if ($options->getBreakOnError()) { $lock->unlock(); throw $e; } Mage::logException($e); if ($options->getVerbose()) { echo "\n{$e->getMessage()}\n{$e->getTraceAsString()}\n\n"; } } // make sure we don't exceed the timeout if ($timeout && time() - $start > $timeout) { break; } // don't send more then max at once if ($maximum && $maximum <= $count) { break; } $lock->touch(); } $lock->unlock(); return $count; }