Application Mixin¶
-
class
sprockets_postgres.ApplicationMixin(*args, **kwargs)[source]¶ sprockets.http.app.Applicationmixin 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.Poolon startup and shutdown by installing on_start and shutdown callbacks into theApplicationinstance.-
postgres_connector(on_error=None, on_duration=None, timeout=None, _attempt=1)[source]¶ Asynchronous context-manager that returns a
PostgresConnectorinstance from the connection pool with a cursor.Note
This function is designed to work in conjunction with the
RequestHandlerMixinand 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
asyncio.TimeoutError – when the request to retrieve a connection from the pool times out.
sprockets_postgres.ConnectionException – when the application can not connect to the configured Postgres instance.
psycopg2.Error – when Postgres raises an exception during the creation of the cursor.
- Parameters
_attempt (
int) –- Return type
typing.AbstractAsyncContextManager[sprockets_postgres.PostgresConnector]
-
async
postgres_status()[source]¶ Invoke from the
/statusRequestHandler to check that there is a Postgres connection handler available and return info about the pool.The
availableitem in the dictionary indicates that the application was able to perform aSELECT 1against the database using aPostgresConnectorinstance.The
pool_sizeitem indicates the current quantity of open connections to Postgres.The
pool_freeitem indicates the current number of idle connections available to process queries.Example return value
{ 'available': True, 'pool_size': 10, 'pool_free': 8 }
- Return type
-