-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
22 changed files
with
738 additions
and
87 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,4 +4,5 @@ flake8==3.5.0 | |
pytest==3.8.2 | ||
coveralls==1.5.1 | ||
requests-mock==1.5.2 | ||
semver==2.8.1 | ||
semver==2.8.1 | ||
jinja2==2.10 |
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
Empty file.
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,58 @@ | ||
from kafka import KafkaConsumer | ||
from kafka.consumer.fetcher import ConsumerRecord | ||
from typing import List | ||
import json | ||
from feeds.api.util import parse_notification_params | ||
from feeds.logger import ( | ||
log, | ||
log_error | ||
) | ||
from feeds.activity.notification import Notification | ||
from feeds.managers.notification_manager import NotificationManager | ||
|
||
|
||
class KafkaNotificationConsumer(object): | ||
def __init__(self, server: str, topics: List[str], group_id: str) -> None: | ||
self.server = server | ||
self.topics = topics | ||
self.group_id = group_id | ||
self.consumer = KafkaConsumer(topics[0], | ||
client_id="feeds-kakfa-consumer", | ||
bootstrap_servers=[server], | ||
consumer_timeout_ms=1000, | ||
group_id=group_id, | ||
enable_auto_commit=True, | ||
auto_commit_interval_ms=1000, | ||
auto_offset_reset="earliest") | ||
|
||
def poll(self) -> None: | ||
for msg in self.consumer: | ||
print(msg) | ||
self._process_message(msg) | ||
|
||
def _process_message(self, message: ConsumerRecord) -> None: | ||
try: | ||
note_params = parse_notification_params(json.loads(message.value)) | ||
# create a Notification from params. | ||
new_note = Notification( | ||
note_params.get('actor'), | ||
note_params.get('verb'), | ||
note_params.get('object'), | ||
note_params.get('source'), | ||
level=note_params.get('level'), | ||
target=note_params.get('target', []), | ||
context=note_params.get('context'), | ||
expires=note_params.get('expires'), | ||
external_key=note_params.get('external_key'), | ||
users=note_params.get('users', []) | ||
) | ||
# pass it to the NotificationManager to dole out to its audience feeds. | ||
manager = NotificationManager() | ||
manager.add_notification(new_note) | ||
log(__name__, "Created notification from Kafka with id {}".format(new_note.id)) | ||
except Exception as e: | ||
log_error(__name__, e) | ||
|
||
def __str__(self): | ||
return ("KafkaNotificationConsumer: host:{},group_id:{}," | ||
"topics:{}".format(self.server, self.group_id, self.topics)) |
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,24 @@ | ||
""" | ||
Main controller class for the "app" that listens to Kafka. | ||
In __init__, this sets up the environment and build the KafkaConsumer. | ||
Activating it with start_listening() will run an endless loop that will | ||
listen for notifications coming in from Kafka, and push them into | ||
the Mongo feeds db by the same mechanism as the main server. | ||
""" | ||
|
||
from feeds.config import get_kafka_config | ||
from .consumer import KafkaNotificationConsumer | ||
import time | ||
|
||
|
||
class KafkaListener(object): | ||
def __init__(self): | ||
kafka_cfg = get_kafka_config() | ||
self.consumer = KafkaNotificationConsumer(kafka_cfg.kafka_host, | ||
kafka_cfg.kafka_topics, | ||
kafka_cfg.kafka_group_id) | ||
|
||
def start_listening(self): | ||
while True: | ||
self.consumer.poll() | ||
time.sleep(1) |
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,9 @@ | ||
from .kafka.listener import KafkaListener | ||
from logger import log | ||
|
||
if __name__ == "__main__": | ||
name = 'kafka_listener' | ||
log(name, "Initializing Kafka listener.") | ||
listener = KafkaListener() | ||
log(name, "Starting Kafka listener loop.") | ||
listener.start_listening() |
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 |
---|---|---|
|
@@ -6,3 +6,4 @@ cachetools==2.1.0 | |
pymongo==3.7.2 | ||
redis==2.10.6 | ||
flask-cors==3.0.6 | ||
kafka-python==1.4.4 |
Oops, something went wrong.