Connection#

class momotor.django.connection.BrokerConnection(*, loop=None, executor=None, **token_pool_kwargs)#

Connection class to Momotor broker.

Provides methods to connect to the Momotor broker and send and receive messages to and from the ClientStub

Is configured through the MOMOTOR_BROKER setting.

Produces log messages on the momotor.django logger.

Parameters:
  • loop – The asyncio loop to use the executor with. If None, uses the current active event loop. (Deprecated on Python 3.8)

  • executor (Executor) – An executor to run I/O blocking tasks on. If None, uses the current loop’s default executor.

  • token_pool_kwargs – Any additional keyword arguments are passed on to the token store

async disconnect(reset=False)#

Disconnect from the broker.

If reset is True, resets the authentication state by deleting the token.

Parameters:

reset – Reset authentication state

fire_message(method_name, request, *, private_channel=False, timeout=None)#

Fire a message to the broker: send the message and don’t wait for the RPC completion

Parameters:
  • method_name (str) – Name of the gRPC method on the ClientStub to call

  • request (Message) – gRPC request message to send

  • private_channel (bool) – True to use a private channel

  • timeout (float) – Timeout in seconds. None for no timeout

get_stub(private_channel=False)#

Context manager to get an authenticated ClientStub

Parameters:

private_channel – If True, creates a new single-use channel to the broker that will be closed when the context exits. If False, uses a shared channel.

async multi_job_status_stream(*, private_channel=False, connect_timeout=None, status_timeout=None)#

Async generator that connects to the multiJobStatusStream() client endpoint and yields the JobStatusStream status messages

Parameters:
  • private_channel (bool) – True to use a private channel

  • connect_timeout (float) – timeout (in seconds) to wait until connected. None for no timeout

  • status_timeout (float) – timeout (in seconds) to wait for next status message. None for no timeout

Return type:

AsyncIterable[JobStatusStream]

async send_message(method_name, request, *, private_channel=False, timeout=None)#

Send a message to the broker’s ClientStub and await the response

If authentication fails, tries to send the message again after a delay.

Parameters:
  • method_name (str) – Name of the gRPC method on the ClientStub to call

  • request (Message) – gRPC request message to send

  • private_channel (bool) – True to use a private channel

  • timeout (float) – Timeout in seconds. None for no timeout

Return type:

Message

Returns:

The response message

async server_info()#

Get the server info.

A cached version of the server info is returned if the server info was retrieved less than MOMOTOR_BROKER.INFO_MAX_AGE seconds ago.

Return type:

ServerInfoResponse

Returns:

The server info response

momotor.django.connection.retry_connection = <function on_exception.<locals>.decorate>#

A decorator to retry a function when a transient connection error occurs.

Catches the exception and retries the function several times whenever one of the following exceptions is raised:

Produces log messages on the momotor.django logger.