Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions backend/beets_flask/database/setup.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from contextlib import contextmanager
from functools import wraps
from typing import Optional

from deprecated import deprecated
from quart import Quart
Expand All @@ -15,7 +16,7 @@
session_factory: scoped_session[Session]


def setup_database(app: Quart) -> None:
def setup_database(app: Optional[Quart] = None) -> None:
"""Set up the database connection and session factory for the FLask application.

This function initializes the global `engine` and `session_factory` variables
Expand All @@ -37,10 +38,12 @@ def setup_database(app: Quart) -> None:

_create_tables(engine)

# Gracefully shutdown the database session
@app.teardown_appcontext
def shutdown_session(exception=None) -> None:
session_factory.remove()
if app is not None:
# Gracefully shutdown the database session, if launched
# from within a Flask app context.
@app.teardown_appcontext
def shutdown_session(exception=None) -> None:
session_factory.remove()


def __setup_factory():
Expand Down
4 changes: 0 additions & 4 deletions backend/beets_flask/server/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,6 @@ def create_app(config: str | ServerConfig | None = None) -> Quart:
register_routes(app)
register_socketio(app)

from ..watchdog.inbox import register_inboxes

register_inboxes()

log.debug("Quart app created!")

return app
Expand Down
18 changes: 10 additions & 8 deletions backend/beets_flask/watchdog/inbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@
# init and watchdog #
# ------------------------------------------------------------------------------------ #

_inboxes: List[OrderedDict] = []


def register_inboxes(timeout: float = 2.5, debounce: float = 30) -> AIOWatchdog | None:
"""
Expand All @@ -42,9 +40,13 @@ def register_inboxes(timeout: float = 2.5, debounce: float = 30) -> AIOWatchdog
You have to wait at least this long before an autotag will trigger
after you add the last file to an inbox.
Default is 30 seconds.

Notes
-----
- This should not be called from uvicorn workers to avoid concurrency issues.
You only want one watchdog (use separate init script).
"""
global _inboxes
_inboxes = get_config()["gui"]["inbox"]["folders"].flatten().values() # type: ignore
_inboxes = get_inboxes()

if os.environ.get("RQ_WORKER_ID", None):
# only launch the observer on the main process
Expand Down Expand Up @@ -205,7 +207,7 @@ def get_inbox_for_path(path: str | Path):
if isinstance(path, str):
path = Path(path)
inbox = None
for i in _inboxes:
for i in get_inboxes():
ipath = Path(i["path"])
if path.is_relative_to(ipath) or path == ipath:
inbox = i
Expand All @@ -214,12 +216,12 @@ def get_inbox_for_path(path: str | Path):


def get_inbox_folders() -> List[str]:
return [i["path"] for i in _inboxes]
return [i["path"] for i in get_inboxes()]


def is_inbox_folder(path: str) -> bool:
return path in get_inbox_folders()


def get_inboxes():
return _inboxes
def get_inboxes() -> List[OrderedDict]:
return get_config()["gui"]["inbox"]["folders"].flatten().values() # type: ignore
21 changes: 21 additions & 0 deletions backend/launch_db_init.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import os

# dirty workaround, we pretend this is a rq worker so we get the logger to create
# a child log with pid
os.environ.setdefault("RQ_JOB_ID", "dbin")

from beets.ui import _open_library

from beets_flask.config.beets_config import get_config
from beets_flask.database import setup_database
from beets_flask.logger import log

if __name__ == "__main__":
log.debug("Launching database init worker")

# ensue beets own db is created
config = get_config()
_open_library(config)

# ensure beets-flask db is created
setup_database()
19 changes: 19 additions & 0 deletions backend/launch_watchdog_worker.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import asyncio
import os

# dirty workaround, we pretend this is a rq worker so we get the logger to create
# a child log with pid
os.environ.setdefault("RQ_JOB_ID", "wdog")

from beets_flask.logger import log
from beets_flask.watchdog.inbox import register_inboxes


async def main():
log.debug(f"Launching inbox watchdog worker")
watchdog = register_inboxes()


if __name__ == "__main__":
asyncio.run(main())
asyncio.get_event_loop().run_forever()
6 changes: 6 additions & 0 deletions docker/entrypoints/entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,14 @@ cd /repo/backend

redis-server --daemonize yes >/dev/null 2>&1


# blocking
python ./launch_db_init.py
python ./launch_redis_workers.py > /logs/redis_workers.log 2>&1

# keeps running in the background
python ./launch_watchdog_worker.py &

redis-cli FLUSHALL >/dev/null 2>&1

# we need to run with one worker for socketio to work
Expand Down
7 changes: 7 additions & 0 deletions docker/entrypoints/entrypoint_dev.sh
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,14 @@ cd /repo/backend

redis-server --daemonize yes


# blocking
python ./launch_db_init.py
python ./launch_redis_workers.py

# keeps running in the background
python ./launch_watchdog_worker.py &

redis-cli FLUSHALL


Expand All @@ -50,6 +56,7 @@ uvicorn beets_flask.server.app:create_app --port 5001 \
--host 0.0.0.0 \
--factory \
--workers 1 \
--use-colors \
--reload


Expand Down
3 changes: 2 additions & 1 deletion docker/entrypoints/entrypoint_fix_permissions.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ if [ ! -z "$USER_ID" ] && [ ! -z "$GROUP_ID" ]; then
usermod -u $USER_ID -g $GROUP_ID beetle > /dev/null 2>&1
chown -R beetle:beetle /home/beetle
chown -R beetle:beetle /repo
fi
chown -R beetle:beetle /logs
fi
Loading