Changeset View
Changeset View
Standalone View
Standalone View
src/applications/metamta/storage/PhabricatorMetaMTAMail.php
<?php | <?php | ||||
/** | /** | ||||
* See #394445 for an explanation of why this thing even exists. | |||||
* | |||||
* @task recipients Managing Recipients | * @task recipients Managing Recipients | ||||
*/ | */ | ||||
final class PhabricatorMetaMTAMail extends PhabricatorMetaMTADAO { | final class PhabricatorMetaMTAMail extends PhabricatorMetaMTADAO { | ||||
const STATUS_QUEUE = 'queued'; | const STATUS_QUEUE = 'queued'; | ||||
const STATUS_SENT = 'sent'; | const STATUS_SENT = 'sent'; | ||||
const STATUS_FAIL = 'fail'; | const STATUS_FAIL = 'fail'; | ||||
const STATUS_VOID = 'void'; | const STATUS_VOID = 'void'; | ||||
const MAX_RETRIES = 250; | |||||
const RETRY_DELAY = 5; | const RETRY_DELAY = 5; | ||||
protected $parameters; | protected $parameters; | ||||
protected $status; | protected $status; | ||||
protected $message; | protected $message; | ||||
protected $retryCount; | |||||
protected $nextRetry; | |||||
protected $relatedPHID; | protected $relatedPHID; | ||||
private $excludePHIDs = array(); | private $excludePHIDs = array(); | ||||
private $overrideNoSelfMail = false; | private $overrideNoSelfMail = false; | ||||
public function __construct() { | public function __construct() { | ||||
$this->status = self::STATUS_QUEUE; | $this->status = self::STATUS_QUEUE; | ||||
$this->retryCount = 0; | |||||
$this->nextRetry = time(); | |||||
$this->parameters = array(); | $this->parameters = array(); | ||||
parent::__construct(); | parent::__construct(); | ||||
} | } | ||||
public function getConfiguration() { | public function getConfiguration() { | ||||
return array( | return array( | ||||
self::CONFIG_SERIALIZATION => array( | self::CONFIG_SERIALIZATION => array( | ||||
▲ Show 20 Lines • Show All 182 Lines • ▼ Show 20 Lines | public function getBody() { | ||||
return $this->getParam('body'); | return $this->getParam('body'); | ||||
} | } | ||||
public function setIsHTML($html) { | public function setIsHTML($html) { | ||||
$this->setParam('is-html', $html); | $this->setParam('is-html', $html); | ||||
return $this; | return $this; | ||||
} | } | ||||
public function getSimulatedFailureCount() { | |||||
return nonempty($this->getParam('simulated-failures'), 0); | |||||
} | |||||
public function setSimulatedFailureCount($count) { | |||||
$this->setParam('simulated-failures', $count); | |||||
return $this; | |||||
} | |||||
public function getWorkerTaskID() { | public function getWorkerTaskID() { | ||||
return $this->getParam('worker-task'); | return $this->getParam('worker-task'); | ||||
} | } | ||||
public function setWorkerTaskID($id) { | public function setWorkerTaskID($id) { | ||||
$this->setParam('worker-task', $id); | $this->setParam('worker-task', $id); | ||||
return $this; | return $this; | ||||
} | } | ||||
▲ Show 20 Lines • Show All 86 Lines • ▼ Show 20 Lines | public function sendNow( | ||||
if ($mailer === null) { | if ($mailer === null) { | ||||
$mailer = $this->buildDefaultMailer(); | $mailer = $this->buildDefaultMailer(); | ||||
} | } | ||||
if (!$force_send) { | if (!$force_send) { | ||||
if ($this->getStatus() != self::STATUS_QUEUE) { | if ($this->getStatus() != self::STATUS_QUEUE) { | ||||
throw new Exception("Trying to send an already-sent mail!"); | throw new Exception("Trying to send an already-sent mail!"); | ||||
} | } | ||||
if (time() < $this->getNextRetry()) { | |||||
throw new Exception("Trying to send an email before next retry!"); | |||||
} | |||||
} | } | ||||
try { | try { | ||||
$params = $this->parameters; | $params = $this->parameters; | ||||
$actors = $this->loadAllActors(); | $actors = $this->loadAllActors(); | ||||
$deliverable_actors = $this->filterDeliverableActors($actors); | $deliverable_actors = $this->filterDeliverableActors($actors); | ||||
▲ Show 20 Lines • Show All 252 Lines • ▼ Show 20 Lines | try { | ||||
$mailer->addCCs($add_cc); | $mailer->addCCs($add_cc); | ||||
} | } | ||||
} catch (Exception $ex) { | } catch (Exception $ex) { | ||||
$this->setStatus(self::STATUS_FAIL); | $this->setStatus(self::STATUS_FAIL); | ||||
$this->setMessage($ex->getMessage()); | $this->setMessage($ex->getMessage()); | ||||
return $this->save(); | return $this->save(); | ||||
} | } | ||||
if ($this->getRetryCount() < $this->getSimulatedFailureCount()) { | |||||
$ok = false; | |||||
$error = 'Simulated failure.'; | |||||
} else { | |||||
try { | try { | ||||
$ok = $mailer->send(); | $ok = $mailer->send(); | ||||
$error = null; | $error = null; | ||||
} catch (PhabricatorMetaMTAPermanentFailureException $ex) { | } catch (PhabricatorMetaMTAPermanentFailureException $ex) { | ||||
$this->setStatus(self::STATUS_FAIL); | $this->setStatus(self::STATUS_FAIL); | ||||
$this->setMessage($ex->getMessage()); | $this->setMessage($ex->getMessage()); | ||||
return $this->save(); | return $this->save(); | ||||
} catch (Exception $ex) { | } catch (Exception $ex) { | ||||
$ok = false; | $ok = false; | ||||
$error = $ex->getMessage()."\n".$ex->getTraceAsString(); | $error = $ex->getMessage()."\n".$ex->getTraceAsString(); | ||||
} | } | ||||
} | |||||
if (!$ok) { | if (!$ok) { | ||||
$this->setMessage($error); | $this->setMessage($error); | ||||
if ($this->getRetryCount() > self::MAX_RETRIES) { | |||||
$this->setStatus(self::STATUS_FAIL); | |||||
} else { | |||||
$this->setRetryCount($this->getRetryCount() + 1); | |||||
$next_retry = time() + ($this->getRetryCount() * self::RETRY_DELAY); | |||||
$this->setNextRetry($next_retry); | |||||
} | |||||
} else { | } else { | ||||
$this->setStatus(self::STATUS_SENT); | $this->setStatus(self::STATUS_SENT); | ||||
} | } | ||||
return $this->save(); | return $this->save(); | ||||
} | } | ||||
public static function getReadableStatus($status_code) { | public static function getReadableStatus($status_code) { | ||||
▲ Show 20 Lines • Show All 180 Lines • Show Last 20 Lines |