UWSApplication¶
- class safir.uws.UWSApplication(config)¶
Bases:
object
Glue between a FastAPI application and the UWS implementation.
An instance of this class should be created during construction of the service that will use the UWS layer. It provides methods to initialize the UWS database, build route handlers, install error handlers, and build the UWS database worker. Construction of the backend worker that does the work of the service is handled separately so that it can have minimal dependencies.
- Parameters:
config (
UWSConfig
) – UWS configuration.
Methods Summary
build_worker
(logger)Construct an arq worker configuration for the UWS worker.
Initialize the UWS subsystem for FastAPI applications.
initialize_uws_database
(logger, *[, reset])Initialize the UWS database.
Install error handlers that follow DALI and UWS conventions.
install_handlers
(router)Install the route handlers for the service.
install_middleware
(app)Install FastAPI middleware needed by UWS.
override_arq_queue
(arq_queue)Change the arq used by the FastAPI route handlers.
Shut down the UWS subsystem for FastAPI applications.
Methods Documentation
- build_worker(logger)¶
Construct an arq worker configuration for the UWS worker.
All UWS job status and results must be stored in the underlying database, since the API serves job information from there. To minimize dependencies for the worker, which may (for example) pin its own version of SQLAlchemy that may not be compatible with that used by the application, the actual worker is not responsible for storing the results in SQL. Instead, it returns results via arq, which temporarily puts them in Redis then uses
on_job_start
andafter_job_end
to notify a different queue. Those results are recovered and stored in the database by separate a separate arq worker.This function returns a class suitable for assigning to a module variable and referencing as the argument to the arq command-line tool to start the worker.
- Parameters:
logger (
BoundLogger
) – Logger to use for messages.- Return type:
- async initialize_fastapi()¶
Initialize the UWS subsystem for FastAPI applications.
This must be called before any UWS routes are accessed, normally from the lifespan function of the FastAPI application.
- Return type:
- async initialize_uws_database(logger, *, reset=False)¶
Initialize the UWS database.
- Parameters:
logger (
BoundLogger
) – Logger to use.reset (
bool
, default:False
) – IfTrue
, also delete all data in the database.
- Return type:
- install_error_handlers(app)¶
Install error handlers that follow DALI and UWS conventions.
This method must be called during application setup for any FastAPI app using the UWS layer for correct error message handling. This will change the error response for all parameter validation errors from FastAPI.
Currently these error handlers return
text/plain
errors. VOTable errors may be a better choice, but revision 1.0 of the SODA standard only allowstext/plain
errors for sync routes.- Parameters:
app (
FastAPI
)- Return type:
- install_handlers(router)¶
Install the route handlers for the service.
This method will always install a POST handler at the root of the router that creates an async job, and handlers under
/jobs
that implement the UWS protocol for managing those jobs. Ifsync_post_route
is set in theUWSConfig
that this application was configured with, a POST handler for/sync
to create a sync job will be added. Ifsync_get_route
is set, a GET handler for/sync
to create a sync job will be added.- Parameters:
router (
APIRouter
)- Return type:
- install_middleware(app)¶
Install FastAPI middleware needed by UWS.
UWS unfortunately requires that the key portion of query parameters be case-insensitive, so UWS FastAPI applications need to add custom middleware to lowercase query parameter keys. This method does that.
- Parameters:
app (
FastAPI
) – FastAPI app.- Return type:
- override_arq_queue(arq_queue)¶
Change the arq used by the FastAPI route handlers.
This method is probably only useful for the test suite.