From df652e66091ef9bea58a9957b96565f7194ea5fc Mon Sep 17 00:00:00 2001 From: Leo Robinovitch Date: Sat, 22 Feb 2025 07:42:59 -0800 Subject: [PATCH] [feat] configurable dev app using env vars --- .gitignore | 1 + dev/deploy.yaml | 48 ++++++++++++++++++++++++++++++++++ dev/flask/app.py | 67 +++++++++++++++++++++++++++++++----------------- 3 files changed, 93 insertions(+), 23 deletions(-) diff --git a/.gitignore b/.gitignore index c020248..38523e2 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,4 @@ kl *.log *.txt *.prof +pyproject.toml diff --git a/dev/deploy.yaml b/dev/deploy.yaml index e9dba37..0c5f787 100644 --- a/dev/deploy.yaml +++ b/dev/deploy.yaml @@ -97,6 +97,22 @@ spec: value: "flaskpassword" - name: POSTGRES_HOST value: "postgres-service" + - name: PERIODIC_KILL_FLASK + value: "" # "" for false + - name: PERIODIC_KILL_FLASK_SECONDS + value: "30" + - name: PERIODIC_LOGGING + value: "true" + - name: PERIODIC_LOGGING_BYTES_PER_LOG + value: "10000" + - name: PERIODIC_LOGGING_LOGS_PER_SECOND + value: "5" + - name: PERIODIC_BIG_LOGGING + value: "true" + - name: PERIODIC_BIG_LOGGING_BYTES_PER_LOG + value: "500000" + - name: PERIODIC_BIG_LOGGING_LOGS_PER_SECOND + value: "0.05" readinessProbe: httpGet: path: /health @@ -118,6 +134,22 @@ spec: value: "flaskpassword" - name: POSTGRES_HOST value: "postgres-service" + - name: PERIODIC_KILL_FLASK + value: "" # "" for false + - name: PERIODIC_KILL_FLASK_SECONDS + value: "30" + - name: PERIODIC_LOGGING + value: "true" + - name: PERIODIC_LOGGING_BYTES_PER_LOG + value: "10000" + - name: PERIODIC_LOGGING_LOGS_PER_SECOND + value: "5" + - name: PERIODIC_BIG_LOGGING + value: "true" + - name: PERIODIC_BIG_LOGGING_BYTES_PER_LOG + value: "500000" + - name: PERIODIC_BIG_LOGGING_LOGS_PER_SECOND + value: "0.05" readinessProbe: httpGet: path: /health @@ -139,6 +171,22 @@ spec: value: "flaskpassword" - name: POSTGRES_HOST value: "postgres-service" + - name: PERIODIC_KILL_FLASK + value: "" # "" for false + - name: PERIODIC_KILL_FLASK_SECONDS + value: "30" + - name: PERIODIC_LOGGING + value: "true" + - name: PERIODIC_LOGGING_BYTES_PER_LOG + value: "10000" + - name: PERIODIC_LOGGING_LOGS_PER_SECOND + value: "5" + - name: PERIODIC_BIG_LOGGING + value: "true" + - name: PERIODIC_BIG_LOGGING_BYTES_PER_LOG + value: "500000" + - name: PERIODIC_BIG_LOGGING_LOGS_PER_SECOND + value: "0.05" readinessProbe: httpGet: path: /health diff --git a/dev/flask/app.py b/dev/flask/app.py index ea92c9a..2ebf286 100644 --- a/dev/flask/app.py +++ b/dev/flask/app.py @@ -1,15 +1,16 @@ -from flask import Flask, jsonify -import psycopg2 -from psycopg2 import pool -from flask_cors import CORS -import os -import logging import json -from datetime import datetime +import logging +import os import random -import signal import threading import time +from datetime import datetime + +import psycopg2 +from flask import Flask, jsonify +from flask_cors import CORS +from psycopg2 import pool + class JSONFormatter(logging.Formatter): def format(self, record): @@ -76,39 +77,59 @@ def cleanup(): db_pool.closeall() def exit_after_delay(): - time.sleep(30) # Wait for 60 seconds - logger.info("Exiting after 1 minute delay") + delay = int(os.getenv("PERIODIC_KILL_FLASK_SECONDS", "30")) + time.sleep(delay) + logger.info("Killing flask app...") cleanup() os._exit(0) init_db() # Start the exit timer thread -# exit_thread = threading.Thread(target=exit_after_delay, daemon=True) -# exit_thread.start() +if os.getenv("PERIODIC_KILL_FLASK"): + exit_thread = threading.Thread(target=exit_after_delay, daemon=True) + exit_thread.start() -def generate_random_text(num_words): +def generate_random_text(num_bytes): words = ['hello', 'world', 'test', 'random', 'text', 'words', 'generator', 'python', 'code', 'sample'] - return ' '.join(random.choices(words, k=num_words)) + # choose words randomly, returning exactly num_bytes of text + res = "" + while len(res) < num_bytes: + res += random.choice(words) + " " + return res[:num_bytes] + def periodic_logger(): - flag = True + num_bytes = int(os.getenv("PERIODIC_LOGGING_BYTES_PER_LOG", "1000")) + logs_per_second = int(os.getenv("PERIODIC_LOGGING_LOGS_PER_SECOND", "1")) + delay = 1/logs_per_second + while True: + logger.info(generate_random_text(num_bytes)) + time.sleep(delay) + +# Periodically log random text +if os.getenv("PERIODIC_LOGGING"): + logging_thread = threading.Thread(target=periodic_logger, daemon=True) + logging_thread.start() + +def periodic_big_logger(): + num_bytes = int(os.getenv("PERIODIC_BIG_LOGGING_BYTES_PER_LOG", "500_000")) + logs_per_second = int(os.getenv("PERIODIC_BIG_LOGGING_LOGS_PER_SECOND", "0.05")) + delay = 1/logs_per_second while True: - logger.info(generate_random_text(100 if flag else 200)) - time.sleep(0.5) - flag = not flag + logger.info(generate_random_text(num_bytes)) + time.sleep(delay) -# Start the periodic logging thread -# logging_thread = threading.Thread(target=periodic_logger, daemon=True) -# logging_thread.start() +# Periodically log really long random text +if os.getenv("PERIODIC_BIG_LOGGING"): + logging_thread = threading.Thread(target=periodic_big_logger, daemon=True) + logging_thread.start() @app.route("/health") def health(): logger.info("Health check endpoint called") logger.info("FIRST Lorem ipsum \n\tdolor sit amet, consectetur adipiscing elit. \n\tSed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim \n\tad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt.") - if random.random() < 0.05: - logger.info(generate_random_text(100_000)) logger.info("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod \n\ttempor incididunt ut labore et dolore magna aliqua. \n\tUt enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt.") logger.info("LAST Lorem ipsum \n\tdolor sit amet, consectetur adipiscing elit. \n\tSed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad \n\tminim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt.") return jsonify({"status": "healthy"}), 200