Files
py_luggage/main.py
T

72 lines
1.6 KiB
Python

import logging
import subprocess
import sys
import os
import auth.api_security as auth
import time
import threading
try:
os.remove("api.log")
os.remove("api_server.log")
os.remove("app.log")
except Exception as e:
logging.error(e)
#Logging
logging.basicConfig(
filename='app.log', # Log to a file
level=logging.DEBUG, # Log everything (DEBUG and above)
format='%(asctime)s %(levelname)s %(message)s'
)
logging.info('Application started')
try:
logging.debug('Debug info here')
except Exception as e:
logging.exception('An error occurred')
env = os.environ.copy()
env["PYTHONIOENCODING"] = "utf-8"
#migration
def run_migration():
global migration_done
process = subprocess.Popen(
["python", "migration/alembic_scipt.py"],
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL
)
process.wait()
migration_done = (process.returncode == 0)
with open("migration_status.flag", "w") as f:
f.write("done" if migration_done else "failed")
thread = threading.Thread(target=run_migration)
thread.start()
#cli and api
log_file = open("api_server.log", "w", encoding="utf-8")
api_process = subprocess.Popen(
["fastapi", "dev", "api_server.py"],
stdout=log_file,
stderr=log_file,
env=env
)
cli_process = subprocess.Popen([sys.executable, "cli.py"])
api_process.wait()
cli_process.wait()
time.sleep(60)
os.remove("migration_status.flag")
# automatic durataion validation check
while True:
time.sleep(300)
try:
auth.check_validaty()
except Exception as e:
logging.info(e)