Mohd Saddam

3 months ago · 1 min. reading time · ~10 ·

Blogging
>
Mohd blog
>
Event-Based Consumer/Worker Apps On Devtron

Event-Based Consumer/Worker Apps On Devtron

Event-Based Consumer/Worker

1. Separate Containerized App

We must be creating separate consumer/worker apps on Devtron for the kafka-based consumers or rabbitmq based workers

Benefits:-
a) This way we can scale the consumers independently and can increase or decrease the pods of the consumer app

b) Separation Of Concerns of Logic: This is more of following the monolith vs microservices architecture pattern. Each Consumer is a separate entity and can be managed easily.

2. Consumers in the same service

We should create the consumers of a particular microservice in the same repository itself. In this way, we can reuse that particular service's already-written code to implement that app's functionality.

3.Separate Dockerfile

We must create a separate dockerfile for the consumer/worker app and a separate requirements file to use for it. In this way, we are separating the requirements of both the containerised applications as there can be some packages which are not needed to be installed in the consumer/worker app.

E.g. Dockerfile.consumer

Co Tr REE EE Ca J
I  —
pyre 3 ye 7 Ty ove oi P Pps I
0.

TU UV GE ER NV Rve or i” i

apt-get install -y wget && \

apt-get install -y curl && \

apt-get install -y vim && \

apt-get install -y awscli && \

apt-get install -y default-libmysqlclient-dev

figure proje

 
  
 

 

 

 

23

 

CMD [“python3", "backend/consumers/s

' ow

IR /home/ubuntu/main/cnext-news
consumer requirements.txt /home/ubuntu/main/c,
\ pip3 install -r consumer requirements.txt

/home/ubuntu/main/cnext -news

V PYTHONPATH “${PYTHONPATH}:/backend

art consumer.py”)

 

s/consumer requirements.

4. Code Structure

a) Separate Package:- We are creating a new consumers package in our application for kafka-based consumers.

Package Name:- consumers

b) start_consumer.py :- There will be an entry point to start this consumer.We are running the consumer here to poll the messages received from the Kafka topic. We are also initializing the Django apps here so that we can use the already created Django apps in our consumer applications as well.

18

 

       

 

import os

import sys

import django

from dotenv import load_dotenv

sys.path.append(os.getcwd())
load_dotenv('.env')

o0s.environ.setdefault( DJANGO SETTINGS MODULE', ‘backend.setting
django.setup()

 

from backen

 

.consumers.consumer_factory import KafkaConsumerFactory
from backend. consumers.consumer_config import KAFKA_ CONFIG

topic names = KAFKA CONFIG.get (“topics”)
consumer config = KAFKA CONFIG.get("conf

 

KafkaConsumerFactory (consumer config,topic names)

c) consumer_config.py:- All the Kafka-related configurations are put into this file

Pr

     

rT Poi

from backend import settings
from backend.consumers.news consumers import CategoryRelationConsumer, NewsConsume

KAFKA_CONFIG
“topics”: |
dev _django360 cnext n
dev django360 category relation”
dev_django360_ trending search”

i

 

I
“config”:
"bootstrap.servers”: settings.KAFKA BOOTSTRAP_SERVERS,

IE “auto.otfset. reset settings. .KAFKA_AUTO_OFFSET_RESET,
1 “group.i1d”: f“news-consumer”,
“enable.auto.commit”: False,
1 “session. timeout.ms": settings.KAFKA SESSION TIMEOUT MS,
| “heartbeat.interval.ms": settings.KAFKA HEARTBEAT INTERVAL MS,

 

 

1 |

JCI

’0 event handlers = {

21 ‘dev_django360_cnext_news': NewsConsumer,

7574 ‘dev_django360 category relation’: CategoryRelationConsumer,
2 ‘dev_django360 trending search’ : TrendingSearchConsumer

d) base_kafka_consumer.py:- We have created an abstract base consumer class for the kafka-based consumer. We have kept the processMessage method here which can be implemented by the subclasses.

E

 

   

class BaseKafkaConsumer (ABC):
"Abstract base class for Kafka consumer.

init (self, consumer config: dict, topic names: list, poll timeout=Non
Initializes the BaseKafkaConsumer
Args
consumer config (dict): A dictionary of configurations to initialize the
topic names (list): A list of topic names to consume from
poll_timeout (int): The timeout in seconds tor polling messages trom Kat

 

 

self.run consumer =
self.consumer config = consumer config
self.topic names = topic names
self.poll timeout = poll timeout

self. consumer = self.getConsumer()
self.graceful death = GracefulDeath()
self. startConsumer() # start consumer

startConsumer (self):
private method should not be triggered from outside this class
Starts the Kafka consumer and continuously polls for messages

re (wy

e) consumer_factory.py:-We have created a subclass of the BaseKafkaConsumer class which is processing the messages received for the consumer and it is returning the implementation class which will handle the message.

f) news_consumers.py:- We have created the factory classes here in this file which will be actual implementation classes to process the messages received from a particular topic.

[RE EP [RELY [J

   

B E——

   

pre BP

from backend.consumers.helper.news helper import NewsHelper

 

class NewsConsumer:

def init (self, *args, **kwargs):
self.news helper = NewsHelper()

 

def consume(self, msg):
1€ self.news helper.perform redis cache update for news(msg)
Hl self.news helper.perform cdn cache update for news(msg)

 

 

os 14 class CategoryRelationConsumer:

 

def init__(self, *args, **kwargs):
self .news_helper = NewsHelper()

 

def consume(self, msg):
self.news helper.perform redis cache update for category relation(msg)
self.news_helper.perform_cdn_cache_update_for_category_relation(msg)

lass TrendingSearchConsumer:

      Science and Technology
Comments

You may be interested in these jobs

  • Mobile Programming LLC

    Net Full Stack Developer

    Found in: Talent IN 2A C2 - 16 hours ago


    Mobile Programming LLC Pune, India

    Job Description : · Job Title : .NET Full Stack Developer · Location : Bangalore (Work From Office) · Experience : 4+ years · Immediate Joiner · Skills Required : · - .NET Framework and C#: Proficiency, ASP.NET and ASP.NET MVC, Angular 4+, Front-end Technologies, Cloud Platforms ...

  • LS Digital Group

    Team Lead _ Accounts

    Found in: Talent IN 2A C2 - 16 hours ago


    LS Digital Group Bangalore Urban, India

    Department : Accounts & Finance · Years of experience : 3 to 7 years · Qualification : CA (Intermediate) – Must · Job Location : Navi Mumbai OR Bangalore · Job Profile: · Managing the day-to-day operations of the finance team · Managing the FP&A process · Month-end closure ...

  • Genpact

    Senior Manager

    Found in: Talent IN C2 - 5 days ago


    Genpact Bengaluru, India Full time

    With a startup spirit and 115,000 + curious and courageous minds, we have the expertise to go deep with the world's biggest brands—and we have fun doing it We dream in digital, dare in reality, and reinvent the ways companies work to make an impact far bigger than just our bottom ...