/** * Sends messages using the given transport instance. * * @param Swift_Transport $transport A transport instance * @param string[] &$failedRecipients An array of failures by-reference * * @return int The number of sent emails */ public function flushQueue(Swift_Transport $transport, &$failedRecipients = null) { if (!$transport->isStarted()) { $transport->start(); } $failedRecipients = (array) $failedRecipients; $count = 0; $time = time(); foreach (new DirectoryIterator($this->_path) as $file) { $file = $file->getRealPath(); if (!strpos($file, '.message')) { continue; } $message = unserialize(file_get_contents($file)); $count += $transport->send($message, $failedRecipients); unlink($file); if ($this->getMessageLimit() && $count >= $this->getMessageLimit()) { break; } if ($this->getTimeLimit() && time() - $time >= $this->getTimeLimit()) { break; } } return $count; }
/** * Sends messages using the given transport instance. * * @param \Swift_Transport $transport A transport instance * @param string[] &$failedRecipients An array of failures by-reference * * @return int The number of sent emails */ public function flushQueue(\Swift_Transport $transport, &$failedRecipients = null) { if (!$transport->isStarted()) { $transport->start(); } $repo = $this->em->getRepository($this->entityClass); $limit = $this->getMessageLimit(); $limit = $limit > 0 ? $limit : null; $emails = $repo->findBy(array("status" => self::STATUS_READY), null, $limit); if (!count($emails)) { return 0; } $failedRecipients = (array) $failedRecipients; $count = 0; $time = time(); /** @var SpoolItem $email */ foreach ($emails as $email) { $email->setStatus(self::STATUS_PROCESSING); $this->em->persist($email); $this->em->flush(); /** @var \Swift_Message $message */ $message = unserialize($email->getMessage()); $count += $transport->send($message, $failedRecipients); $this->em->remove($email); $this->em->flush(); if ($this->getTimeLimit() && time() - $time >= $this->getTimeLimit()) { break; } } return $count; }
/** * Sends messages using the given transport instance. * * @param Swift_Transport $transport A transport instance * @param string[] $failedRecipients An array of failures by-reference * * @return int The number of sent emails */ public function flushQueue(Swift_Transport $transport, &$failedRecipients = null) { if (!$this->messages) { return 0; } if (!$transport->isStarted()) { $transport->start(); } $count = 0; $retries = $this->flushRetries; while ($retries--) { try { while ($message = array_pop($this->messages)) { $count += $transport->send($message, $failedRecipients); } } catch (Swift_TransportException $exception) { if ($retries) { // re-queue the message at the end of the queue to give a chance // to the other messages to be sent, in case the failure was due to // this message and not just the transport failing array_unshift($this->messages, $message); // wait half a second before we try again usleep(500000); } else { throw $exception; } } } return $count; }
/** * Sends messages using the given transport instance. * * @param Swift_Transport $transport A transport instance * @param string[] &$failedRecipients An array of failures by-reference * * @return int The number of sent emails */ public function flushQueue(Swift_Transport $transport, &$failedRecipients = null) { if (!$transport->isStarted()) { $transport->start(); } $count = 0; while ($message = array_pop($this->messages)) { $count += $transport->send($message, $failedRecipients); } return $count; }
/** * {@inheritdoc} */ public function flushQueue(\Swift_Transport $transport, &$failedRecipients = null) { if (!$this->redis->llen($this->key)) { return 0; } if (!$transport->isStarted()) { $transport->start(); } $failedRecipients = (array) $failedRecipients; $count = 0; $time = time(); while ($message = unserialize($this->redis->lpop($this->key))) { $count += $transport->send($message, $failedRecipients); if ($this->getMessageLimit() && $count >= $this->getMessageLimit()) { break; } if ($this->getTimeLimit() && time() - $time >= $this->getTimeLimit()) { break; } } return $count; }
/** * Sends messages using the given transport instance. * * @param Swift_Transport $transport A transport instance * @param string[] &$failedRecipients An array of failures by-reference * * @return int The number of sent emails */ public function flushQueue(Swift_Transport $transport, &$failedRecipients = null) { $queryMethod = $this->method; $model = constant($this->model.'::PEER'); $modelQuery = PropelQuery::from($this->model); $objects = $modelQuery->$queryMethod()->limit($this->getMessageLimit())->find(); if (!$transport->isStarted()) { $transport->start(); } $method = 'get'.call_user_func(array($model, 'translateFieldName'), $this->column, BasePeer::TYPE_FIELDNAME, BasePeer::TYPE_PHPNAME); $count = 0; $time = time(); foreach ($objects as $object) { $message = unserialize($object->$method()); try { $count += $transport->send($message, $failedRecipients); $object->mailSent(); } catch (Exception $e) { $object->mailUnsent(); } if ($this->getTimeLimit() && (time() - $time) >= $this->getTimeLimit()) { break; } } return $count; }
/** * Sends messages using the given transport instance. * * @param Swift_Transport $transport A transport instance * @param string[] &$failedRecipients An array of failures by-reference * * @return int The number of sent emails */ public function flushQueue(Swift_Transport $transport, &$failedRecipients = null) { $criteria = new Criteria(); $criteria->setLimit($this->getMessageLimit()); $model = constant($this->model . '::PEER'); $objects = call_user_func(array($model, $this->method), $criteria); if (!$transport->isStarted()) { $transport->start(); } $method = 'get' . call_user_func(array($model, 'translateFieldName'), $this->column, BasePeer::TYPE_FIELDNAME, BasePeer::TYPE_PHPNAME); $count = 0; $time = time(); foreach ($objects as $object) { if (is_resource($object->getMessage())) { $message = unserialize(stream_get_contents($object->getMessage())); } else { $message = unserialize($object->getMessage()); } $object->delete(); try { $count += $transport->send($message, $failedRecipients); } catch (Exception $e) { // TODO: What to do with errors? } if ($this->getTimeLimit() && time() - $time >= $this->getTimeLimit()) { break; } } return $count; }
/** * Sends messages using the given transport instance. * * @param Swift_Transport $transport A transport instance * @param string[] $failedRecipients An array of failures by-reference * * @return int The number of sent e-mail's */ public function flushQueue(Swift_Transport $transport, &$failedRecipients = null) { $directoryIterator = new DirectoryIterator($this->_path); /* Start the transport only if there are queued files to send */ if (!$transport->isStarted()) { foreach ($directoryIterator as $file) { if (substr($file->getRealPath(), -8) == '.message') { $transport->start(); break; } } } $failedRecipients = (array) $failedRecipients; $count = 0; $time = time(); foreach ($directoryIterator as $file) { $file = $file->getRealPath(); if (substr($file, -8) != '.message') { continue; } /* We try a rename, it's an atomic operation, and avoid locking the file */ if (rename($file, $file . '.sending')) { $message = unserialize(file_get_contents($file . '.sending')); $count += $transport->send($message, $failedRecipients); unlink($file . '.sending'); } else { /* This message has just been catched by another process */ continue; } if ($this->getMessageLimit() && $count >= $this->getMessageLimit()) { break; } if ($this->getTimeLimit() && time() - $time >= $this->getTimeLimit()) { break; } } return $count; }
/** * Sends messages using the given transport instance. * * @param Swift_Transport $transport A transport instance * @param string[] $failedRecipients An array of failures by-reference * @return int The number of sent e-mails * @throws Swift_IoException */ public function flushQueue(Swift_Transport $transport, &$failedRecipients = null) { if (!$transport->isStarted()) { $transport->start(); } $limit = $this->getMessageLimit(); $results = $this->collection->find(array('sentOn' => null)); if ($limit > 0) { $results->limit($limit); } $failedRecipients = (array) $failedRecipients; $count = 0; $time = time(); $timeLimit = $this->getTimeLimit(); foreach ($results as $result) { if (!isset($result['message']) || !$result['message'] instanceof \MongoBinData) { continue; } $id = $result['_id']; $this->write(array('_id' => $id), array('$set' => array('sentOn' => new \MongoDate()))); $message = unserialize($result['message']->bin); $count += $transport->send($message, $failedRecipients); try { $this->collection->remove(array('_id' => $id)); } catch (\Exception $e) { throw new Swift_IoException("Could not update email sent on date", 0, $e); } if ($timeLimit && time() - $time >= $timeLimit) { break; } } return $count; }
/** * Sends messages using the given transport instance. * * @param Swift_Transport $transport A transport instance * @param string[] $failedRecipients An array of failures by-reference * * @return int The number of sent e-mails */ public function flushQueue(Swift_Transport $transport, &$failedRecipients = null) { if (!$transport->isStarted()) { $transport->start(); } $limit = $this->getMessageLimit(); $limit = $limit > 0 ? $limit : null; $failedRecipients = (array) $failedRecipients; $count = 0; $time = time(); $timeLimit = $this->getTimeLimit(); try { $results = $this->pdo->query('SELECT ' . $this->pkey . ' as pkey, ' . $this->messageField . ' as email FROM ' . $this->table . ' WHERE ' . $this->timeField . ' IS NULL'); $ustmt = $this->pdo->prepare('UPDATE ' . $this->table . ' SET ' . $this->timeField . ' = ? WHERE ' . $this->pkey . ' = ?'); $dstmt = $this->pdo->prepare('DELETE FROM ' . $this->table . ' WHERE ' . $this->pkey . ' = ?'); foreach ($results->fetchAll(PDO::FETCH_COLUMN) as $result) { $id = $result[0]; $ustmt->execute(array(time(), $result[0])); $message = unserialize($result[1]); $count += $transport->send($message, $failedRecipients); $dstmt->execute(array($id)); if ($limit && $count >= $limit) { break; } if ($timeLimit && time() - $time >= $timeLimit) { break; } } return $count; } catch (\Exception $e) { throw new Swift_IoException("Could not access database", 0, $e); } }
/** * Sends messages using the given transport instance. * * @param Swift_Transport $transport A transport instance * @param string[] &$failedRecipients An array of failures by-reference * * @return int The number of sent emails */ public function flushQueue(Swift_Transport $transport, &$failedRecipients = null) { $table = Doctrine_Core::getTable($this->model); $objects = $table->{$this->method}()->limit($this->getMessageLimit())->execute(); if (!$transport->isStarted()) { $transport->start(); } $count = 0; $time = time(); foreach ($objects as $object) { $message = unserialize($object->{$this->column}); $object->delete(); try { $count += $transport->send($message, $failedRecipients); } catch (Exception $e) { // TODO: What to do with errors? } if ($this->getTimeLimit() && time() - $time >= $this->getTimeLimit()) { break; } } return $count; }
/** * Sends messages using the given transport instance. * * @param Swift_Transport $transport A transport instance * @param string[] &$failedRecipients An array of failures by-reference * * @return int The number of sent emails */ public function flushQueue(Swift_Transport $transport, &$failedRecipients = null) { $table = Doctrine_Core::getTable($this->model); //$objects = $table->{$this->method}()->limit($this->getMessageLimit())->execute(); $objects = $table->lockAndFetch( $this->getMessageLimit(), $this->getTimeLimit() )->execute(); // print_r( $objects, false); $count = 0; if ( $objects->count() > 0 ) { if (!$transport->isStarted()) { $transport->start(); } $time = time(); // TODO // loop while ( $mail = MailQueue::getNext() ) // --> Lock / Checkout 1 Mail // 1. check STATUS // 2. set STATUS to "SENDING" foreach ($objects as $object) { //$message = unserialize($object->{$this->column}); $message = $object->getMessage(); // is unserialized // $message->setTo('topo@localhost'); // $message->setCc(''); // $message->setBcc(''); try { // // TODO // // add queue id to metadata // // $message->getHeaders()->addTextHeader('X-MC-Metadata', json_encode( array( 'spool_id' => $object->id ))); // $count += $transport->send($message, $failedRecipients); // TODO // set STATUS to sent $object->setSent(); // $object->setSent(); } catch (Exception $e) { // TODO // 1. UNLOCK Mail for another try // 2. mark as failed echo $e; break; } if ($this->getTimeLimit() && (time() - $time) >= $this->getTimeLimit()) { break; } } } return $count; }
/** * Sends messages using the given transport instance. * * @param Swift_Transport $transport A transport instance * @param string[] &$failedRecipients An array of failures by-reference * * @return int The number of sent emails */ public function flushQueue(Swift_Transport $transport, &$failedRecipients = null) { $model = constant($this->model . '::PEER'); $objects = $this->method ? call_user_func(array($model, $this->method)) : call_user_func(array($model, 'doSelect'), new Criteria()); if (!$transport->isStarted()) { $transport->start(); } $method = 'get' . call_user_func(array($model, 'translateFieldName'), $this->column, BasePeer::TYPE_FIELDNAME, BasePeer::TYPE_PHPNAME); $count = 0; foreach ($objects as $object) { $message = unserialize($object->{$method}()); $object->delete(); try { $count += $transport->send($message, $failedRecipients); } catch (Exception $e) { // TODO: What to do with errors? } } return $count; }
/** * Sends messages using the given transport instance. * * @param \Swift_Transport $transport A transport instance * @param string[] &$failedRecipients An array of failures by-reference * * @return int The number of sent emails */ public function flushQueue(\Swift_Transport $transport, &$failedRecipients = null) { $repoClass = $this->doc->getManager()->getRepository($this->entityClass); $limit = $this->getMessageLimit(); $limit = $limit > 0 ? $limit : null; $emails = $repoClass->findBy(array("status" => EmailInterface::STATUS_READY, "environment" => $this->environment), null, $limit); if (!count($emails)) { return 0; } $failedRecipients = (array) $failedRecipients; $count = 0; $time = time(); foreach ($emails as $email) { $email->setStatus(EmailInterface::STATUS_PROCESSING); $this->doc->getManager()->persist($email); $this->doc->getManager()->flush($email); $message = unserialize($email->getMessage()); if (!$transport->isStarted()) { $transport->start(); } $count += $transport->send($message, $failedRecipients); $transport->stop(); if ($this->keepSentMessages === true) { $email->setStatus(EmailInterface::STATUS_COMPLETE); $this->doc->getManager()->persist($email); } else { $this->doc->getManager()->remove($email); } $this->doc->getManager()->flush($email); if ($this->getTimeLimit() && time() - $time >= $this->getTimeLimit()) { break; } } return $count; }
/** * Sends messages using the given transport instance. * * @param Swift_Transport $transport A transport instance * @param string[] $failedRecipients An array of failures by-reference * @throws Swift_IoException * @return int The number of sent e-mails */ public function flushQueue(Swift_Transport $transport, &$failedRecipients = null) { if (!$transport->isStarted()) { $transport->start(); } $limit = $this->getMessageLimit(); $results = $this->find(array('sentOn' => null), $limit > 0 ? array('limit' => $limit) : array()); $failedRecipients = (array) $failedRecipients; $count = 0; $time = time(); $timeLimit = $this->getTimeLimit(); foreach ($results as $result) { if (!isset($result->message) || !$result->message instanceof \MongoDB\BSON\Binary) { continue; } $id = $result->_id; $this->setSentOn($id, $this->now()); $count += $transport->send(unserialize($result->message->getData()), $failedRecipients); $this->delete($id); if ($timeLimit && time() - $time >= $timeLimit) { break; } } return $count; }