-
Notifications
You must be signed in to change notification settings - Fork 6
Extractor registration fixes #250
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
5d05259
Move heartbeat & some updates
max-zilla 247b4c6
various cleanup and bugfix
max-zilla cc4aba0
add blank exchange
max-zilla 8dca373
remove dataset queue bind
max-zilla 3680083
rename heartbeat from root
max-zilla dd12472
default API port to 8000
max-zilla efd9e62
Black formatting
max-zilla 024d1c9
parameters fixed, is now saved
tcnichol a32354a
Merge remote-tracking branch 'origin/extractor-register-fixes' into e…
tcnichol b3cd5fa
parameters fixed, is now saved
tcnichol 7e70c9e
properly registers parameters under schema
tcnichol 1c39efa
fixed docker file
tcnichol File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -5,6 +5,7 @@ | |
|
|
||
| class Settings(BaseSettings): | ||
| APP_NAME: str = "Clowder" | ||
| API_HOST: str = "http://127.0.0.1:8000" | ||
| API_V2_STR: str = "/api/v2" | ||
| admin_email: str = "[email protected]" | ||
| frontend_url: str = "http://localhost:3000" | ||
|
|
@@ -26,9 +27,11 @@ class Settings(BaseSettings): | |
| "http://localhost:3002", | ||
| ] | ||
|
|
||
| # Mongo database connection | ||
| MONGODB_URL: str = "mongodb://localhost:27017" | ||
| MONGO_DATABASE: str = "clowder2" | ||
|
|
||
| # Minio (file storage) information | ||
| MINIO_SERVER_URL: str = "localhost:9000" | ||
| MINIO_BUCKET_NAME: str = "clowder" | ||
| MINIO_ACCESS_KEY: str = "minioadmin" | ||
|
|
@@ -73,12 +76,13 @@ class Settings(BaseSettings): | |
| } | ||
|
|
||
| # RabbitMQ message bus | ||
| RABBITMQ_USER = "guest" | ||
| RABBITMQ_PASS = "guest" | ||
| RABBITMQ_HOST = "localhost" | ||
| RABBITMQ_URL = ( | ||
| RABBITMQ_USER: str = "guest" | ||
| RABBITMQ_PASS: str = "guest" | ||
| RABBITMQ_HOST: str = "localhost" | ||
| RABBITMQ_URL: str = ( | ||
| "amqp://" + RABBITMQ_USER + ":" + RABBITMQ_PASS + "@" + RABBITMQ_HOST + "/" | ||
| ) | ||
| HEARTBEAT_EXCHANGE: str = "extractors" | ||
|
|
||
|
|
||
| settings = Settings() | ||
This file contains hidden or 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 hidden or 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 was deleted.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -1,77 +1,74 @@ | ||
| import logging | ||
| import pika | ||
| import json | ||
| from packaging import version | ||
| from app.config import settings | ||
| from pymongo import MongoClient | ||
| from app.models.listeners import ( | ||
| EventListenerDB, | ||
| EventListenerOut, | ||
| ) | ||
|
|
||
| from app.config import settings | ||
| from app.models.listeners import EventListenerDB, EventListenerOut, ExtractorInfo | ||
|
|
||
| logging.basicConfig(level=logging.INFO) | ||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| def callback(ch, method, properties, body): | ||
| statusBody = json.loads(body.decode("utf-8")) | ||
| print("received extractor heartbeat: " + str(statusBody)) | ||
| extractor_id = statusBody["id"] | ||
| extractor_queue = statusBody["queue"] | ||
| extractor_info = statusBody["extractor_info"] | ||
| """This method receives messages from RabbitMQ and processes them.""" | ||
| msg = json.loads(body.decode("utf-8")) | ||
|
|
||
| extractor_info = msg["extractor_info"] | ||
| extractor_name = extractor_info["name"] | ||
| extractor_db = EventListenerDB(**extractor_info) | ||
| client = MongoClient(settings.MONGODB_URL) | ||
| db = client["clowder2"] | ||
| existing_extractor = db["listeners"].find_one({"name": extractor_queue}) | ||
| extractor_db = EventListenerDB( | ||
| **extractor_info, properties=ExtractorInfo(**extractor_info) | ||
| ) | ||
|
|
||
| mongo_client = MongoClient(settings.MONGODB_URL) | ||
| db = mongo_client[settings.MONGO_DATABASE] | ||
|
|
||
| # TODO: This block could go in app.rabbitmq.listeners register_listener and update_listener methods? | ||
| existing_extractor = db["listeners"].find_one({"name": msg["queue"]}) | ||
| if existing_extractor is not None: | ||
| # Update existing listener | ||
| existing_version = existing_extractor["version"] | ||
| new_version = extractor_db.version | ||
| if version.parse(new_version) > version.parse(existing_version): | ||
| # TODO: Should this delete old version, or just add new entry? If 1st one, why not update? | ||
| new_extractor = db["listeners"].insert_one(extractor_db.to_mongo()) | ||
| found = db["listeners"].find_one({"_id": new_extractor.inserted_id}) | ||
| removed = db["listeners"].delete_one({"_id": existing_extractor["_id"]}) | ||
| extractor_out = EventListenerOut.from_mongo(found) | ||
| print( | ||
| "extractor updated: " | ||
| + extractor_name | ||
| + ", old version: " | ||
| + existing_version | ||
| + ", new version: " | ||
| + new_version | ||
| logger.info( | ||
| "%s updated from %s to %s" | ||
| % (extractor_name, existing_version, new_version) | ||
| ) | ||
| return extractor_out | ||
| else: | ||
| # Register new listener | ||
| new_extractor = db["listeners"].insert_one(extractor_db.to_mongo()) | ||
| found = db["listeners"].find_one({"_id": new_extractor.inserted_id}) | ||
| extractor_out = EventListenerOut.from_mongo(found) | ||
| print("new extractor registered: " + extractor_name) | ||
| logger.info("New extractor registered: " + extractor_name) | ||
| return extractor_out | ||
|
|
||
|
|
||
| def listen_for_heartbeats(): | ||
| credentials = pika.PlainCredentials(settings.RABBITMQ_USER, settings.RABBITMQ_PASS) | ||
|
|
||
| parameters = pika.ConnectionParameters( | ||
| settings.RABBITMQ_HOST, 5672, "/", credentials | ||
| ) | ||
|
|
||
| connection = pika.BlockingConnection(parameters) | ||
|
|
||
| channel = connection.channel() | ||
|
|
||
| channel.exchange_declare( | ||
| exchange="extractors", exchange_type="fanout", durable=True | ||
| exchange=settings.HEARTBEAT_EXCHANGE, exchange_type="fanout", durable=True | ||
| ) | ||
|
|
||
| result = channel.queue_declare(queue="", exclusive=True) | ||
| queue_name = result.method.queue | ||
| channel.queue_bind(exchange=settings.HEARTBEAT_EXCHANGE, queue=queue_name) | ||
|
|
||
| channel.queue_bind(exchange="extractors", queue=queue_name) | ||
|
|
||
| print(" [*] Waiting for heartbeats. To exit press CTRL+C") | ||
|
|
||
| logger.info(" [*] Waiting for heartbeats. To exit press CTRL+C") | ||
| channel.basic_consume(queue=queue_name, on_message_callback=callback, auto_ack=True) | ||
|
|
||
| channel.start_consuming() | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| print("starting heartbeat listener") | ||
| listen_for_heartbeats() |
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can this block be deleted?