wrolpi/wrolpi/api_utils.py
lrnselfreliance ab6a40be20
Some checks are pending
Backend Tests / api-tests (core) (push) Waiting to run
Backend Tests / api-tests (files) (push) Waiting to run
Backend Tests / api-tests (media) (push) Waiting to run
Backend Tests / api-tests (rest) (push) Waiting to run
Backend Tests / api-contract-tests (push) Waiting to run
Backend Tests / api-migrations-and-start (push) Waiting to run
CodeQL / Analyze (push) Waiting to run
Controller Tests / controller-tests (push) Waiting to run
Cypress Tests / cypress-component-tests (push) Waiting to run
Cypress Tests / cypress-e2e-tests (push) Waiting to run
Jest Tests / jest-tests (push) Waiting to run
API JSON: encode paths without a lookup per path (#810)
CustomJSONEncoder looked up the media directory and called Path.relative_to
for every path it encoded.  A response with hundreds of paths (an Archive's
page with 51 history entries holds 416) spent most of its encoding time
there: 18.8 ms of 19 ms per response, measured on a Mac.  The encoder now
looks up the media directory once per response and strips it as a string
prefix: 1.5 ms for the same response.

Paths outside the media directory, relative paths, and siblings that share
the media directory's name prefix are still returned unchanged; the media
directory itself and "." are still "".

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-09 19:45:43 -06:00

224 lines
8.6 KiB
Python

import asyncio
import json
import logging
import os
import signal
from datetime import datetime, timezone, date
from decimal import Decimal
from functools import wraps
from http import HTTPStatus
from pathlib import Path
from sanic import response, HTTPResponse, Request, Sanic, SanicException
from wrolpi.common import Base, get_media_directory, logger, LOGGING_CONFIG
from wrolpi.db import RequestDB, no_db_access
from wrolpi.errors import APIError
from wrolpi.perpetual import PERPETUAL_LOOPS, PER_WORKER_TASKS, PerpetualLoop, run_loop
from wrolpi.vars import PYTEST
logger = logger.getChild(__name__)
# Sanic 25.x wants to use 'spawn' as the multiprocessing start method, but our code
# uses 'fork' extensively (via multiprocessing.Manager, Event, Queue, etc.).
# Tell Sanic to use 'fork' and that the start method is already configured.
Sanic.start_method = "fork"
Sanic.START_METHOD_SET = True
def _reset_inherited_signal_handlers():
"""A forked worker carries the manager's SIGTERM/SIGINT handlers until Sanic installs the worker's own. In
that window (startup listeners, ~2s with many workers) a signal must do the default thing, exit, rather than
run the manager's handler inside the child. Otherwise a second auto-reload leaves the half-started
generation alive, untracked, holding the listening socket."""
for sig in (signal.SIGTERM, signal.SIGINT):
signal.signal(sig, signal.SIG_DFL)
os.register_at_fork(after_in_child=_reset_inherited_signal_handlers)
# The only Sanic App, this is imported all over.
api_app = Sanic(name='api_app', log_config=LOGGING_CONFIG)
@wraps(response.json)
def json_response(*a, **kwargs) -> HTTPResponse:
"""
Handles encoding date/datetime in JSON.
"""
with no_db_access('json_response'):
resp = response.json(*a, **kwargs, cls=CustomJSONEncoder, dumps=json.dumps)
return resp
class CustomJSONEncoder(json.JSONEncoder):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
# One encoder serializes one response; look up the media directory once, not once per path.
self._media_prefix = None
def _relative_path(self, path: Path) -> str:
"""The path relative to the media directory, or unchanged if it is outside of it."""
if self._media_prefix is None:
self._media_prefix = str(get_media_directory()).rstrip('/') + '/'
path = str(path)
if path.startswith(self._media_prefix):
return path[len(self._media_prefix):]
if path == '.' or path + '/' == self._media_prefix:
return ''
return path
def default(self, obj):
try:
if hasattr(obj, '__json__'):
# Get __json__ before others.
return obj.__json__()
elif isinstance(obj, datetime):
# API always returns dates in UTC.
if obj.tzinfo:
obj = obj.astimezone(timezone.utc)
else:
# A datetime with no timezone is UTC.
obj = obj.replace(tzinfo=timezone.utc)
obj = obj.isoformat()
return obj
elif isinstance(obj, date):
# API always returns dates in UTC.
obj = datetime(obj.year, obj.month, obj.day, tzinfo=timezone.utc)
return obj.isoformat()
elif isinstance(obj, Decimal):
return str(obj)
elif isinstance(obj, Base):
if hasattr(obj, 'dict'):
return obj.dict()
elif isinstance(obj, Path):
return self._relative_path(obj)
return super(CustomJSONEncoder, self).default(obj)
except Exception as e:
logger.fatal(f'Failed to JSON encode {obj}', exc_info=e)
raise
def get_error_json(exception: BaseException):
"""Return a JSON representation of the Exception instance."""
if isinstance(exception, APIError):
# An exception from WROLPi.
body = dict(error=str(exception), message=exception.summary, code=exception.code)
elif isinstance(exception, SanicException):
# An exception from Sanic.
body = dict(error=str(exception), message=exception.message, code=type(exception).__name__)
else:
# Not a WROLPi APIError error.
body = dict(
error=str(exception),
message=None,
code=type(exception).__name__,
)
if exception.__cause__:
# This exception was caused by another, follow the stack.
body['cause'] = get_error_json(exception.__cause__)
return body
def json_error_handler(request: Request, exception: Exception):
"""Converts all API APIError/SanicException to more informative json object."""
try:
body = get_error_json(exception)
except Exception as e:
logger.error('Failed to create error json', exc_info=e)
raise
error = repr(str(body["error"]))
message = repr(str(body["message"]))
code = body['code']
if logger.isEnabledFor(logging.DEBUG):
logger.error(f'API returning JSON error {type(exception).__name__} {error=} {message=} {code=}',
exc_info=exception)
else:
logger.error(f'API returning JSON error {type(exception).__name__} {error=} {message=} {code=}')
if isinstance(exception, SanicException):
return json_response(body, exception.status_code)
logger.error('Unexpected error', exc_info=exception)
# Some unknown error, use internal error code.
return json_response(body, HTTPStatus.INTERNAL_SERVER_ERROR)
api_app.error_handler.add(Exception, json_error_handler)
@api_app.middleware('request')
async def inject_session(request: Request):
"""Give the request its database handle, `request.ctx.db` (see `wrolpi.db.RequestDB`).
`request.ctx.session` is the handle's Session. It begins *deferred*, even for a POST, because it
is committed by the response middleware: a writer would hold the SQLite write lock for the whole
handler -- including the slow parts (a chunk being written to disk, a subprocess) and any
background task the handler fires before returning. A handler that writes does so in
`request.ctx.db.write()`, which begins as a writer and commits when its block exits.
Nothing connects until the handler's first statement, so requests that never touch the database
cost nothing here.
"""
request.ctx.db = RequestDB()
request.ctx.session = request.ctx.db.session
@api_app.middleware('response')
async def cleanup_session(request: Request, response_: HTTPResponse):
"""Commit (or, for an error response, roll back) the request's transaction and release its connection."""
if hasattr(request.ctx, 'db'):
request.ctx.db.finish(ok=response_.status < 400)
return response_
def perpetual_signal(sleep: int | float = 1, run_while_testing: bool = False):
"""Register a singleton background loop. The wrapped function is called forever, `sleep` seconds after each
call returns, in the ONE perpetual process that Sanic's Worker Manager owns (see wrolpi/perpetual.py). Errors
are logged and the loop continues; a long call is not re-entered until it returns.
The function is returned unchanged so tests can call it directly. Under pytest nothing is registered unless
`run_while_testing` is set."""
def wrapper(func: callable):
if PYTEST and not run_while_testing:
return func
PERPETUAL_LOOPS.append(PerpetualLoop(func.__name__, func, sleep))
return func
return wrapper
def per_worker_task(sleep: int | float = 1):
"""Register a loop that EVERY Sanic server worker runs for itself (e.g. syncing its own log level). Anything
that must happen exactly once belongs in `perpetual_signal` instead."""
def wrapper(func: callable):
if PYTEST:
return func
PER_WORKER_TASKS.append(PerpetualLoop(func.__name__, func, sleep))
return func
return wrapper
@api_app.after_server_start
async def start_per_worker_tasks(app: Sanic):
stop = asyncio.Event()
app.ctx.per_worker_stop = stop
app.ctx.per_worker_tasks = [asyncio.create_task(run_loop(loop, stop), name=loop.name) for loop in PER_WORKER_TASKS]
logger.info(f'start_per_worker_tasks pid={os.getpid()} tasks={[i.name for i in PER_WORKER_TASKS]}')
@api_app.listener('before_server_stop')
async def stop_per_worker_tasks(app: Sanic):
stop = getattr(app.ctx, 'per_worker_stop', None)
if stop is not None:
stop.set()
tasks = getattr(app.ctx, 'per_worker_tasks', None) or []
for task in tasks:
task.cancel()
if tasks:
await asyncio.gather(*tasks, return_exceptions=True)