From 746808c48483f2884bec21e1bebad4b8afa40f5c Mon Sep 17 00:00:00 2001 From: Aquib Baig Date: Tue, 25 Jun 2019 16:18:48 +0530 Subject: [PATCH] create Notification data class, setup Rabbit MQ architecture Currently creates two consumers for email and web notifications --- .../config/packages/old_sound_rabbit_mq.yml | 14 ++ site/app/config/services.yml | 2 + .../Consumer/AbstractNotificationConsumer.php | 87 +++++++++ .../Consumer/EmailNotificationConsumer.php | 39 ++++ site/src/Consumer/WebNotificationConsumer.php | 42 +++++ site/src/Util/Notification.php | 177 ++++++++++++++++++ 6 files changed, 361 insertions(+) create mode 100644 site/src/Consumer/AbstractNotificationConsumer.php create mode 100644 site/src/Consumer/EmailNotificationConsumer.php create mode 100644 site/src/Consumer/WebNotificationConsumer.php create mode 100644 site/src/Util/Notification.php diff --git a/site/app/config/packages/old_sound_rabbit_mq.yml b/site/app/config/packages/old_sound_rabbit_mq.yml index 662a8e31..74be06a9 100644 --- a/site/app/config/packages/old_sound_rabbit_mq.yml +++ b/site/app/config/packages/old_sound_rabbit_mq.yml @@ -19,6 +19,9 @@ old_sound_rabbit_mq: update_project_info: connection: default exchange_options: {name: 'update-project-info', type: fanout} + notification: + connection: default + exchange_options: {name: 'notification', type: fanout} consumers: update_project_info: connection: default @@ -30,3 +33,14 @@ old_sound_rabbit_mq: exchange_options: {name: 'update-project-info', type: fanout} queue_options: {name: 'update-github-metadata'} callback: update_github_metadata_consumer + multiple_consumers: + notification: + connection: default + exchange_options: {name: 'notification', type: fanout} + queues: + web_notification: + name: 'web-notification' + callback: web_notification_consumer + email_notification: + name: 'email-notification' + callback: email_notification_consumer diff --git a/site/app/config/services.yml b/site/app/config/services.yml index 76e9dd0a..e3f3cbf5 100644 --- a/site/app/config/services.yml +++ b/site/app/config/services.yml @@ -27,6 +27,8 @@ services: # Required legacy-style aliases update_project_info_consumer: '@App\Consumer\UpdateProjectInformation' update_github_metadata_consumer: '@App\Consumer\UpdateGitHubMetadataConsumer' + web_notification_consumer: '@App\Consumer\WebNotificationConsumer' + email_notification_consumer: '@App\Consumer\EmailNotificationConsumer' # Explicit configurations # TODO: Investigate and deprecate these in favor of auto-wiring diff --git a/site/src/Consumer/AbstractNotificationConsumer.php b/site/src/Consumer/AbstractNotificationConsumer.php new file mode 100644 index 00000000..5cc4dc40 --- /dev/null +++ b/site/src/Consumer/AbstractNotificationConsumer.php @@ -0,0 +1,87 @@ + + */ +abstract class AbstractNotificationConsumer implements ConsumerInterface +{ + /** + * @var LoggerInterface + */ + protected $logger; + + /** + * @var Notification $notification + */ + protected $notification; + + /** + * AbstractNotificationConsumer constructor + * + * @param LoggerInterface $logger + */ + public function __construct(LoggerInterface $logger) + { + $this->logger = $logger; + } + + /** + * @param AMQPMessage $msg + * + * @return bool + * + * @throws Exception + */ + public function execute(AMQPMessage $msg) + { + $this->notification = unserialize($msg->body); + try { + if ($this->shouldHandle()) { + return $this->handle(); + } + + return true; //don't requeue + } catch (Exception $e) { + // Log out exceptions if they occur, and keep Consumers running for + // next requests + $this->logger->error( + "Processing the Notification resulted in an ".get_class($e) + ); + $this->logger->error("Message: ".$e->getMessage()); + $this->logger->error("Trace: ".$e->getTraceAsString()); + + return false; + } + } + + /** + * Should a Notification be handled by a the Consumer? + * + * @return bool + */ + protected function shouldHandle() + { + return true; + } + + /** + * Actually processes a Notification to its sink + * + * @return mixed + */ + abstract protected function handle():bool; +} + +?> diff --git a/site/src/Consumer/EmailNotificationConsumer.php b/site/src/Consumer/EmailNotificationConsumer.php new file mode 100644 index 00000000..36da2249 --- /dev/null +++ b/site/src/Consumer/EmailNotificationConsumer.php @@ -0,0 +1,39 @@ + + */ +class EmailNotificationConsumer extends AbstractNotificationConsumer +{ + /** + * EmailNotificationConsumer constructor + * + * @param LoggerInterface $logger + */ + public function __construct(LoggerInterface $logger) + { + parent::__construct($logger); + } + + /** + * Handles an email Notification + * + * @return bool + */ + protected function handle(): bool + { + // TODO: Process the Notification($this->notification) + echo "Email Notification Consumer"; + + return true; + } +} + +?> diff --git a/site/src/Consumer/WebNotificationConsumer.php b/site/src/Consumer/WebNotificationConsumer.php new file mode 100644 index 00000000..778d6592 --- /dev/null +++ b/site/src/Consumer/WebNotificationConsumer.php @@ -0,0 +1,42 @@ + + */ +class WebNotificationConsumer extends AbstractNotificationConsumer +{ + /** + * WebNotificationConsumer constructor + * + * @param LoggerInterface $logger + */ + public function __construct(LoggerInterface $logger) + { + parent::__construct($logger); + } + + /** + * Handles Notifications that will be sent to UI + * + * @return bool + */ + protected function handle(): bool + { + // TODO: Process the Notification($this->notification) + echo "Web Notification Consumer"; + + return true; + } +} + +?> diff --git a/site/src/Util/Notification.php b/site/src/Util/Notification.php new file mode 100644 index 00000000..bd8a123d --- /dev/null +++ b/site/src/Util/Notification.php @@ -0,0 +1,177 @@ + + */ +class Notification +{ + /** + * Subject of the Notification + * + * @var string $subject + */ + protected $subject; + + /** + * Notification message + * + * @var string $message + */ + protected $message; + + /** + * Recipient of the Notification + * + * @var User $recipient + */ + protected $recipient; + + /** + * The type of Notification + * + * @var string $type + */ + protected $type; + + /** + * When is the Notification created + * + * @var DateTime $createdAt + */ + protected $createdAt; + + /** + * Notification constructor. + */ + public function __construct() + { + $this->setCreatedAt(new DateTime()); + } + + /** + * Gets the Notification Subject + * + * @return string + */ + public function getSubject(): string + { + return $this->subject; + } + + /** + * Sets the Notification Subject + * + * @param string $subject + * + * @return $this + */ + public function setSubject(string $subject) + { + $this->subject = $subject; + + return $this; + } + + /** + * Get the Notification Message + * + * @return string + */ + public function getMessage(): string + { + return $this->message; + } + + /** + * Set the Notification Message + * + * @param string $message + * + * @return $this + */ + public function setMessage(string $message) + { + $this->message = $message; + + return $this; + } + + /** + * Returns the type of the Notification + * + * @return string + */ + public function getType(): string + { + return $this->type; + } + + /** + * Set the Notification type + * + * @param string $type + * + * @return $this + */ + public function setType(string $type) + { + $this->type = $type; + + return $this; + } + + /** + * Get the recipient of this notification + * + * @return User + */ + public function getRecipient(): User + { + return $this->recipient; + } + + /** + * Set the recipient of this message + * + * @param User $recipient + * + * @return $this + */ + public function setRecipient(User $recipient) + { + $this->recipient = $recipient; + + return $this; + } + + /** + * Get the creation date of this notification + * + * @return DateTime + */ + public function getCreatedAt(): DateTime + { + return $this->createdAt; + } + + /** + * Set the creation date of this notification + * + * @param DateTime $createdAt + * + * @return $this + */ + public function setCreatedAt(DateTime $createdAt) + { + $this->createdAt = $createdAt; + + return $this; + } +}