Skip to content

ELEVATE-Project/notification

Repository files navigation

Notifications Service

CircleCI Duplicated Lines (%) Coverage Vulnerabilities Prettier Docs Docs GitHub package.json version (subfolder of monorepo) License

CircleCI insights

CircleCI


The Notification building block enables sending emails via SendGrid service. It can receive informations - To, From, Subject, and Body via API as well as Kafka topic and sends the email using Sendgrid service, the credentials of which can be configured in the .env file. This service also maintains a log of all the emails sent in the Postgres.

System Requirements

  • Operating System: Ubuntu 22
  • Node.js: v20
  • PostgreSQL: 16
  • Citus: 12.1
  • Apache Kafka: 3.5.0

Setup Options

Elevate notification services can be setup in local using two methods:

Dockerized service with local dependencies(Intermediate)

A. Dockerized Service With Local Dependencies

Expectation: Run single docker containerized service with existing local (in host) or remote dependencies.

Local Dependencies Steps

  1. Update dependency (Kafka etc) IP addresses in .env with "host.docker.internal".

    Eg:

     #Kafka Host Server URL
     KAFKA_URL = host.docker.external:9092
    
  2. Build the docker image.

    /ELEVATE/notification$ docker build -t elevate/notification:1.0 .
    
  3. Run the docker container.

    • For Mac & Windows with docker v18.03+:

      $ docker run --name notification elevate/notification:1.0
      
    • For Linux:

      $ docker run --name notification --add-host=host.docker.internal:host-gateway elevate/notification:1.0`
      

      Refer this for more information.

Remote Dependencies Steps

  1. Update dependency (Kafka etc) Ip addresses in .env with respective remote server IPs.

    Eg:

     #Kafka Host Server URL
     KAFKA_URL = 11.2.3.45:9092
    
  2. Build the docker image.

    /ELEVATE/notification$ docker build -t elevate/notification:1.0 .
    
  3. Run the docker container.

    $ docker run --name notification elevate/notification:1.0
    
Local Service with local dependencies(Hardest)

B. Local Service With Local Dependencies

Expectation: Run single service with existing local dependencies in host (Non-Docker Implementation).

Installations

Install Node.js LTS

Refer to the NodeSource distributions installation scripts for Node.js installation.

$ curl -fsSL https://deb.nodesource.com/setup_lts.x | sudo -E bash - &&\
sudo apt-get install -y nodejs

Install Build Essential

$ sudo apt-get install build-essential

Install Kafka

Refer to Kafka Ubuntu 22.04 setup guide

  1. Install OpenJDK 11:

    $ sudo apt install openjdk-11-jdk
  2. Download and extract Kafka:

    $ sudo wget https://downloads.apache.org/kafka/3.5.0/kafka_2.12-3.5.0.tgz
    $ sudo tar xzf kafka_2.12-3.5.0.tgz
    $ sudo mv kafka_2.12-3.5.0 /opt/kafka
  3. Configure Zookeeper:

    $ sudo nano /etc/systemd/system/zookeeper.service

    Paste the following lines into the zookeeper.service file:

    /etc/systemd/system/zookeeper.service
    [Unit]
    Description=Apache Zookeeper service
    Documentation=http://zookeeper.apache.org
    Requires=network.target remote-fs.target
    After=network.target remote-fs.target
    
    [Service]
    Type=simple
    ExecStart=/opt/kafka/bin/zookeeper-server-start.sh /opt/kafka/config/zookeeper.properties
    ExecStop=/opt/kafka/bin/zookeeper-server-stop.sh
    Restart=on-abnormal
    
    [Install]
    WantedBy=multi-user.target

    Save and exit.

  4. Reload systemd:

    $ sudo systemctl daemon-reload
  5. Configure Kafka:

    $ sudo nano /etc/systemd/system/kafka.service

    Paste the following lines into the kafka.service file:

    [Unit]
    Description=Apache Kafka Service
    Documentation=http://kafka.apache.org/documentation.html
    Requires=zookeeper.service
    
    [Service]
    Type=simple
    Environment="JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64"
    ExecStart=/opt/kafka/bin/kafka-server-start.sh /opt/kafka/config/server.properties
    ExecStop=/opt/kafka/bin/kafka-server-stop.sh
    
    [Install]
    WantedBy=multi-user.target

    Save and exit.

  6. Reload systemd:

    $ sudo systemctl daemon-reload
  7. Start Zookeeper:

    $ sudo systemctl start zookeeper

    Check status:

    $ sudo systemctl status zookeeper

    Zookeeper service status should be shown as active (running).

  8. Start Kafka:

    $ sudo systemctl start kafka

    Check status:

    $ sudo systemctl status kafka

    Kafka status should be shown as active (running).

Install PM2

Refer to How To Set Up a Node.js Application for Production on Ubuntu 22.04.

Run the following command

$ sudo npm install pm2@latest -g

Setting up Repository

Clone the notification repository to /opt/backend directory

opt/backend$ git clone -b develop-2.5 --single-branch "https://github.com/ELEVATE-Project/notification.git"

Install Npm packages from src directory

backend/notification/src$ sudo npm i

Create .env file in src directory

notification/src$ sudo nano .env

Copy-paste the following env variables to the .env file:

# Notification Service Config

# Port on which service runs
APPLICATION_PORT=3002

# Application environment
APPLICATION_ENV=development

# Route after the base URL
APPLICATION_BASE_URL=/notification/

# Kafka endpoint
KAFKA_HOST="localhost:9092"

# Kafka topic name
KAFKA_TOPIC="dev.notification"

# Kafka consumer group id
KAFKA_GROUP_ID="elevate-notification"

# Sendgrid API key
SENDGRID_API_KEY="SG.asd9f87a9s8d7f."

# Sendgrid sender email address
SENDGRID_FROM_MAIL="no-reply@some.org"

# Api doc URL
API_DOC_URL= "/notification/api-doc"

INTERNAL_ACCESS_TOKEN="internal_access_token"
ERROR_LOG_LEVEL='silly'
DISABLE_LOG=false
DEV_DATABASE_URL=postgres://shikshalokam:slpassword@localhost:9700/elevate_notification

ZEST_ENV= "ZEST_ENV"
created_time= "2023-12-29T17:04:19.017783534Z"
custom_metadata= null
destroyed=false
version=8

Save and exit.

Setting up Databases

Log into the postgres user

sudo su postgres

Log into psql

psql -p 9700

Create a database user/role:

CREATE USER shikshalokam WITH ENCRYPTED PASSWORD 'slpassword';

Create the elevate_notification database

CREATE DATABASE elevate_notification;
GRANT ALL PRIVILEGES ON DATABASE elevate_notification TO shikshalokam;
\c elevate_notification
GRANT ALL ON SCHEMA public TO shikshalokam;

Running Migrations To Create Tables

Exit the postgres user account and install sequelize-cli globally

$ sudo npm i sequelize-cli -g

Navigate to the src folder of notification service and run sequelize-cli migration command:

notification/src$ npx sequelize-cli db:migrate

Now all the tables must be available in the Citus databases

Start the Service

Navigate to the src folder of notification service and run pm2 start command:

notification/src$ pm2 start app.js -i 2 --name elevate-notification

Run pm2 ls command

$ pm2 ls

Output should look like this (Sample output, might slightly differ in your installation):

┌────┬─────────────────────────┬─────────────┬─────────┬─────────┬──────────┬────────┬──────┬───────────┬──────────┬──────────┬──────────┬──────────┐
│ id │ name                    │ namespace   │ version │ mode    │ pid      │ uptime │ ↺    │ status    │ cpu      │ mem      │ user     │ watching │
├────┼─────────────────────────┼─────────────┼─────────┼─────────┼──────────┼────────┼──────┼───────────┼──────────┼──────────┼──────────┼──────────┤
│ 19 │ elevate-notification    │ default     │ 1.0.0   │ cluster │ 88026    │ 47h    │ 0    │ online    │ 0%       │ 113.2mb  │ jenkins  │ disabled │
│ 20 │ elevate-notification    │ default     │ 1.0.0   │ cluster │ 88036    │ 47h    │ 0    │ online    │ 0%       │ 80.3mb   │ jenkins  │ disabled │
└────┴─────────────────────────┴─────────────┴─────────┴─────────┴──────────┴────────┴──────┴───────────┴──────────┴──────────┴──────────┴──────────┘

This concludes the service and dependency setup.


Run tests

Integration tests

npm run test:integration

To know more about integration tests and their implementation refer to the project Wiki.

Unit tests

npm test

Used in

This project was built to be used with Mentoring Service and User Service.

You can learn more about the full implementation of various capabilities of ELEVATE here .

Team


Open Source Dependencies

Several open source dependencies that have aided Mentoring's development:

NodeJS MongoDB Apache Kafka Jest Git