RabbitMQ Work Queue with Symfony

Add to .env


Add to services.yaml

        arguments: ['%env(RABBITMQ_HOST)%', '%env(RABBITMQ_PORT)%', '%env(RABBITMQ_USERNAME)%', '%env(RABBITMQ_PASSWORD)%', '%env(RABBITMQ_QUEUE_NAME)%']

            arguments: ['%env(RABBITMQ_HOST)%', '%env(RABBITMQ_PORT)%', '%env(RABBITMQ_USERNAME)%', '%env(RABBITMQ_PASSWORD)%', '%env(RABBITMQ_QUEUE_NAME)%']

Continue reading “RabbitMQ Work Queue with Symfony” »

Setting up RabbitMQ

First, we need to use composer to install RabbitMQ Server and the PHP package for interfacing with RabbitMQ.

composer require php-amqplib/php-amqplib

sudo apt-key adv --keyserver "hkps.pool.sks-keyservers.net" --recv-keys "0x6B73A36E6026DFCA"
wget -O - "https://github.com/rabbitmq/signing-keys/releases/download/2.0/rabbitmq-release-signing-key.asc" | sudo apt-key add -
sudo apt-get install apt-transport-https
sudo apt-get update -y
sudo apt-get install -y erlang
sudo apt-get update
sudo apt-cache policy
sudo apt-get install -y rabbitmq-server

Create user for rabbitMQ and set permissions
only root can create user

sudo su root 
rabbitmqctl add_user test test
rabbitmqctl set_user_tags test administrator
rabbitmqctl set_permissions -p / test ".*" ".*" ".*"

You can now reach RabbitMQ interface at http://localhost:15672

Install MongoDB in Ubuntu

Create a bash file named install_mongo.bash:

apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 9DA31620334BD75D9DCB49F368818C72E52529D4
echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/4.0 multiverse$
apt-get update
apt-get install -y mongodb-org
sudo systemctl enable mongod
sudo systemctl start mongod

Execute the bash file

sudo bash ./install_mongo.bash

You are ready to use Mongo

Problem with NULL values you should remember

When trying to execute SELECT OR UPDATE query for rows different than a specific value, we should be aware to insert additional statement for null values too.

For Example:

        UPDATE account_event 
        SET account_event.assigned_to_id = 2 
        WHERE account_event.status != 1

With this query your goal is to update all rows where account_event.status is different from 1. But if status have null value it wont be updated!
In MySQL all null elements are skipped from comparison and will never be Selected or Updated.

When you have a null fields you should always add additional statement for null like this:

        UPDATE account_event 
        SET account_event.assigned_to_id = 2 
        WHERE account_event.status != 1 
        OR account_event.status IS NULL

Symfony – Create a service – the right way

Always create service interface for your service. This way you will have better structured code and phpunit testing will be real pleasure.
Also use dependency injection for your repositories in the service.

Service Interface Example

interface UserServiceInterface{
  public function getUsername();
  public function viewAll();

Service Example

class UserService implements UserServiceInterface

  private $entityManager;
  private $userRepository;

  public function __construct(EntityManager $entityManager, UserRepository $userRepository)
    $this->entityManager = $entityManager;
    $this->userRepository = $userRepository;

  public function viewAll()
    return $this->userRepository->findAll();

Repository Example

class UserRepository extends \Doctrine\ORM\EntityRepository
  public function __construct(EntityManager $em, Mapping\ClassMetadata $metadata = null)
      $metadata == null ?
      new Mapping\ClassMetadata(User::class) :