From 25d0a5a39a9b08dc8b4b90ca9c771ebaf2b25608 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Donatas=20Rasiukevi=C4=8Dius?= Date: Tue, 21 Nov 2023 10:55:18 +0100 Subject: [PATCH] Add basic logging for facts db task --- humitifier/infra/__init__.py | 8 ++++++++ humitifier/logging.py | 6 ++++++ humitifier/tasks.py | 2 -- 3 files changed, 14 insertions(+), 2 deletions(-) create mode 100644 humitifier/logging.py diff --git a/humitifier/infra/__init__.py b/humitifier/infra/__init__.py index bf690fd..f91ad1a 100644 --- a/humitifier/infra/__init__.py +++ b/humitifier/infra/__init__.py @@ -1,22 +1,30 @@ import aiosqlite import asyncio import time +from humitifier.logging import logging from humitifier.infra.models.host import Host from humitifier.infra.models.hostfacts import HostFacts from humitifier.infra.facts import query_inventory_outputs from humitifier.db import queries from humitifier.config import CONFIG +logger = logging.getLogger(__name__) +logger.setLevel(logging.INFO) + async def update_fact_db(): hosts = list(Host.load_inventory()) ts = int(time.time()) + logger.info("Querying inventory for %s hosts", len(hosts)) outputs = await query_inventory_outputs(hosts) + logger.info("Parsing %s outputs", len(outputs)) parse_tasks = [HostFacts.from_output(out, ts) for out in outputs] hostfacts = await asyncio.gather(*parse_tasks) rows = [await hf.sql_row for hf in hostfacts] async with aiosqlite.connect(CONFIG.db) as db: + logger.info("Running schema create script on: %s", CONFIG.db) await queries.create_facts_table(db) + logger.info("Inserting %s rows into %s", len(rows), CONFIG.db) await queries.bulk_insert_host_facts(db, rows) await db.commit() diff --git a/humitifier/logging.py b/humitifier/logging.py new file mode 100644 index 0000000..ed99428 --- /dev/null +++ b/humitifier/logging.py @@ -0,0 +1,6 @@ +import logging +import sys + + +log_format = "%(asctime)s - %(name)s - %(levelname)s - %(message)s" +logging.basicConfig(stream=sys.stdout, level=logging.WARNING, format=log_format) diff --git a/humitifier/tasks.py b/humitifier/tasks.py index 3ab5d35..3e486cc 100644 --- a/humitifier/tasks.py +++ b/humitifier/tasks.py @@ -8,8 +8,6 @@ def create_scheduler(): @task_app.task(CONFIG.tasks["infra_update"]) async def infra_update(): - print("Updating facts") await update_fact_db() - print("Fact Update Complete") return task_app