Skip to content

Commit

Permalink
Merge pull request #61 from agentsofthesystem/develop
Browse files Browse the repository at this point in the history
Create Minor Release for Testing with Log Files
  • Loading branch information
jreed1701 committed Mar 29, 2024
2 parents b819a72 + f54a0a6 commit be78635
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 13 deletions.
20 changes: 14 additions & 6 deletions application/api/v1/blueprints/architect.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,20 @@ def agent_info():
# TODO - Tech Debt: Update agent info page to get this info over websocket. Works for now
# but does not scale.
for game in games:
update_dict = steam_mgr.is_update_required(
game["game_steam_build_id"],
game["game_steam_build_branch"],
game["game_steam_id"],
)
game["update_required"] = update_dict["is_required"]
game_steam_id = game["game_steam_id"]
try:
update_dict = steam_mgr.is_update_required(
game["game_steam_build_id"],
game["game_steam_build_branch"],
game_steam_id,
)
game["update_required"] = update_dict["is_required"]
except Exception:
game["update_required"] = "ERROR"
logger.error(
f"Unable to retrieve game info for game_steam_id: {game_steam_id}",
exc_info=True,
)

info: dict = platform_dict
info.update({"games": games})
Expand Down
9 changes: 9 additions & 0 deletions application/common/constants.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import logging
import platform

from datetime import datetime
Expand Down Expand Up @@ -67,6 +68,14 @@ class FileModes(Enum):
WAIT_FOR_BACKEND: int = 1
FLASK_SERVER_PORT: int = 5000
MILIS_PER_SECOND = 1000
BYTES_PER_KB = 1024
KB_PER_MB = 1024

# Logging
DEFAULT_LOG_LEVEL = logging.NOTSET
DEFAULT_LOG_DATE_FORMAT = "%m/%d/%Y %I:%M:%S %p"
DEFAULT_LOG_FORMAT = "%(filename)s:%(lineno)s %(levelname)s:%(message)s"
DEFAULT_LOG_SIZE_BYTES = 1024 * KB_PER_MB * 64 # MB

# Controls not meant to be hooked up to GUI. Just for dev/debug:
ENABLE_TIMEIT_PRINTS = False
Expand Down
2 changes: 2 additions & 0 deletions application/config/config.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import logging
import os
import platform
import uuid
Expand All @@ -19,6 +20,7 @@ class DefaultConfig:
FLASK_RUN_PORT = "5000"
FLASK_FORCE_AUTH = False # Leave as False except in testing.
FLASK_DISABLE_AUTH = False
LOG_LEVEL = logging.NOTSET

# NGINX Settings
NGINX_DEFAULT_HOSTNAME = "localhost"
Expand Down
53 changes: 50 additions & 3 deletions application/factory.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import logging
import os
import shutil
import sys

from alembic import command
from alembic.config import Config
from concurrent_log_handler import ConcurrentRotatingFileHandler
from flask import Flask

from application.common import logger, constants, toolbox
Expand Down Expand Up @@ -65,12 +69,53 @@ def _handle_migrations(flask_app: Flask):
command.upgrade(alembic_cfg, "head")


def create_app(config=None):
logger.info("Begin initialization.")
def _handle_logging(logger_level=constants.DEFAULT_LOG_LEVEL):
"""Update log configuration."""
# Remove all handlers associated with the root logger object.
for handler in logging.root.handlers[:]:
logging.root.removeHandler(handler)

log_file_folder = os.path.join(
constants.DEFAULT_INSTALL_PATH,
"logs",
)

# Remove old log directory.
if os.path.exists(log_file_folder):
shutil.rmtree(log_file_folder)

# Create the directory
os.makedirs(log_file_folder, exist_ok=True)

log_file_full_path = os.path.join(log_file_folder, "agent-smith-log.txt")

# Reconfigure logging again, this time with a file in addition to stdout.
formatter = logging.Formatter(constants.DEFAULT_LOG_FORMAT)

# File handler
file_handler = ConcurrentRotatingFileHandler(
log_file_full_path,
mode="a",
encoding="utf-8",
maxBytes=constants.DEFAULT_LOG_SIZE_BYTES,
backupCount=10,
)
file_handler.setLevel(logger_level)
file_handler.setFormatter(formatter)

# Also route to stdout
stdout_handler = logging.StreamHandler(sys.stdout)
stdout_handler.setLevel(logger_level)
stdout_handler.setFormatter(formatter)

logger.addHandler(stdout_handler)
logger.addHandler(file_handler)


def create_app(config=None):
if config is None:
config = DefaultConfig("python")
logger.critical("WARNING. Missing Configuration. Initializing with default...")
logger.warning("WARNING. Missing Configuration. Initializing with default...")

flask_app = Flask(
constants.APP_NAME,
Expand Down Expand Up @@ -133,6 +178,8 @@ def create_app(config=None):
# Run other startup checks.
_startup_checks()

_handle_logging(logger_level=config.LOG_LEVEL)

logger.info(f"{constants.APP_NAME} has been successfully created.")

return flask_app
7 changes: 5 additions & 2 deletions application/gui/launch.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import logging
import os
import time

Expand Down Expand Up @@ -54,9 +55,11 @@ def _create_backend(self) -> Flask:
config = DefaultConfig("python")
config.obtain_environment_variables()

config.DEBUG = False
config.LOG_LEVEL = logging.INFO
config.ENV = "production"

app = create_app(config=config)
app.debug = False
app.config["ENV"] = "production"

return app

Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
alembic
concurrent-log-handler
debugpy
flask
Flask-SQLAlchemy
Expand Down
4 changes: 2 additions & 2 deletions server.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import argparse as _argparse
import logging
import sys as _sys

from application.config.config import DefaultConfig
Expand Down Expand Up @@ -45,6 +44,7 @@ def apply(self):

config.DEBUG = True
config.ENV = "development"
config.LOG_LEVEL = logging.DEBUG

self.app = create_app(config=config)

Expand Down

0 comments on commit be78635

Please sign in to comment.