75 lines
1.7 KiB
Python
75 lines
1.7 KiB
Python
import argparse
|
|
import asyncio
|
|
import signal
|
|
|
|
from loguru import logger
|
|
|
|
from archives import _name, _version
|
|
from archives.config import read_config, setup_logger
|
|
from archives.ducache import DuCache
|
|
from archives.http import AsyncServer
|
|
|
|
|
|
async def async_main(conf):
|
|
server = AsyncServer(conf)
|
|
await server.serve()
|
|
|
|
loop = asyncio.get_running_loop()
|
|
loop.add_signal_handler(signal.SIGINT, server.stop_graceful)
|
|
loop.add_signal_handler(signal.SIGTERM, server.stop_graceful)
|
|
|
|
while True:
|
|
await asyncio.sleep(3600)
|
|
|
|
|
|
def get_parser():
|
|
parser = argparse.ArgumentParser(
|
|
formatter_class=argparse.ArgumentDefaultsHelpFormatter
|
|
)
|
|
|
|
parser.add_argument(
|
|
"-d", "--debug", action="store_true", help="print debug output"
|
|
)
|
|
parser.add_argument(
|
|
"-c",
|
|
"--config",
|
|
help="path to config file",
|
|
default="/etc/archives.toml",
|
|
)
|
|
|
|
return parser
|
|
|
|
|
|
def init():
|
|
parser = get_parser()
|
|
args = parser.parse_args()
|
|
conf = read_config(args.config)
|
|
return conf
|
|
|
|
|
|
def run_app():
|
|
conf = init()
|
|
|
|
env = conf["archives"]["env"]
|
|
logger.info(f"{_name} v{_version}, in mode: {env}")
|
|
if logfile := conf['log'].get("file"):
|
|
logger.info(f"logging to file '{logfile}'")
|
|
|
|
loglevel = conf['log'].get("level", "INFO")
|
|
setup_logger(logfile, loglevel)
|
|
|
|
asyncio.run(async_main(conf))
|
|
|
|
|
|
def update_du_cache():
|
|
conf = init()
|
|
setup_logger(conf['log'].get('file'), conf['log'].get('level'))
|
|
|
|
_path = conf["archives"]["path"]
|
|
_du_cache_file = conf["archives"]["du_cache_file"]
|
|
|
|
cache = DuCache(_du_cache_file, _path)
|
|
logger.info(f"updating du cache from '{_path}'")
|
|
count = cache.update()
|
|
logger.info(f"done, udpated {count} paths")
|