Skip to content
This repository has been archived by the owner on Jan 16, 2023. It is now read-only.

Commit

Permalink
setup Rabbit MQ architecture with respective consumers
Browse files Browse the repository at this point in the history
  • Loading branch information
aquibbaig committed Jul 2, 2019
1 parent 083ddf2 commit 6fb1420
Show file tree
Hide file tree
Showing 7 changed files with 232 additions and 6 deletions.
13 changes: 13 additions & 0 deletions site/app/config/packages/old_sound_rabbit_mq.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ old_sound_rabbit_mq:
update_project_info:
connection: default
exchange_options: {name: 'update-project-info', type: fanout}
send_notification:
connection: default
exchange_options: {name: 'send-notification', type: fanout}
consumers:
update_project_info:
connection: default
Expand All @@ -30,3 +33,13 @@ old_sound_rabbit_mq:
exchange_options: {name: 'update-project-info', type: fanout}
queue_options: {name: 'update-github-metadata'}
callback: update_github_metadata_consumer
web_notification:
connection: default
exchange_options: {name: 'send-notification', type: fanout}
queue_options: {name: 'send-notification'}
callback: web_notification_consumer
email_notification:
connection: default
exchange_options: {name: 'send-notification', type: fanout}
queue_options: {name: 'send-notification'}
callback: email_notification_consumer
6 changes: 6 additions & 0 deletions site/app/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ services:
public: false
bind:
$updateProjectInfoProducer: '@old_sound_rabbit_mq.update_project_info_producer'
$notificationProducer: '@old_sound_rabbit_mq.send_notification_producer'

# makes classes in src/Librecores/ProjectRepoBundle available to be used as services
# this creates a service per class whose id is the fully-qualified class name
Expand All @@ -27,6 +28,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
Expand Down Expand Up @@ -94,3 +97,6 @@ services:
App\RepoCrawler\GitRepoCrawler:
tags:
- 'app.repo_crawler'

# Register Mgilet NotificationManager as a service to be used in WebNotificationConsumer
Mgilet\NotificationBundle\Manager\NotificationManager: '@mgilet.notification'
42 changes: 42 additions & 0 deletions site/src/Consumer/AbstractNotificationConsumer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace App\Consumer;

use FOS\UserBundle\Model\UserManagerInterface;
use App\Util\NotificationHydrator;

/**
* Class AbstractNotificationConsumer
*
* Base class for all the Notification Consumers that we have
*
* @author Aquib Baig <aquibbaig97@gmail.com>
*/
abstract class AbstractNotificationConsumer
{
/**
* @var UserManagerInterface
*/
protected $userManager;

/**
* AbstractNotificationConsumer constructor.
* @param UserManagerInterface $userManager
*/
public function __construct(UserManagerInterface $userManager)
{
$this->userManager = $userManager;
}

/**
* Decides if a notification should be handled by a particular Consumer
* or not by returning a boolean value
*
* @param NotificationHydrator $notification
*
* @return bool
*/
abstract protected function shouldHandle(NotificationHydrator $notification): bool;
}

?>
53 changes: 53 additions & 0 deletions site/src/Consumer/EmailNotificationConsumer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

namespace App\Consumer;

use FOS\UserBundle\Model\UserManagerInterface;
use OldSound\RabbitMqBundle\RabbitMq\ConsumerInterface;
use PhpAmqpLib\Message\AMQPMessage;
use App\Util\NotificationHydrator;

/**
* Class EmailNotificationConsumer
*
* This class is responsible for sending out Notifications
* to the user's email
*
* @author Aquib Baig <aquibbaig97@gmail.com>
*/
class EmailNotificationConsumer extends AbstractNotificationConsumer implements ConsumerInterface
{
/**
* EmailNotificationConsumer constructor.
* @param UserManagerInterface $userManager
*/
public function __construct(UserManagerInterface $userManager)
{
parent::__construct($userManager);
}

/**
* Find the user that created the notification
* and persist it in the database
*
* @param AMQPMessage $msg
*
* @throws \Exception
*/
public function execute(AMQPMessage $msg)
{
echo "\nEmail Notification Consumer\n";
}

/**
* @param NotificationHydrator $notification
*
* @return bool
*/
protected function shouldHandle(NotificationHydrator $notification): bool
{
return true;
}
}

?>
53 changes: 53 additions & 0 deletions site/src/Consumer/WebNotificationConsumer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

namespace App\Consumer;

use App\Util\NotificationHydrator;
use FOS\UserBundle\Model\UserManagerInterface;
use OldSound\RabbitMqBundle\RabbitMq\ConsumerInterface;
use PhpAmqpLib\Message\AMQPMessage;

/**
* Class WebNotificationConsumer
*
* This class stores Notifications to the database
* and sends them out to the UI
*
* @author Aquib Baig <aquibbaig97@gmail.com>
*/
class WebNotificationConsumer extends AbstractNotificationConsumer implements ConsumerInterface
{
/**
* WebNotificationConsumer constructor.
* @param UserManagerInterface $userManager
*/
public function __construct(UserManagerInterface $userManager)
{
parent::__construct($userManager);
}

/**
* Find the user that created the notification
* and persist it in the database
*
* @param AMQPMessage $msg
*
* @throws \Exception
*/
public function execute(AMQPMessage $msg)
{
echo "\nWeb Notification Consumer\n";
}

/**
* @param NotificationHydrator $notification
*
* @return bool
*/
protected function shouldHandle(NotificationHydrator $notification): bool
{
return true;
}
}

?>
25 changes: 19 additions & 6 deletions site/src/Controller/ProjectController.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
use App\Service\GitHub\GitHubApiService;
use App\Service\ProjectMetricsProvider;
use App\Service\QueueDispatcherService;
use App\Service\NotificationProducerService;
use App\Util\Dates;
use App\Util\Notification;
use DateInterval;
use DateTimeImmutable;
use Doctrine\ORM\NonUniqueResultException;
Expand All @@ -42,14 +44,16 @@ class ProjectController extends AbstractController
/**
* Render the "New Project" page
*
* @param Request $request
* @param Request $request
*
* @param GitHubApiService $githubApiService
* @param QueueDispatcherService $queueDispatcherService
* @param GitHubApiService $githubApiService
* @param QueueDispatcherService $queueDispatcherService
*
* @param OrganizationRepository $organizationRepository
* @param OrganizationRepository $organizationRepository
*
* @param UserManagerInterface $userManager
* @param UserManagerInterface $userManager
*
* @param NotificationProducerService $notificationProducerService
*
* @return Response
*
Expand All @@ -61,7 +65,8 @@ public function newAction(
GitHubApiService $githubApiService,
QueueDispatcherService $queueDispatcherService,
OrganizationRepository $organizationRepository,
UserManagerInterface $userManager
UserManagerInterface $userManager,
NotificationProducerService $notificationProducerService
) {
$p = new Project();
$p->setParentUser($this->getUser());
Expand Down Expand Up @@ -181,6 +186,14 @@ public function newAction(
$em->persist($p);
$em->flush();

// Send Notification when Project is created
$notif = new Notification();
$notif->setSubject('A new project has been added to Librecores');
$notif->setMessage('Your project '.$p->getDisplayName().' was created successfully!');
$notif->setType('new_project');
$notif->setUserIdentifier($this->getUser()->getId());
$notificationProducerService->publishNotification($notif);

// queue data collection from repository
$queueDispatcherService->updateProjectInfo($p);

Expand Down
46 changes: 46 additions & 0 deletions site/src/Service/NotificationProducerService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace App\Service;

use OldSound\RabbitMqBundle\RabbitMq\ProducerInterface;
use App\Util\NotificationHydrator;

/**
* Class NotificationProducer
*
* This class publishes a Notification to the RabbitMQ queue
* which is then taken up by the Consumers
*
* @author Aquib Baig <aquibbaig97@gmail.com>
*/
class NotificationProducerService
{
/**
* Holds the notificationProducer service
*
* @var ProducerInterface
*/
private $notificationProducer;

/**
* NotificationService constructor.
*
* @param ProducerInterface $notificationProducer
*/
public function __construct(ProducerInterface $notificationProducer)
{
$this->notificationProducer = $notificationProducer;
}

/**
* Publishes a Notification to the RabbitMQ Queue
*
* @param NotificationHydrator $notification
*/
public function publishNotification(NotificationHydrator $notification)
{
$this->notificationProducer->publish((serialize($notification)));
}
}

?>

0 comments on commit 6fb1420

Please sign in to comment.