-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #78 from sailthru/email_queuing
Email queuing
- Loading branch information
Showing
15 changed files
with
455 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
<?php | ||
|
||
namespace Sailthru\MageSail\Mail\Queue; | ||
|
||
use Psr\Log\LoggerInterface; | ||
use Sailthru\MageSail\Helper\Settings; | ||
use Sailthru\MageSail\Mail\Transport\Sailthru as SailthruTransport; | ||
use Sailthru\MageSail\Mail\Transport\SailthruFactory as SailthruTransportFactory; | ||
|
||
class EmailSendProcessor | ||
{ | ||
/** | ||
* @var EmailSendPublisher | ||
*/ | ||
protected $publisher; | ||
|
||
/** | ||
* @var LoggerInterface | ||
*/ | ||
protected $logger; | ||
|
||
/** | ||
* @var Settings | ||
*/ | ||
protected $settingsHelper; | ||
|
||
/** | ||
* @var SailthruTransportFactory | ||
*/ | ||
protected $sailthruTransportFactory; | ||
|
||
public function __construct( | ||
EmailSendPublisher $publisher, | ||
LoggerInterface $logger, | ||
Settings $settingsHelper, | ||
SailthruTransportFactory $sailthruTransportFactory | ||
) { | ||
$this->publisher = $publisher; | ||
$this->logger = $logger; | ||
$this->settingsHelper = $settingsHelper; | ||
$this->sailthruTransportFactory = $sailthruTransportFactory; | ||
} | ||
|
||
/** | ||
* Init and return transport | ||
* | ||
* @param array $data | ||
* | ||
* @return SailthruTransport | ||
*/ | ||
public function getTransport(array $data) | ||
{ | ||
return $this->sailthruTransportFactory->create(['data' => $data]); | ||
} | ||
|
||
/** | ||
* Consumer handler of send Sailthru email | ||
* | ||
* @param string $messageData | ||
* | ||
* @return $this | ||
* | ||
* @throws \Exception | ||
*/ | ||
public function execute($messageData) | ||
{ | ||
$decodedData = json_decode($messageData, true); | ||
try { | ||
$this->getTransport($decodedData)->sendMessage(); | ||
} catch (\Throwable $t) { | ||
$this->logger->critical($t->getMessage()); | ||
$this->publisher->execute($decodedData, ($decodedData['attempt'] ?? 0) + 1); | ||
} | ||
|
||
return $this; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
<?php | ||
|
||
namespace Sailthru\MageSail\Mail\Queue; | ||
|
||
use Magento\Framework\Amqp\Config as AmqpConfig; | ||
use Magento\Framework\MessageQueue\PublisherInterface; | ||
use Sailthru\MageSail\Helper\Settings as SettingsHelper; | ||
|
||
class EmailSendPublisher | ||
{ | ||
/** | ||
* Topic names | ||
*/ | ||
const TOPIC_NAME_DB = 'sailthru.email.send.db'; | ||
const TOPIC_NAME_AMQP = 'sailthru.email.send.amqp'; | ||
|
||
/** | ||
* @var AmqpConfig | ||
*/ | ||
protected $amqpConfig; | ||
|
||
/** | ||
* @var PublisherInterface | ||
*/ | ||
protected $publisher; | ||
|
||
/** | ||
* EmailSendPublisher constructor. | ||
* | ||
* @param AmqpConfig $amqpConfig | ||
* @param PublisherInterface $publisher | ||
*/ | ||
public function __construct( | ||
AmqpConfig $amqpConfig, | ||
PublisherInterface $publisher | ||
) { | ||
$this->amqpConfig = $amqpConfig; | ||
$this->publisher = $publisher; | ||
} | ||
|
||
/** | ||
* Get topic name | ||
* | ||
* @return string | ||
*/ | ||
public function getTopicName() | ||
{ | ||
try { | ||
return $this->isAmqpConfigured() ? self::TOPIC_NAME_AMQP : self::TOPIC_NAME_DB; | ||
} catch (\LogicException $exception) { | ||
return self::TOPIC_NAME_DB; | ||
} | ||
} | ||
|
||
/** | ||
* Check Amqp is configured. | ||
* | ||
* @return bool | ||
* | ||
* @throws \LogicException | ||
*/ | ||
protected function isAmqpConfigured() | ||
{ | ||
return $this->amqpConfig->getValue(AmqpConfig::HOST) ? true : false; | ||
} | ||
|
||
/** | ||
* Add message to queue | ||
* | ||
* @param array $messageData | ||
* @param int $attempt | ||
* | ||
* @return $this | ||
*/ | ||
public function execute(array $messageData, int $attempt = 0) | ||
{ | ||
if ($attempt >= SettingsHelper::QUEUE_ATTEMPTS_COUNT) { | ||
return $this; | ||
} | ||
$messageData['attempt'] = $attempt; | ||
$this->publisher->publish($this->getTopicName(), json_encode($messageData)); | ||
|
||
return $this; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.