diff options
| -rw-r--r-- | gunicorn_config.py | 56 | ||||
| -rw-r--r-- | pysite/service_discovery.py | 22 | 
2 files changed, 54 insertions, 24 deletions
| diff --git a/gunicorn_config.py b/gunicorn_config.py index 7c86851e..4814f5bf 100644 --- a/gunicorn_config.py +++ b/gunicorn_config.py @@ -8,6 +8,7 @@ from pysite.constants import (  )  from pysite.migrations.runner import run_migrations  from pysite.queues import QUEUES +from pysite.service_discovery import wait_for_rmq  STRIP_REGEX = re.compile(r"<[^<]+?>")  WIKI_TABLE = "wiki" @@ -42,27 +43,34 @@ def _when_ready(server=None, output_func=None):      run_migrations(db, output=output) -    output("Declaring RabbitMQ queues...") - -    try: -        with Connection(hostname=RMQ_HOST, userid=RMQ_USERNAME, password=RMQ_PASSWORD, port=RMQ_PORT) as c: -            with c.channel() as channel: -                for name, queue in QUEUES.items(): -                    queue.declare(channel=channel) -                    output(f"Queue declared: {name}") - -                if not DEBUG_MODE: -                    producer = c.Producer() -                    producer.publish( -                        { -                            "event": BotEventTypes.send_embed.value, -                            "data": { -                                "target": CHANNEL_DEV_LOGS, -                                "title": "Site Deployment", -                                "description": "The site has been deployed!" -                            } -                        }, -                        routing_key=BOT_EVENT_QUEUE -                    ) -    except Exception as e: -        output(f"Failed to declare RabbitMQ Queues: {e}") +    output("Waiting for RabbitMQ...") + +    has_rmq = wait_for_rmq() + +    if not has_rmq: +        output("Timed out while waiting for RabbitMQ") +    else: +        output("RabbitMQ found, declaring RabbitMQ queues...") + +        try: +            with Connection(hostname=RMQ_HOST, userid=RMQ_USERNAME, password=RMQ_PASSWORD, port=RMQ_PORT) as c: +                with c.channel() as channel: +                    for name, queue in QUEUES.items(): +                        queue.declare(channel=channel) +                        output(f"Queue declared: {name}") + +                    if not DEBUG_MODE: +                        producer = c.Producer() +                        producer.publish( +                            { +                                "event": BotEventTypes.send_embed.value, +                                "data": { +                                    "target": CHANNEL_DEV_LOGS, +                                    "title": "Site Deployment", +                                    "description": "The site has been deployed!" +                                } +                            }, +                            routing_key=BOT_EVENT_QUEUE +                        ) +        except Exception as e: +            output(f"Failed to declare RabbitMQ Queues: {e}") diff --git a/pysite/service_discovery.py b/pysite/service_discovery.py new file mode 100644 index 00000000..2852c2fb --- /dev/null +++ b/pysite/service_discovery.py @@ -0,0 +1,22 @@ +import datetime +import socket +import time +from contextlib import closing + +from pysite.constants import RMQ_HOST, RMQ_PORT + +THIRTY_SECONDS = datetime.timedelta(seconds=30) + + +def wait_for_rmq(): +    start = datetime.datetime.now() + +    while True: +        if datetime.datetime.now() - start > THIRTY_SECONDS: +            return False + +        with closing(socket.socket(socket.AF_INET, socket.SOCK_STREAM)) as sock: +            if sock.connect_ex((RMQ_HOST, RMQ_PORT)) == 0: +                return True + +        time.sleep(0.5) | 
