Daemon fully working. Log still spamming on stderr.

This commit is contained in:
sfigato 2019-01-28 21:08:04 +01:00 committed by blallo
parent 004da87425
commit 9a99020709
Signed by: blallo
GPG Key ID: 0CBE577C9B72DC3F
2 changed files with 35 additions and 4 deletions

View File

@ -3,7 +3,9 @@
"""Console script to control the bot_z daemon""" """Console script to control the bot_z daemon"""
import errno import errno
import logging
import os import os
import sys
import time import time
import typing as T import typing as T
@ -12,9 +14,22 @@ import lockfile
from bot_z.zdaemon import daemon_process, Fifo from bot_z.zdaemon import daemon_process, Fifo
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
sh = logging.StreamHandler(stream=sys.stdout)
sh.setFormatter(logging.Formatter("%(message)s"))
logger.addHandler(sh)
@click.group() @click.group()
@click.option("-d", "--debug", is_flag=True, default=False, help="Enable debug mode.") @click.option("-d", "--debug", is_flag=True, default=False, help="Enable debug mode.")
@click.option(
"-v",
"--verbose",
is_flag=True,
default=False,
help="More verbose output from this cli.",
)
@click.option( @click.option(
"-f", "-f",
"--fifo", "--fifo",
@ -30,12 +45,17 @@ from bot_z.zdaemon import daemon_process, Fifo
help="The working dir where to launch the daemon and where the lockfile is put.", help="The working dir where to launch the daemon and where the lockfile is put.",
) )
@click.pass_context @click.pass_context
def cli(ctx: click.Context, debug: bool, fifo: str, workdir: str) -> None: def cli(
ctx: click.Context, debug: bool, verbose: bool, fifo: str, workdir: str
) -> None:
""" """
Group cli. Group cli.
""" """
if verbose:
logger.setLevel(logging.DEBUG)
ctx.ensure_object(dict) ctx.ensure_object(dict)
ctx.obj["debug"] = debug ctx.obj["debug"] = debug
ctx.obj["verbose"] = verbose
ctx.obj["fifo"] = fifo ctx.obj["fifo"] = fifo
ctx.obj["workdir"] = workdir ctx.obj["workdir"] = workdir
@ -85,7 +105,8 @@ def start_command(
""" """
Invokes daemon_process for the first time. Invokes daemon_process for the first time.
""" """
lf = lockfile.FileLock(os.path.join(ctx.obj["workdir"], "bot_z.lock")) lf = lockfile.FileLock(os.path.join(ctx.obj["workdir"], "bot_z"))
logger.debug("Daemon starting. Lockfile: %s", lf)
proxy_tuple = None proxy_tuple = None
if proxy: if proxy:
proxy_tuple = tuple(proxy.split(":")) proxy_tuple = tuple(proxy.split(":"))
@ -103,6 +124,7 @@ def start_command(
debug=ctx.obj["debug"], debug=ctx.obj["debug"],
foreground=foreground, foreground=foreground,
) )
logger.info("Daemon started.")
@cli.command("stop") @cli.command("stop")
@ -111,9 +133,11 @@ def stop_command(ctx: click.Context):
""" """
Writes on the fifo. Invokes the stop. Writes on the fifo. Invokes the stop.
""" """
logger.debug("Sending the stop command down the pipe: %s", ctx.obj["fifo"])
with open(ctx.obj["fifo"], "w") as fifo: with open(ctx.obj["fifo"], "w") as fifo:
fifo.write("stop") fifo.write("stop")
fifo.flush() fifo.flush()
logger.info("Stop sent.")
@cli.command("reload") @cli.command("reload")
@ -122,9 +146,11 @@ def reload_command(ctx: click.Context):
""" """
Writes on the fifo. Invokes the reload. Writes on the fifo. Invokes the reload.
""" """
logger.debug("Sending the reload command down the pipe: %s", ctx.obj["fifo"])
with open(ctx.obj["fifo"], "w") as fifo: with open(ctx.obj["fifo"], "w") as fifo:
fifo.write("reload") fifo.write("reload")
fifo.flush() fifo.flush()
logger.info("Reload sent.")
@cli.command("status") @cli.command("status")
@ -134,14 +160,18 @@ def status_command(ctx: click.Context):
Writes on the fifo. Queries the status. Writes on the fifo. Queries the status.
""" """
rfifo_path = os.path.join(ctx.obj["workdir"], "rfifo-{}".format(id(ctx))) rfifo_path = os.path.join(ctx.obj["workdir"], "rfifo-{}".format(id(ctx)))
logger.debug("Awaiting response on fifo: %s", rfifo_path)
try: try:
os.mkfifo(rfifo_path) os.mkfifo(rfifo_path)
logger.debug("Response fifo newly created.")
except OSError as err: except OSError as err:
if err.errno != errno.EEXIST: if err.errno != errno.EEXIST:
raise raise
logger.debug("Response fifo exists.")
with Fifo(ctx.obj["fifo"], "w") as fifo: with Fifo(ctx.obj["fifo"], "w") as fifo:
fifo.write("status|{}".format(rfifo_path)) fifo.write("status|{}".format(rfifo_path))
logger.debug("Awaiting response...")
done = False done = False
while not done: while not done:
try: try:
@ -149,9 +179,9 @@ def status_command(ctx: click.Context):
resp = rfifo.read() resp = rfifo.read()
done = True done = True
except FileNotFoundError as e: except FileNotFoundError as e:
print(e) logger.warning("File not found: %s", e)
pass pass
print(resp) logger.info(resp)
if __name__ == "__main__": if __name__ == "__main__":

View File

@ -137,6 +137,7 @@ def daemon_process(
class Fifo(object): class Fifo(object):
""" """
Iterator to continuously read the command fifo. Iterator to continuously read the command fifo.
Supports also write operations if opened with mode="r".
""" """
def __init__(self, fifopath: str, mode: str = "r") -> None: def __init__(self, fifopath: str, mode: str = "r") -> None: