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_fastapi()

Initialize the UWS subsystem for FastAPI applications.

initialize_uws_database(logger, *[, reset])

Initialize the UWS database.

install_error_handlers(app)

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.

shutdown_fastapi()

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 and after_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:

WorkerSettings

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:

None

async initialize_uws_database(logger, *, reset=False)

Initialize the UWS database.

Parameters:
  • logger (BoundLogger) – Logger to use.

  • reset (bool, default: False) – If True, also delete all data in the database.

Return type:

None

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 allows text/plain errors for sync routes.

Parameters:

app (FastAPI)

Return type:

None

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. If sync_post_route is set in the UWSConfig that this application was configured with, a POST handler for /sync to create a sync job will be added. If sync_get_route is set, a GET handler for /sync to create a sync job will be added.

Parameters:

router (APIRouter)

Return type:

None

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:

None

override_arq_queue(arq_queue)

Change the arq used by the FastAPI route handlers.

This method is probably only useful for the test suite.

Parameters:
  • arq – New arq queue.

  • arq_queue (ArqQueue)

Return type:

None

async shutdown_fastapi()

Shut down the UWS subsystem for FastAPI applications.

This should be called during application shutdown, normally from the lifespan function of the FastAPI application.

Return type:

None