Clients

This module implements classes for interacting with different services utilized in the PHT.

PHT Client

Client for interacting with the central user interface and for publishing messages to a message queue.

class train_lib.clients.pht_client.PHTClient(api_url: str, api_port: int = 5555, api_token: Optional[str] = None, ampq_url: Optional[str] = None, vault_url: Optional[str] = None, vault_token: Optional[str] = None)[source]

Bases: object

Client class for interacting with PHT services

get_multiple_station_pks(station_ids: List) dict[source]
get_station_pk(station_id)[source]

Get the rsa public of the station specified by station id from vault storage

Parameters

station_id – identifier of the station in vault

Returns

hex string containing an rsa public key

get_train_files_archive(train_id: str, token: Optional[str] = None, client_id: Optional[str] = None)[source]

Get the tar archive containing files for building a train from the UI api

Parameters

train_id

Returns

get_user_pk(user_id)[source]

Get the public key associated with the given user_id from vault storage

Parameters

user_id

Returns

hex string containing an rsa public key

post_route_to_vault(train_id, route, periodic=False)[source]
publish_message_rabbit_mq(message: Union[str, bytes, List[str], dict], exchange: str = 'pht', exchange_type: str = 'topic', routing_key: str = 'pht')[source]

Publish a message to rabbit mq with the given message parameters

Parameters
  • message – the message to be published

  • exchange – the identifier of the exchange

  • exchange_type

  • routing_key

Returns

RabbitMQ Consumer

Generalized RabbitMQ consumer that handles setup and error handling for processing a message queue. Override the on_message(..) method to process the received messages

class train_lib.clients.rabbitmq.Consumer(amqp_url: str, queue: Optional[str] = None, auto_reconnect=False, routing_key: Optional[str] = None)[source]

Bases: object

This is an example consumer that will handle unexpected interactions with RabbitMQ such as channel and connection closures.

If RabbitMQ closes the connection, this class will stop and indicate that reconnection is necessary. You should look at the output, as there are limited reasons why the connection may be closed, which usually are tied to permission related issues or socket timeouts.

If the channel is closed, it will indicate a problem with one of the commands that were issued and that should surface in the output as well.

EXCHANGE = 'pht'
EXCHANGE_TYPE = 'topic'
QUEUE = ''
ROUTING_KEY = '*'
acknowledge_message(delivery_tag)[source]

Acknowledge the message delivery from RabbitMQ by sending a Basic.Ack RPC method for the delivery tag.

Parameters

delivery_tag (int) – The delivery tag from the Basic.Deliver frame

add_on_cancel_callback()[source]

Add a callback that will be invoked if RabbitMQ cancels the consumer for some reason. If RabbitMQ does cancel the consumer, on_consumer_cancelled will be invoked by pika.

add_on_channel_close_callback()[source]

This method tells pika to call the on_channel_closed method if RabbitMQ unexpectedly closes the channel.

close_channel()[source]

Call to close the channel with RabbitMQ cleanly by issuing the Channel.Close RPC command.

close_connection()[source]
connect()[source]

This method connects to RabbitMQ, returning the connection handle. When the connection is established, the on_connection_open method will be invoked by pika.

Return type

pika.SelectConnection

on_basic_qos_ok(_unused_frame)[source]

Invoked by pika when the Basic.QoS method has completed. At this point we will start consuming messages by calling start_consuming which will invoke the needed RPC commands to start the process.

Parameters

_unused_frame (pika.frame.Method) – The Basic.QosOk response frame

on_bindok(_unused_frame, userdata)[source]

Invoked by pika when the Queue.Bind method has completed. At this point we will set the prefetch count for the channel.

Parameters
  • _unused_frame (pika.frame.Method) – The Queue.BindOk response frame

  • userdata (str|unicode) – Extra user data (queue name)

on_cancelok(_unused_frame, userdata)[source]

This method is invoked by pika when RabbitMQ acknowledges the cancellation of a consumer. At this point we will close the channel. This will invoke the on_channel_closed method once the channel has been closed, which will in-turn close the connection.

Parameters
  • _unused_frame (pika.frame.Method) – The Basic.CancelOk frame

  • userdata (str|unicode) – Extra user data (consumer tag)

on_channel_closed(channel, reason)[source]

Invoked by pika when RabbitMQ unexpectedly closes the channel. Channels are usually closed if you attempt to do something that violates the protocol, such as re-declare an exchange or queue with different parameters. In this case, we’ll close the connection to shutdown the object.

Parameters
  • pika.channel.Channel – The closed channel

  • reason (Exception) – why the channel was closed

on_channel_open(channel)[source]

This method is invoked by pika when the channel has been opened. The channel object is passed in so we can make use of it.

Since the channel is now open, we’ll declare the exchange to use.

Parameters

channel (pika.channel.Channel) – The channel object

on_connection_closed(_unused_connection, reason)[source]

This method is invoked by pika when the connection to RabbitMQ is closed unexpectedly. Since it is unexpected, we will reconnect to RabbitMQ if it disconnects.

Parameters
  • connection (pika.connection.Connection) – The closed connection obj

  • reason (Exception) – exception representing reason for loss of connection.

on_connection_open(_unused_connection)[source]

This method is called by pika once the connection to RabbitMQ has been established. It passes the handle to the connection object in case we need it, but in this case, we’ll just mark it unused.

Parameters

_unused_connection (pika.SelectConnection) – The connection

on_connection_open_error(_unused_connection, err)[source]

This method is called by pika if the connection to RabbitMQ can’t be established.

Parameters
  • _unused_connection (pika.SelectConnection) – The connection

  • err (Exception) – The error

on_consumer_cancelled(method_frame)[source]

Invoked by pika when RabbitMQ sends a Basic.Cancel for a consumer receiving messages.

Parameters

method_frame (pika.frame.Method) – The Basic.Cancel frame

on_exchange_declareok(_unused_frame, userdata)[source]

Invoked by pika when RabbitMQ has finished the Exchange.Declare RPC command.

Parameters
  • unused_frame (pika.Frame.Method) – Exchange.DeclareOk response frame

  • userdata (str|unicode) – Extra user data (exchange name)

on_message(_unused_channel, basic_deliver, properties, body)[source]

Invoked by pika when a message is delivered from RabbitMQ. The channel is passed for your convenience. The basic_deliver object that is passed in carries the exchange, routing key, delivery tag and a redelivered flag for the message. The properties passed in is an instance of BasicProperties with the message properties and the body is the message that was sent.

Parameters
  • _unused_channel (pika.channel.Channel) – The channel object

  • pika.Spec.Basic.Deliver – basic_deliver method

  • pika.Spec.BasicProperties – properties

  • body (bytes) – The message body

on_queue_declareok(_unused_frame, userdata)[source]

Method invoked by pika when the Queue.Declare RPC call made in setup_queue has completed. In this method we will bind the queue and exchange together with the routing key by issuing the Queue.Bind RPC command. When this command is complete, the on_bindok method will be invoked by pika.

Parameters
  • _unused_frame (pika.frame.Method) – The Queue.DeclareOk frame

  • userdata (str|unicode) – Extra user data (queue name)

open_channel()[source]

Open a new channel with RabbitMQ by issuing the Channel.Open RPC command. When RabbitMQ responds that the channel is open, the on_channel_open callback will be invoked by pika.

reconnect()[source]

Will be invoked if the connection can’t be opened or is closed. Indicates that a reconnect is necessary then stops the ioloop.

run()[source]

Run the example consumer by connecting to RabbitMQ and then starting the IOLoop to block and allow the SelectConnection to operate.

set_qos()[source]

This method sets up the consumer prefetch to only be delivered one message at a time. The consumer must acknowledge this message before RabbitMQ will deliver another one. You should experiment with different prefetch values to achieve desired performance.

setup_exchange(exchange_name)[source]

Setup the exchange on RabbitMQ by invoking the Exchange.Declare RPC command. When it is complete, the on_exchange_declareok method will be invoked by pika.

Parameters

exchange_name (str|unicode) – The name of the exchange to declare

setup_queue(queue_name)[source]

Setup the queue on RabbitMQ by invoking the Queue.Declare RPC command. When it is complete, the on_queue_declareok method will be invoked by pika.

Parameters

queue_name (str|unicode) – The name of the queue to declare.

start_consuming()[source]

This method sets up the consumer by first calling add_on_cancel_callback so that the object is notified if RabbitMQ cancels the consumer. It then issues the Basic.Consume RPC command which returns the consumer tag that is used to uniquely identify the consumer with RabbitMQ. We keep the value to use it when we want to cancel consuming. The on_message method is passed in as a callback pika will invoke when a message is fully received.

stop()[source]

Cleanly shutdown the connection to RabbitMQ by stopping the consumer with RabbitMQ. When RabbitMQ confirms the cancellation, on_cancelok will be invoked by pika, which will then closing the channel and connection. The IOLoop is started again because this method is invoked when CTRL-C is pressed raising a KeyboardInterrupt exception. This exception stops the IOLoop which needs to be running for pika to communicate with RabbitMQ. All of the commands issued prior to starting the IOLoop will be buffered but not processed.

stop_consuming()[source]

Tell RabbitMQ that you would like to stop consuming by sending the Basic.Cancel RPC command.

class train_lib.clients.rabbitmq.ReconnectingConsumer(amqp_url: str, queue: Optional[str] = None, consumer: Optional[train_lib.clients.rabbitmq.Consumer] = None)[source]

Bases: object

This is an example consumer that will reconnect if the nested Consumer indicates that a reconnect is necessary.

run()[source]