Skip to content

Commit

Permalink
Started pm log hierarchy
Browse files Browse the repository at this point in the history
  • Loading branch information
PawelPlesniak committed Dec 10, 2024
1 parent 137eb5f commit b178a5d
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 44 deletions.
5 changes: 0 additions & 5 deletions src/drunc/apps/__main_pm_shell__.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@

def main():
from drunc.process_manager.interface.context import ProcessManagerContext
context = ProcessManagerContext()

try:
from drunc.process_manager.interface.shell import process_manager_shell

process_manager_shell(obj = context)

except Exception as e:
Expand All @@ -16,7 +13,5 @@ def main():
rprint(f'Exiting...')
exit(1)



if __name__ == '__main__':
main()
4 changes: 3 additions & 1 deletion src/drunc/process_manager/interface/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
from drunc.utils.shell_utils import ShellContext, GRPCDriver

class ProcessManagerContext(ShellContext): # boilerplatefest
status_receiver = None
def __init__(self):
self.status_receiver = None
super(ProcessManagerContext, self).__init__()

def reset(self, address:str=None):
self.address = address
Expand Down
18 changes: 8 additions & 10 deletions src/drunc/process_manager/interface/process_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@
import os
import logging
import getpass
from rich.console import Console
from rich.logging import RichHandler
from drunc.utils.utils import log_levels, setup_root_logger, get_logger
from drunc.process_manager.utils import get_log_path, get_pm_conf_name_from_dir
_cleanup_coroutines = []

def run_pm(pm_conf:str, pm_address:str, log_level:str, override_logs:bool, log_path:str=None, user:str=getpass.getuser(), ready_event:bool=None, signal_handler:bool=None, generated_port:bool=None):
def run_pm(pm_conf:str, pm_address:str, log_level:str, override_logs:bool, log_path:str=None, user:str=getpass.getuser(), ready_event:bool=None, signal_handler:bool=None, generated_port:bool=None) -> None:
appName = "process_manager"
pmConfFileName = get_pm_conf_name_from_dir(pm_conf) # Treating the pm conf data filename as the session

Expand All @@ -34,8 +33,6 @@ def run_pm(pm_conf:str, pm_address:str, log_level:str, override_logs:bool, log_p
from drunc.utils.utils import parent_death_pact
parent_death_pact() # If the parent dies (for example unified shell), we die too

# from rich.console import Console
# console = Console()
log.debug(f'Using \'{pm_conf}\' as the ProcessManager configuration')

from drunc.process_manager.process_manager import ProcessManager
Expand All @@ -54,6 +51,7 @@ def run_pm(pm_conf:str, pm_address:str, log_level:str, override_logs:bool, log_p
loop = asyncio.get_event_loop()

async def serve(address:str) -> None:
log.debug("serve called")
if not address:
from drunc.exceptions import DruncSetupException
raise DruncSetupException('The address on which to expect commands/send status wasn\'t specified')
Expand Down Expand Up @@ -90,10 +88,7 @@ async def server_shutdown():
)
except Exception as e:
log.error("Serving the ProcessManager received an Exception")
import os
from rich.console import Console
console = Console()
console.print_exception(width=os.get_terminal_size()[0])
log.exception(e)
finally:
if _cleanup_coroutines:
log.info("Clearing coroutines")
Expand All @@ -110,13 +105,16 @@ async def server_shutdown():
log_levels.keys(),
case_sensitive=False
),
default='DEBUG',
default='INFO',
help='Set the log level'
)
@click.option('-o/-no', '--override-logs/--no-override-logs', type=bool, default=True, help="Override logs, if --no-override-logs filenames have the timestamp of the run.")
@click.option('-lp', '--log-path', type=str, default=None, help="Log path of process_manager logs.")
@click.option('-u', '--user', type=str, default=getpass.getuser(), help="Username for process_manager logs.")
def process_manager_cli(pm_conf:str, pm_port:int, log_level:str, override_logs:bool, log_path:str, user:str):
def process_manager_cli(pm_conf:str, pm_port:int, log_level:str, override_logs:bool, log_path:str, user:str) -> None:
from drunc.utils.utils import setup_root_logger
setup_root_logger(log_level)

from drunc.process_manager.configuration import get_process_manager_configuration
pm_conf = get_process_manager_configuration(pm_conf)
run_pm(
Expand Down
28 changes: 18 additions & 10 deletions src/drunc/process_manager/interface/shell.py
Original file line number Diff line number Diff line change
@@ -1,39 +1,45 @@
import click_shell
import click
import os
import getpass

from drunc.utils.grpc_utils import ServerUnreachable
from drunc.utils.utils import CONTEXT_SETTINGS, log_levels,validate_command_facility

@click_shell.shell(prompt='drunc-process-manager > ', chain=True, context_settings=CONTEXT_SETTINGS, hist_file=os.path.expanduser('~')+'/.drunc-pm-shell.history')
@click.option('-l', '--log-level', type=click.Choice(log_levels.keys(), case_sensitive=False), default='INFO', help='Set the log level')
@click.argument('process-manager-address', type=str, callback=validate_command_facility)
@click.pass_context
def process_manager_shell(ctx, process_manager_address:str, log_level:str) -> None:
from drunc.utils.utils import setup_logger
setup_logger(log_level)

from drunc.utils.utils import setup_root_logger
setup_root_logger(log_level)
log = get_logger(

Check failure on line 16 in src/drunc/process_manager/interface/shell.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (F821)

src/drunc/process_manager/interface/shell.py:16:11: F821 Undefined name `get_logger`
logger_name = "process_manager_shell",
rich_handler = True
)
log.debug("Resetting the context instance address")
ctx.obj.reset(
address = process_manager_address,
)

from drunc.utils.grpc_utils import ServerUnreachable

desc = None
log.info(f"Connecting to process_manager at address {process_manager_address}")
try:
import asyncio
desc = asyncio.get_event_loop().run_until_complete(
ctx.obj.get_driver('process_manager').describe()
)
except ServerUnreachable as e:
ctx.obj.critical(f'Could not connect to the process manager')
raise e
log.critical(f'Could not connect to the process manager')
log.exception(e)
exit(1)

ctx.obj.info(f'{process_manager_address} is \'{desc.data.name}.{desc.data.session}\' (name.session), starting listening...')
log.info(f'{process_manager_address} is \'{desc.data.name}.{desc.data.session}\' (name.session), starting listening...')
if desc.data.HasField('broadcast'):
ctx.obj.start_listening(desc.data.broadcast)

def cleanup():
ctx.obj.terminate()

ctx.call_on_close(cleanup)

from drunc.process_manager.interface.commands import boot, terminate, kill, flush, logs, restart, ps, dummy_boot
Expand All @@ -44,4 +50,6 @@ def cleanup():
ctx.command.add_command(logs, 'logs')
ctx.command.add_command(restart, 'restart')
ctx.command.add_command(ps, 'ps')
ctx.command.add_command(dummy_boot, 'dummy_boot')
ctx.command.add_command(dummy_boot, 'dummy_boot')

log.info("process_manager_shell ready")
7 changes: 2 additions & 5 deletions src/drunc/process_manager/process_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,7 @@ class ProcessManager(abc.ABC, ProcessManagerServicer):

def __init__(self, configuration:ProcessManagerConfHandler, name:str, session:str=None, **kwargs):
super().__init__()
self.log = get_logger(
logger_name = f"{name}.{configuration.data.type._name_}_process_manager",
rich_handler = True
)
self.log = get_logger(logger_name = f"process_manager.{configuration.data.type._name_}_process_manager")
self.log.debug(pid_info_str())
self.log.debug("Initialized ProcessManager")

Expand Down Expand Up @@ -491,7 +488,7 @@ def _get_process_uid(self, query:ProcessQuery, in_boot_request:bool=False) -> [s
@staticmethod
def get(conf, **kwargs):
from drunc.utils.utils import get_logger
log = get_logger("process_manager.get", rich_handler = True)
log = get_logger("process_manager.get")

if conf.data.type == ProcessManagerTypes.SSH:
log.info(f'Starting [green]SSH process_manager[/green]', extra={'markup': True})
Expand Down
2 changes: 1 addition & 1 deletion src/drunc/process_manager/process_manager_driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def __init__(self, address:str, token, **kwargs):
token = token,
**kwargs
)
self.log.debug(f"set up process_manager.driver")
self.log.debug("set up process_manager.driver")

def create_stub(self, channel):
from druncschema.process_manager_pb2_grpc import ProcessManagerStub
Expand Down
6 changes: 3 additions & 3 deletions src/drunc/unified_shell/shell.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ def unified_shell(
else: # user provided an address
process_manager_address = process_manager.replace('grpc://', '') # remove the grpc scheme
unified_shell_log.info(f"Connecting to process manager at \'{process_manager}\' at address [green]{process_manager_address}[/green]")
unified_shell_log.info(f"Unified shell connected to the process_manager")
unified_shell_log.info(f"process_manager connected to the unified_shell")

ctx.obj.reset(
address_pm = process_manager_address,
Expand All @@ -127,7 +127,7 @@ def unified_shell(
ctx.obj.session_name = session_name

unified_shell_log.debug(f'{process_manager_address} is \'{desc.name}.{desc.session}\' (name.session), starting listening...')
unified_shell_log.info(f"process_manager listening...")
unified_shell_log.debug(f"process_manager listening...")

if desc.HasField('broadcast'):
ctx.obj.start_listening_pm(
Expand Down Expand Up @@ -207,4 +207,4 @@ def cleanup():
ctx.command.add_command(exclude, 'exclude')
ctx.command.add_command(wait, 'wait')

unified_shell_log.debug("Unified shell ready")
unified_shell_log.info("unified_shell ready with process_manager and controller commands")
19 changes: 10 additions & 9 deletions src/drunc/utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def regex_match(regex, string):

log_level = logging.INFO

def print_traceback(with_rich:bool=True): # RETURNTOME - make this false
def print_traceback(with_rich:bool=True): # RETURNTOME - rename to print_console_traceback
if with_rich:
from rich.console import Console
c = Console()
Expand All @@ -62,8 +62,8 @@ def setup_root_logger(stream_log_level:str) -> None:
return

stream_log_level = log_levels[stream_log_level]
logger.setLevel(stream_log_level)
for handler in logger.handlers:
root_logger.setLevel(stream_log_level)
for handler in root_logger.handlers:
handler.setLevel(stream_log_level)

# And then manually tweak 'sh.command' logger. Sigh.
Expand All @@ -82,15 +82,16 @@ def setup_root_logger(stream_log_level:str) -> None:
for handler in kafka_command_logger.handlers:
handler.setLevel(kafka_command_level)

def get_logger(logger_name:str, log_file_path:str = None, log_file_log_level:str = None, rich_handler:bool = False, rich_log_level:str = None):
def get_logger(logger_name:str, log_file_path:str = None, log_file_log_level:str = None, override_log_file:bool = True, rich_handler:bool = False, rich_log_level:str = None):
if logger_name == "":
raise DruncSetupException("This was an attempt to set up the root logger `drunc`, this should be corrected to command `setup_root_logger`.")
if "drunc" not in logging.Logger.manager.loggerDict:
raise DruncSetupException("The root logger has not been initialized, exiting.")
if logger_name == "process_manager" and not log_file_path and not 'drunc.process_manager' in logging.Logger.manager.loggerDict:
raise DruncSetupException("process_manager setup requires a log path.")
if logger_name == "process_manager" and not rich_handler:
raise DruncSetupException("process_manager requires a rich handler.")
if logger_name == "process_manager" and not 'drunc.process_manager' in logging.Logger.manager.loggerDict:
if not log_file_path:
raise DruncSetupException("process_manager setup requires a log path.")
if not rich_handler:
raise DruncSetupException("process_manager requires a rich handler.")

root_logger_level = logging.getLogger('drunc').level
if not log_file_log_level:
Expand All @@ -117,7 +118,7 @@ def get_logger(logger_name:str, log_file_path:str = None, log_file_log_level:str
logger.debug(f"Logger {logger_name} already exists, not overwriting handlers")
return logger

if log_file_path and os.path.isfile(log_file_path):
if override_log_file and log_file_path and os.path.isfile(log_file_path):
os.remove(log_file_path)

while logger.hasHandlers():
Expand Down

0 comments on commit b178a5d

Please sign in to comment.