-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add user course points realtime consumer; scale batch consumer down (#…
…1185) * add user course points realtime consumer; scale batch consumer down * fix helm chart config key
- Loading branch information
Showing
10 changed files
with
149 additions
and
10 deletions.
There are no files selected for viewing
2 changes: 1 addition & 1 deletion
2
...er/userCoursePointsConsumer/interfaces.ts → ...mer/common/userCoursePoints/interfaces.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 5 additions & 5 deletions
10
...umer/userCoursePointsConsumer/saveToDB.ts → ...sumer/common/userCoursePoints/saveToDB.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...umer/userCoursePointsConsumer/validate.ts → ...sumer/common/userCoursePoints/validate.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
backend/bin/kafkaConsumer/userCoursePointsRealtimeConsumer/kafkaConsumer.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import { Message as KafkaMessage, LibrdKafkaError } from "node-rdkafka" | ||
|
||
import { Mutex } from "../../../lib/await-semaphore" | ||
import { KafkaError } from "../../../lib/errors" | ||
import sentryLogger from "../../../lib/logger" | ||
import prisma from "../../../prisma" | ||
import knex from "../../../services/knex" | ||
import { createKafkaConsumer } from "../common/createKafkaConsumer" | ||
import { handleMessage } from "../common/handleMessage" | ||
import { KafkaContext } from "../common/kafkaContext" | ||
import { Message } from "../common/userCoursePoints/interfaces" | ||
import { saveToDatabase } from "../common/userCoursePoints/saveToDB" | ||
import { MessageYupSchema } from "../common/userCoursePoints/validate" | ||
import config from "../kafkaConfig" | ||
|
||
const TOPIC_NAME = [config.user_course_points_realtime_consumer.topic_name] | ||
|
||
const mutex = new Mutex() | ||
|
||
const logger = sentryLogger({ | ||
service: "kafka-consumer-user-course-points-realtime", | ||
}) | ||
const consumer = createKafkaConsumer({ logger, prisma }) | ||
|
||
consumer.connect() | ||
|
||
const context: KafkaContext = { | ||
prisma, | ||
logger, | ||
mutex, | ||
consumer, | ||
knex, | ||
topic_name: TOPIC_NAME[0], | ||
} | ||
|
||
consumer.on("ready", () => { | ||
logger.info("Ready to consume") | ||
consumer.subscribe(TOPIC_NAME) | ||
|
||
const consumerImpl = async ( | ||
error: LibrdKafkaError, | ||
messages: KafkaMessage[], | ||
) => { | ||
if (error) { | ||
logger.error(new KafkaError("Error while consuming", error)) | ||
process.exit(-1) | ||
} | ||
|
||
if (messages.length > 0) { | ||
await handleMessage<Message>({ | ||
context, | ||
kafkaMessage: messages[0], | ||
MessageYupSchema, | ||
saveToDatabase, | ||
}) | ||
setImmediate(() => { | ||
consumer.consume(1, consumerImpl) | ||
}) | ||
} else { | ||
setTimeout(() => { | ||
consumer.consume(1, consumerImpl) | ||
}, 10) | ||
} | ||
} | ||
|
||
consumer.consume(1, consumerImpl) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
helm/templates/kafka/kafka-consumer-user-course-points-realtime.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
apiVersion: apps/v1 | ||
kind: Deployment | ||
metadata: | ||
name: kafka-consumer-user-course-points-realtime | ||
labels: | ||
{{- include "helm.labels" . | nindent 4 }} | ||
spec: | ||
selector: | ||
matchLabels: | ||
app: kafka-consumer-user-course-points-realtime | ||
{{- include "helm.selectorLabels" . | nindent 6 }} | ||
replicas: {{ .Values.kafkaConsumer.userCoursePointsRealtime.replicaCount }} | ||
template: | ||
metadata: | ||
labels: | ||
app: kafka-consumer-user-course-points-realtime | ||
{{- include "helm.selectorLabels" . | nindent 8 }} | ||
spec: | ||
containers: | ||
- name: kafka-consumer-user-course-points-realtime | ||
image: "{{ .Values.image.repository }}/moocfi-backend:{{ .Values.image.tag | default .Chart.AppVersion }}" | ||
command: ["sh", "-c", "npm run kafka-consumer-user-course-points-realtime"] | ||
imagePullPolicy: Always | ||
ports: | ||
- name: backend-http | ||
containerPort: 4000 | ||
resources: | ||
limits: | ||
memory: 800Mi | ||
cpu: 80m | ||
requests: | ||
memory: 200Mi | ||
cpu: 20m | ||
envFrom: | ||
- secretRef: | ||
name: backend-secret | ||
env: | ||
- name: REDIS_PASSWORD | ||
valueFrom: | ||
secretKeyRef: | ||
name: redis | ||
key: redis-password | ||
volumeMounts: | ||
- name: google-cloud-storage-serviceaccount | ||
mountPath: "/etc/gcs" | ||
readOnly: true | ||
volumes: | ||
- name: google-cloud-storage-serviceaccount | ||
secret: | ||
secretName: google-cloud-storage-serviceaccount | ||
items: | ||
- key: account.json | ||
path: account.json |
13 changes: 13 additions & 0 deletions
13
helm/templates/kafka/user-course-points-realtime-topic.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
apiVersion: kafka.strimzi.io/v1beta1 | ||
kind: KafkaTopic | ||
metadata: | ||
name: user-course-points-realtime | ||
namespace: kafka | ||
labels: | ||
strimzi.io/cluster: kafka-cluster | ||
spec: | ||
partitions: 10 | ||
replicas: 2 | ||
config: | ||
retention.ms: 604800000 | ||
segment.bytes: 107374182 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters