mirror of
https://github.com/migatu/conjurer.git
synced 2026-07-14 21:38:38 +00:00
Dockerization + ai review recomendations.
This commit is contained in:
+43
-10
@@ -1,12 +1,14 @@
|
||||
import json
|
||||
import logging
|
||||
import os
|
||||
import re
|
||||
import threading
|
||||
import time
|
||||
from queue import Empty, Queue
|
||||
from typing import Optional
|
||||
from urllib import request as urequest
|
||||
|
||||
from flask import Flask, jsonify, request
|
||||
from flask import Flask, abort, jsonify, request
|
||||
from waitress import serve
|
||||
|
||||
HOST_ADDRESS = "192.168.1.191"
|
||||
@@ -31,6 +33,13 @@ PREPPED_TRACKS = {
|
||||
}
|
||||
logger = logging.getLogger("discord")
|
||||
|
||||
API_KEY = os.getenv("CONJURER_API_KEY")
|
||||
|
||||
|
||||
def _authorize_request() -> None:
|
||||
if API_KEY and request.headers.get("X-Conjurer-Api-Key") != API_KEY:
|
||||
abort(401)
|
||||
|
||||
|
||||
class QueryControl:
|
||||
"""
|
||||
@@ -53,6 +62,7 @@ class QueryControl:
|
||||
|
||||
@app.route("/prepped_tracks", methods=["POST"])
|
||||
def log_radio_tracks():
|
||||
_authorize_request()
|
||||
app.logger = logging.getLogger("discord")
|
||||
|
||||
app.logger.info(request)
|
||||
@@ -79,6 +89,7 @@ def log_radio_tracks():
|
||||
|
||||
@app.route("/conjurer", methods=["POST"])
|
||||
def answer_external_command():
|
||||
_authorize_request()
|
||||
"""
|
||||
The function `answer_external_command` logs the request data, loads the data as JSON, logs the
|
||||
record, and then puts the record into an incoming queue before returning a success message.
|
||||
@@ -129,7 +140,7 @@ def waitress_run():
|
||||
serve(app, host=HOST_ADDRESS, port=PORT_ADDRESS)
|
||||
|
||||
|
||||
def scan_queue():
|
||||
def scan_queue(stop_event: Optional[threading.Event] = None):
|
||||
"""
|
||||
The function `scan_queue` reads data from a queue, logs it, and appends it to another queue.
|
||||
|
||||
@@ -140,12 +151,18 @@ def scan_queue():
|
||||
"""
|
||||
logger = logging.getLogger("discord")
|
||||
while True:
|
||||
data = OUT_COMM_Q.get()
|
||||
if stop_event and stop_event.is_set():
|
||||
logger.info("scan_queue: stop requested")
|
||||
break
|
||||
try:
|
||||
data = OUT_COMM_Q.get(timeout=1)
|
||||
except Empty:
|
||||
continue
|
||||
logger.info(data)
|
||||
awaiting_q.append(data)
|
||||
|
||||
|
||||
def scan_incoming():
|
||||
def scan_incoming(stop_event: Optional[threading.Event] = None):
|
||||
"""
|
||||
The `scan_incoming` function continuously checks for incoming data, processes it, and logs when data
|
||||
is found.
|
||||
@@ -157,6 +174,9 @@ def scan_incoming():
|
||||
"""
|
||||
logger = logging.getLogger("discord")
|
||||
while True:
|
||||
if stop_event and stop_event.is_set():
|
||||
logger.info("scan_incoming: stop requested")
|
||||
break
|
||||
try:
|
||||
answer = incoming_q.get(block=False)
|
||||
logger.info("DATA FOUND")
|
||||
@@ -204,7 +224,7 @@ def id3(url: str) -> dict:
|
||||
return tagdata
|
||||
|
||||
|
||||
def comm_subroutine():
|
||||
def comm_subroutine(stop_event: Optional[threading.Event] = None):
|
||||
"""
|
||||
The `comm_subroutine` function starts multiple threads to run different tasks concurrently.
|
||||
|
||||
@@ -217,14 +237,27 @@ def comm_subroutine():
|
||||
logger.info("Started comms")
|
||||
threads = []
|
||||
# threads.append(threading.Thread(target=flask_debug))
|
||||
threads.append(threading.Thread(target=waitress_run))
|
||||
threads.append(threading.Thread(target=scan_queue))
|
||||
threads.append(threading.Thread(target=scan_incoming))
|
||||
threads.append(
|
||||
threading.Thread(target=waitress_run, daemon=True)
|
||||
)
|
||||
threads.append(
|
||||
threading.Thread(target=scan_queue, kwargs={"stop_event": stop_event}, daemon=True)
|
||||
)
|
||||
threads.append(
|
||||
threading.Thread(target=scan_incoming, kwargs={"stop_event": stop_event}, daemon=True)
|
||||
)
|
||||
|
||||
for worker in threads:
|
||||
worker.start()
|
||||
for worker in threads:
|
||||
worker.join()
|
||||
|
||||
try:
|
||||
while any(thread.is_alive() for thread in threads):
|
||||
if stop_event and stop_event.is_set():
|
||||
break
|
||||
time.sleep(0.5)
|
||||
finally:
|
||||
if stop_event:
|
||||
stop_event.set()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
Reference in New Issue
Block a user