Skip to content

Commit

Permalink
[feat] configurable dev app using env vars
Browse files Browse the repository at this point in the history
  • Loading branch information
robinovitch61 committed Feb 22, 2025
1 parent 9d1201d commit df652e6
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 23 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ kl
*.log
*.txt
*.prof
pyproject.toml
48 changes: 48 additions & 0 deletions dev/deploy.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
67 changes: 44 additions & 23 deletions dev/flask/app.py
Original file line number Diff line number Diff line change
@@ -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):
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit df652e6

Please sign in to comment.