|
| 1 | +import logging |
| 2 | +import pika |
| 3 | +import json |
| 4 | +from packaging import version |
| 5 | +from pymongo import MongoClient |
| 6 | + |
| 7 | +from app.config import settings |
| 8 | +from app.models.search import SearchCriteria |
| 9 | +from app.routers.feeds import FeedIn, FeedListener, FeedOut, FeedDB, associate_listener |
| 10 | +from app.models.listeners import EventListenerDB, EventListenerOut, ExtractorInfo |
| 11 | + |
| 12 | +logging.basicConfig(level=logging.INFO) |
| 13 | +logger = logging.getLogger(__name__) |
| 14 | +logger.setLevel(logging.INFO) |
| 15 | + |
| 16 | + |
| 17 | +def callback(ch, method, properties, body): |
| 18 | + """This method receives messages from RabbitMQ and processes them. |
| 19 | + the extractor info is parsed from the message and if the extractor is new |
| 20 | + or is a later version, the db is updated. |
| 21 | + """ |
| 22 | + msg = json.loads(body.decode("utf-8")) |
| 23 | + |
| 24 | + extractor_info = msg["extractor_info"] |
| 25 | + extractor_name = extractor_info["name"] |
| 26 | + extractor_db = EventListenerDB( |
| 27 | + **extractor_info, properties=ExtractorInfo(**extractor_info) |
| 28 | + ) |
| 29 | + |
| 30 | + mongo_client = MongoClient(settings.MONGODB_URL) |
| 31 | + db = mongo_client[settings.MONGO_DATABASE] |
| 32 | + |
| 33 | + # check to see if extractor alredy exists |
| 34 | + existing_extractor = db["listeners"].find_one({"name": msg["queue"]}) |
| 35 | + if existing_extractor is not None: |
| 36 | + # Update existing listener |
| 37 | + existing_version = existing_extractor["version"] |
| 38 | + new_version = extractor_db.version |
| 39 | + if version.parse(new_version) > version.parse(existing_version): |
| 40 | + # if this is a new version, add it to the database |
| 41 | + new_extractor = db["listeners"].insert_one(extractor_db.to_mongo()) |
| 42 | + found = db["listeners"].find_one({"_id": new_extractor.inserted_id}) |
| 43 | + # TODO - for now we are not deleting an older version of the extractor, just adding a new one |
| 44 | + # removed = db["listeners"].delete_one({"_id": existing_extractor["_id"]}) |
| 45 | + extractor_out = EventListenerOut.from_mongo(found) |
| 46 | + logger.info( |
| 47 | + "%s updated from %s to %s" |
| 48 | + % (extractor_name, existing_version, new_version) |
| 49 | + ) |
| 50 | + return extractor_out |
| 51 | + else: |
| 52 | + # Register new listener |
| 53 | + new_extractor = db["listeners"].insert_one(extractor_db.to_mongo()) |
| 54 | + found = db["listeners"].find_one({"_id": new_extractor.inserted_id}) |
| 55 | + extractor_out = EventListenerOut.from_mongo(found) |
| 56 | + logger.info("New extractor registered: " + extractor_name) |
| 57 | + |
| 58 | + # Assign MIME-based listener if needed |
| 59 | + if extractor_out.properties and extractor_out.properties.process: |
| 60 | + process = extractor_out.properties.process |
| 61 | + if "file" in process: |
| 62 | + # Create a MIME-based feed for this v1 extractor |
| 63 | + criteria_list = [] |
| 64 | + for mime in process["file"]: |
| 65 | + main_type = mime.split("/")[0] if mime.find("/") > -1 else mime |
| 66 | + sub_type = mime.split("/")[1] if mime.find("/") > -1 else None |
| 67 | + if sub_type: |
| 68 | + if sub_type == "*": |
| 69 | + # If a wildcard, just match on main type |
| 70 | + criteria_list.append( |
| 71 | + SearchCriteria( |
| 72 | + field="content_type_main", value=main_type |
| 73 | + ) |
| 74 | + ) |
| 75 | + else: |
| 76 | + # Otherwise match the whole string |
| 77 | + criteria_list.append( |
| 78 | + SearchCriteria(field="content_type", value=mime) |
| 79 | + ) |
| 80 | + else: |
| 81 | + criteria_list.append( |
| 82 | + SearchCriteria(field="content_type", value=mime) |
| 83 | + ) |
| 84 | + |
| 85 | + # TODO: Who should the author be for an auto-generated feed? Currently None. |
| 86 | + new_feed = FeedDB( |
| 87 | + name=extractor_name, |
| 88 | + search={ |
| 89 | + "index_name": "file", |
| 90 | + "criteria": criteria_list, |
| 91 | + "mode": "or", |
| 92 | + }, |
| 93 | + listeners=[ |
| 94 | + FeedListener(listener_id=extractor_out.id, automatic=True) |
| 95 | + ], |
| 96 | + ) |
| 97 | + db["feeds"].insert_one(new_feed.to_mongo()) |
| 98 | + |
| 99 | + return extractor_out |
| 100 | + |
| 101 | + |
| 102 | +def listen_for_heartbeats(): |
| 103 | + """ |
| 104 | +
|
| 105 | + this method runs continuously listening for extractor heartbeats send over rabbitmq |
| 106 | +
|
| 107 | + """ |
| 108 | + credentials = pika.PlainCredentials(settings.RABBITMQ_USER, settings.RABBITMQ_PASS) |
| 109 | + parameters = pika.ConnectionParameters( |
| 110 | + settings.RABBITMQ_HOST, 5672, "/", credentials |
| 111 | + ) |
| 112 | + connection = pika.BlockingConnection(parameters) |
| 113 | + channel = connection.channel() |
| 114 | + |
| 115 | + channel.exchange_declare( |
| 116 | + exchange=settings.HEARTBEAT_EXCHANGE, exchange_type="fanout", durable=True |
| 117 | + ) |
| 118 | + result = channel.queue_declare(queue="", exclusive=True) |
| 119 | + queue_name = result.method.queue |
| 120 | + channel.queue_bind(exchange=settings.HEARTBEAT_EXCHANGE, queue=queue_name) |
| 121 | + |
| 122 | + logger.info(" [*] Waiting for heartbeats. To exit press CTRL+C") |
| 123 | + channel.basic_consume(queue=queue_name, on_message_callback=callback, auto_ack=True) |
| 124 | + channel.start_consuming() |
| 125 | + |
| 126 | + |
| 127 | +if __name__ == "__main__": |
| 128 | + listen_for_heartbeats() |
0 commit comments