| 1 |
"""Use pika with the Tornado IOLoop |
| 2 |
|
| 3 |
""" |
| 4 |
from __future__ import annotations |
| 5 |
|
| 6 |
import logging |
| 7 |
from typing import Any, Callable, Optional, Sequence, Union, TYPE_CHECKING |
| 8 |
|
| 9 |
from tornado import ioloop |
| 10 |
|
| 11 |
from pika.adapters.utils import nbio_interface, selector_ioloop_adapter |
| 12 |
from pika.adapters import base_connection |
| 13 |
|
| 14 |
if TYPE_CHECKING: |
| 15 |
from pika import connection |
| 16 |
from pika.adapters.utils import connection_workflow |
| 17 |
|
| 18 |
LOGGER = logging.getLogger(__name__) |
| 19 |
|
| 20 |
|
| 21 |
class TornadoConnection(base_connection.BaseConnection): |
| 22 |
"""The TornadoConnection runs on the Tornado IOLoop. |
| 23 |
""" |
| 24 |
|
| 25 |
def __init__(self, |
| 26 |
parameters: Optional[connection.Parameters] = None, |
| 27 |
on_open_callback: Optional[Callable[[connection.Connection], |
| 28 |
None]] = None, |
| 29 |
on_open_error_callback: Optional[Callable[ |
| 30 |
[connection.Connection, BaseException], None]] = None, |
| 31 |
on_close_callback: Optional[Callable[ |
| 32 |
[connection.Connection, BaseException], None]] = None, |
| 33 |
custom_ioloop: Optional[Union[ |
| 34 |
ioloop.IOLoop, nbio_interface.AbstractIOServices]] = None, |
| 35 |
internal_connection_workflow: bool = True): |
| 36 |
"""Create a new instance of the TornadoConnection class, connecting |
| 37 |
to RabbitMQ automatically. |
| 38 |
|
| 39 |
:param pika.connection.Parameters|None parameters: The connection |
| 40 |
parameters |
| 41 |
:param callable|None on_open_callback: The method to call when the |
| 42 |
connection is open |
| 43 |
:param callable|None on_open_error_callback: Called if the connection |
| 44 |
can't be established or connection establishment is interrupted by |
| 45 |
`Connection.close()`: |
| 46 |
on_open_error_callback(Connection, exception) |
| 47 |
:param callable|None on_close_callback: Called when a previously fully |
| 48 |
open connection is closed: |
| 49 |
`on_close_callback(Connection, exception)`, where `exception` is |
| 50 |
either an instance of `exceptions.ConnectionClosed` if closed by |
| 51 |
user or broker or exception of another type that describes the |
| 52 |
cause of connection failure |
| 53 |
:param ioloop.IOLoop|nbio_interface.AbstractIOServices|None custom_ioloop: |
| 54 |
Override using the global IOLoop in Tornado |
| 55 |
:param bool internal_connection_workflow: True for autonomous connection |
| 56 |
establishment which is default; False for externally-managed |
| 57 |
connection workflow via the `create_connection()` factory |
| 58 |
|
| 59 |
""" |
| 60 |
if isinstance(custom_ioloop, nbio_interface.AbstractIOServices): |
| 61 |
nbio = custom_ioloop |
| 62 |
else: |
| 63 |
nbio = (selector_ioloop_adapter.SelectorIOServicesAdapter( |
| 64 |
custom_ioloop or ioloop.IOLoop.instance())) # type: ignore |
| 65 |
super().__init__( |
| 66 |
parameters, |
| 67 |
on_open_callback, |
| 68 |
on_open_error_callback, |
| 69 |
on_close_callback, |
| 70 |
nbio, |
| 71 |
internal_connection_workflow=internal_connection_workflow) |
| 72 |
|
| 73 |
@classmethod |
| 74 |
def create_connection( |
| 75 |
cls, |
| 76 |
connection_configs: Sequence[connection.Parameters], |
| 77 |
on_done: Callable[[ |
| 78 |
Union[connection.Connection, |
| 79 |
connection_workflow.AMQPConnectorException] |
| 80 |
], None], |
| 81 |
custom_ioloop: Optional[Any] = None, |
| 82 |
workflow: Optional[ |
| 83 |
connection_workflow.AbstractAMQPConnectionWorkflow] = None |
| 84 |
) -> connection_workflow.AbstractAMQPConnectionWorkflow: |
| 85 |
"""Implement |
| 86 |
:py:classmethod::`pika.adapters.BaseConnection.create_connection()`. |
| 87 |
|
| 88 |
""" |
| 89 |
nbio = selector_ioloop_adapter.SelectorIOServicesAdapter( |
| 90 |
custom_ioloop or ioloop.IOLoop.instance()) # type: ignore |
| 91 |
|
| 92 |
def connection_factory( |
| 93 |
params: Optional[connection.Parameters]) -> 'TornadoConnection': |
| 94 |
"""Connection factory.""" |
| 95 |
if params is None: |
| 96 |
raise ValueError('Expected pika.connection.Parameters ' |
| 97 |
'instance, but got None in params arg.') |
| 98 |
return cls(parameters=params, |
| 99 |
custom_ioloop=nbio, |
| 100 |
internal_connection_workflow=False) |
| 101 |
|
| 102 |
return cls._start_connection_workflow( |
| 103 |
connection_configs=connection_configs, |
| 104 |
connection_factory=connection_factory, |
| 105 |
nbio=nbio, |
| 106 |
workflow=workflow, |
| 107 |
on_done=on_done) |