-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #66 from ImperialCollegeLondon/messages
Basic support for broadcast messages
- Loading branch information
Showing
10 changed files
with
122 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# Data files | ||
|
||
## process-manager-pocket-kafka.json | ||
|
||
Process manager configuration file for use in local development with Kafka. |
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 @@ | ||
{ | ||
"type": "ssh", | ||
"name": "SSHProcessManager", | ||
"command_address": "0.0.0.0:10054", | ||
"authoriser": { | ||
"type": "dummy" | ||
}, | ||
"broadcaster": { | ||
"type": "kafka", | ||
"kafka_address": "127.0.0.1:30092", | ||
"publish_timeout": 2 | ||
} | ||
} |
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
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 |
---|---|---|
@@ -0,0 +1,33 @@ | ||
"""Example client that consumes messages from Kafka and sends them to the web app.""" | ||
|
||
import os | ||
from urllib.parse import urlencode | ||
from urllib.request import Request, urlopen | ||
|
||
from druncschema.broadcast_pb2 import BroadcastMessage | ||
from kafka import KafkaConsumer | ||
|
||
KAFKA_URL = os.getenv("KAFKA_URL", "127.0.0.1:30092") | ||
SERVER_URL = os.getenv("SERVER_URL", "http://localhost:8000") | ||
|
||
|
||
def main() -> None: | ||
"""Listen for Kafka messages and process them indefinitely.""" | ||
consumer = KafkaConsumer(bootstrap_servers=[KAFKA_URL]) | ||
consumer.subscribe(pattern="control.*.process_manager") | ||
|
||
print("Listening for messages from Kafka.") | ||
while True: | ||
for messages in consumer.poll(timeout_ms=500).values(): | ||
for message in messages: | ||
print(f"Message received: {message}") | ||
bm = BroadcastMessage() | ||
bm.ParseFromString(message.value) | ||
|
||
data = urlencode(dict(message=bm.data.value)) | ||
request = Request(f"{SERVER_URL}/message/", data=data.encode()) | ||
urlopen(request) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |