Application Mixin

class sprockets_postgres.ApplicationMixin(*args, **kwargs)[source]

sprockets.http.app.Application mixin for handling the connection to Postgres and exporting functions for querying the database, getting the status, and proving a cursor.

Automatically creates and shuts down aiopg.Pool on startup and shutdown by installing on_start and shutdown callbacks into the Application instance.

postgres_connector(on_error=None, on_duration=None, timeout=None, _attempt=1)[source]

Asynchronous context-manager that returns a PostgresConnector instance from the connection pool with a cursor.

Note

This function is designed to work in conjunction with the RequestHandlerMixin and is generally not invoked directly.

Parameters
  • on_error (typing.Optional[typing.Callable]) – A callback function that is invoked on exception. If an exception is returned from that function, it will raise it.

  • on_duration (typing.Optional[typing.Callable]) – An optional callback function that is invoked after a query has completed to record the duration that encompasses both executing the query and retrieving the returned records, if any.

  • timeout (Timeout) – Used to override the default query timeout.

Raises
Parameters

_attempt (int) –

Return type

typing.AbstractAsyncContextManager[sprockets_postgres.PostgresConnector]

property postgres_is_connected

Returns True if Postgres is currently connected

Return type

bool

async postgres_status()[source]

Invoke from the /status RequestHandler to check that there is a Postgres connection handler available and return info about the pool.

The available item in the dictionary indicates that the application was able to perform a SELECT 1 against the database using a PostgresConnector instance.

The pool_size item indicates the current quantity of open connections to Postgres.

The pool_free item indicates the current number of idle connections available to process queries.

Example return value

{
    'available': True,
    'pool_size': 10,
    'pool_free': 8
}
Return type

dict