Skip to content

Commit

Permalink
Add basic logging for facts db task
Browse files Browse the repository at this point in the history
  • Loading branch information
fliepeltje committed Nov 21, 2023
1 parent bd9b8cb commit 25d0a5a
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
8 changes: 8 additions & 0 deletions humitifier/infra/__init__.py
Original file line number Diff line number Diff line change
@@ -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()

Expand Down
6 changes: 6 additions & 0 deletions humitifier/logging.py
Original file line number Diff line number Diff line change
@@ -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)
2 changes: 0 additions & 2 deletions humitifier/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 25d0a5a

Please sign in to comment.