wrolpi/modules/inventory/__init__.py
Roland 58de8c7c5b
Upgrading Sanic and yt-dlp. Major changes occurred because Sanic changed how multiprocessing works.
Python 3.11 is now required, old versions were causing unpredictability in tests.  (Sanic does not yet
support 3.12)

Sanic has been upgraded to 23.6.0, which is the latest version that avoids this bug:
 https://github.com/sanic-org/sanic/issues/2921

New strategy for multiprocessing is to create all multiprocessing tools in one process, then fork
to other processes.  The previous strategy was to declare multiprocessing tools at the top of every
file, or wherever they were needed at import/creation.  Now all multiprocessing tools are attached to the
app.shared_ctx.  This means `api_app` is imported in many, many places.

This forced a change in how the DownloadManager works.  Previously, it would continually run
download workers which would pull downloads from a multiprocessing.Queue.  Now, a single worker checks
for new downloads and sends a Sanic signal.

Flags have been reworked to use the `api_app`.  I removed the `which` flag functionality because the
`which` are called at import and needed their own multiprocessing.Event.
2024-04-30 10:55:25 -06:00

33 lines
1 KiB
Python

import wrolpi
from wrolpi.db import get_db_session
from .api import inventory_bp
from .inventory import logger, DEFAULT_CATEGORIES, DEFAULT_INVENTORIES
from .models import Item, Inventory
INVENTORY_INITIALIZED = False
@wrolpi.before_startup
def init(force=False):
"""
Initialize inventory categories, but only if none already exist. Initializes the inventories, but only if none
already exist.
"""
global INVENTORY_INITIALIZED
if INVENTORY_INITIALIZED and force is False:
return
logger.info('Initializing inventory')
with get_db_session(commit=True) as session:
if session.query(Item).count() == 0:
for subcategory, category in DEFAULT_CATEGORIES:
item = Item(subcategory=subcategory, category=category)
session.add(item)
if session.query(Inventory).count() == 0:
for name in DEFAULT_INVENTORIES:
inv = Inventory(name=name)
session.add(inv)
INVENTORY_INITIALIZED = True