Example #1
0
 /**
  * Tests Varien_Object->getDataSetDefault()
  */
 public function testGetDataSetDefault()
 {
     $this->_object->setData(array('key1' => 'value1', 'key2' => null));
     $this->assertEquals('value1', $this->_object->getDataSetDefault('key1', 'default'));
     $this->assertEquals(null, $this->_object->getDataSetDefault('key2', 'default'));
     $this->assertEquals('default', $this->_object->getDataSetDefault('key3', 'default'));
 }
Example #2
0
 /**
  * Send out emails
  * 
  */
 public function sendEmails(array $options = array())
 {
     $options = new Varien_Object($options);
     $lock = Mage::helper('mzax_emarketing')->lock('send_emails');
     if (!$lock) {
         if ($options->getVerbose()) {
             echo "\nACITVE LOCK- STOP\n\n\n";
         }
         return false;
     }
     $timeout = $options->getDataSetDefault('timeout', 60 * 5);
     $maximum = $options->getDataSetDefault('maximum', 200);
     $now = $options->getDataSetDefault('now', time());
     $force = $options->getDataSetDefault('force', false);
     $emailIds = $options->getDataSetDefault('ids', false);
     /* @var $emails Mzax_Emarketing_Model_Resource_Outbox_Email_Collection */
     $emails = Mage::getResourceModel('mzax_emarketing/outbox_email_collection');
     $emails->assignCampaigns();
     $emails->assignRecipients();
     $emails->addFieldToFilter('sent_at', array('null' => true));
     $emails->addFieldToFilter('status', Mzax_Emarketing_Model_Outbox_Email::STATUS_NOT_SEND);
     $emails->setOrder('expire_at', 'ASC');
     $emails->setPageSize($maximum);
     if (!$force) {
         $emails->addTimeFilter($now);
     }
     if (!empty($emailIds)) {
         $emails->addFieldToFilter('email_id', array('in' => $emailIds));
     }
     if ($options->getVerbose()) {
         echo "\n\n{$emails->getSelect()}\n\n";
     }
     $domainThrottle = $this->enableDomainThrottling();
     if ($domainThrottle) {
         /* @var $domainThrottle Mzax_Emarketing_Model_DomainThrottle */
         $domainThrottle = Mage::getModel('mzax_emarketing/domainThrottle');
         $domainThrottle->setTimeThreshold($this->getConfig('time_threshold'));
         $domainThrottle->setSendThreshold($this->getConfig('send_threshold'));
         $domainThrottle->setRestTime($this->getConfig('rest_time'));
         $domainSpecific = $this->getConfig('domain_specific');
         if ($domainSpecific) {
             foreach (unserialize($domainSpecific) as $data) {
                 $domainThrottle->addDomainOption($data['domain'], $data['time_threshold'], $data['send_threshold'], $data['rest_time']);
             }
         }
         $domainThrottle->purge();
     }
     if ($options->getVerbose()) {
         echo sprintf("found %s emails...\n", count($emails));
     }
     $start = time();
     $count = 0;
     /* @var $email Mzax_Emarketing_Model_Outbox_Email */
     foreach ($emails as $email) {
         // recipient may has been removed, if so - discard email
         if (!$email->getRecipient()->getId()) {
             $email->setStatus(Mzax_Emarketing_Model_Outbox_Email::STATUS_DISCARDED);
             $email->getLog()->warn("Recipient does not exist any more.");
             $email->save();
             continue;
         }
         if (time() - $start > $timeout) {
             $this->log("Mzax Emarketing: Reached timelimit of {$timeout}sec", $options->getVerbose());
             break;
         }
         // check if we can still send the message or if we missed the expire date
         if ($email->isExpired($now)) {
             $email->setStatus(Mzax_Emarketing_Model_Outbox_Email::STATUS_EXPIRED);
             $warn = "Message has expired, stop sending";
             $email->getLog()->warn($warn);
             $email->save();
             continue;
         }
         // check if we can send now if not ignore
         if (!$email->canSend($now)) {
             continue;
         }
         if ($domainThrottle && ($time = $domainThrottle->isResting($email->getDomain()))) {
             $notice = "DomainThrottle currently prevents this message from sending for at least {$time} more seconds";
             $this->log($notice, $options->getVerbose());
             $email->getLog()->notice($notice);
             $email->save();
             continue;
         }
         if ($options->getVerbose()) {
             echo sprintf("try sending email %s to %s.\n", $email->getId(), $email->getTo());
         }
         $email->send($options->getVerbose());
         $lock->touch();
         $count++;
     }
     $lock->unlock();
     return $count;
 }
Example #3
0
 /**
  * Prepare pending recipients and move them to
  * the email outbox
  * This will render all emails and makes them ready
  * to get send
  * 
  * @see Mzax_Emarketing_Model_Outbox
  * @return integer Number of prepared recipients
  */
 public function sendRecipients($options = array())
 {
     $options = new Varien_Object($options);
     $options->getDataSetDefault('timeout', 300);
     $options->getDataSetDefault('maximum', 500);
     $options->getDataSetDefault('break_on_error', Mage::getIsDeveloperMode());
     if ($this->getId() && $this->getRecipientProvider() && $this->getMedium()) {
         return $this->getMedium()->sendRecipients($this, $options);
     }
     return 0;
 }
Example #4
0
 /**
  * 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;
 }
Example #5
0
 /**
  * Retrieve option value
  * 
  * @param string $key
  * @param mixed $default
  * @return mixed
  */
 protected function getOption($key, $default = null)
 {
     return $this->_options->getDataSetDefault($key, $default);
 }